From 2eacc71acc6d4f83a49d5765f8e63d166317b9ee Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 14 May 2024 20:28:26 -0400 Subject: [PATCH 1/2] Logging improvements * Consistently use logging functions instead of `print` * Wording/formatting fixes * Use `info(...)` instead of `verbose('VERBOSE:', ...)` --- ddclient.in | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/ddclient.in b/ddclient.in index ecb7820..3edb930 100755 --- a/ddclient.in +++ b/ddclient.in @@ -1226,8 +1226,7 @@ sub main { $SIG{'CHLD'} = 'IGNORE'; my $pid = fork; if ($pid < 0) { - print STDERR "${program}: can not fork ($!)\n"; - exit -1; + fatal("failed to fork: %s", $!); } elsif ($pid) { exit 0; } @@ -2274,17 +2273,17 @@ sub pipecmd { ## execute the command. local *FD; if (!open(FD, $cmd)) { - printf STDERR "%s: cannot execute command %s.\n", $program, $cmd; + warning('cannot execute command %s.', $cmd); } elsif ($stdin && (!print FD "$stdin\n")) { - printf STDERR "%s: failed writting to %s.\n", $program, $cmd; + warning('failed writting to %s.', $cmd); close(FD); } elsif (!close(FD)) { - printf STDERR "%s: failed closing %s.(%s)\n", $program, $cmd, $@; + warning('failed closing %s. (%s)', $cmd, $@); } elsif (opt('exec') && $?) { - printf STDERR "%s: failed %s. (%s)\n", $program, $cmd, $@; + warning('failed %s. (%s)', $cmd, $@); } else { $ok = 1; @@ -2391,6 +2390,7 @@ sub ynu { ## warning ## fatal ###################################################################### +my $_in_msg = 0; sub _msg { my $fh = shift; my $log = shift; @@ -2413,7 +2413,11 @@ sub _msg { print $fh $buffer; $msgs .= $buffer if $log; - logger($buffer) if $log; + if ($log && !$_in_msg) { + ++$_in_msg; # avoid infinite recursion if logger calls _msg + logger($buffer); + --$_in_msg; + } } sub msg { _msg(*STDOUT, 0, '', @_); } @@ -5205,7 +5209,7 @@ sub nic_nfsn_update { my $ip = delete $config{$h}{'wantip'}; info("setting IP address to %s for %s", $ip, $h); - verbose("UPDATE", "updating %s", $h); + verbose("UPDATE:", "updating %s", $h); my $list_path = "/dns/$zone/listRRs"; my $list_body = encode_www_form_urlencoded({name => $name, type => 'A'}); @@ -5843,7 +5847,7 @@ sub nic_godaddy_update { failed("%s.%s -- Unexpected or empty service response, cannot parse data.", $hostname, $zone); } elsif (defined($response->{code})) { - verbose("VERBOSE:", "%s.%s -- %s - %s.", $hostname, $zone, $response->{code}, $response->{message}); + info("%s.%s -- %s - %s.", $hostname, $zone, $response->{code}, $response->{message}); } if ($ok) { # read data @@ -7287,7 +7291,7 @@ sub nic_porkbun_update { # Default to the subdomain/domain being split at the first dot ($sub_domain, $domain) = split(/\./, $host, 2); } - verbose("VERBOSE:", "subdomain %s, root domain %s", $sub_domain, $domain) if $sub_domain ne ''; + info("subdomain %s, root domain %s", $sub_domain, $domain) if $sub_domain ne ''; foreach my $ipv ('ipv4', 'ipv6') { my $ip = delete $config{$host}{"want$ipv"}; @@ -8061,8 +8065,7 @@ sub nic_infomaniak_update { next; } - verbose("VERBOSE:", "setting IP address to %s for %s", $ip, $h); - info("updating %s", $h); + info("setting IP address to %s for %s", $ip, $h); # No change in IP => nochg # Bad auth => badauth @@ -8091,7 +8094,7 @@ sub nic_infomaniak_update { my $reply; foreach my $url ($url1, $url2) { - verbose("VERBOSE:", "trying update with %s", $url); + info("trying update with %s", $url); $reply = geturl(proxy => opt('proxy'), url => $url); if (!defined($reply) || !$reply) { info("could not update %s using url %s, trying next one", $h, $url); From 5cad38a047accc5facd05704f32d3b37f9532f38 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 14 May 2024 20:39:49 -0400 Subject: [PATCH 2/2] Don't attempt to read file if open fails --- ddclient.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ddclient.in b/ddclient.in index 3edb930..e0e2304 100755 --- a/ddclient.in +++ b/ddclient.in @@ -1664,6 +1664,7 @@ sub _read_config { local *FD; if (!open(FD, "< $file")) { warning("Cannot open file '%s'. (%s)", $file, $!); + goto done; } # If file is owned by our effective uid, ensure that it has no access for group or others. @@ -1809,6 +1810,7 @@ sub _read_config { warning("file ends while expecting a continuation line.") if $continuation; + done: %$globals = %globals; %$config = %config;