1 #!/usr/local/bin/perl -w
6 $sendmailDaemon = "/usr/sbin/sendmail -q30m -bd";
8 ##########################################################################
10 # &get_controlname -- read ControlSocketName option from sendmail.cf
16 # control socket filename, undef if not found
24 open(CF, "</etc/sendmail.cf") or return $cn;
28 if (/^O ControlSocketName\s*=\s*([^#]+)$/o)
32 if (/^O QueueDirectory\s*=\s*([^#]+)$/o)
48 return undef if (not defined $qd);
50 $cn = $qd . "/" . $cn;
55 ##########################################################################
57 # &do_command -- send command to sendmail daemon view control socket
60 # controlsocket -- filename for socket
61 # command -- command to send
64 # reply from sendmail daemon
69 my $controlsocket = shift;
71 my $proto = getprotobyname('ip');
74 socket(SOCK, PF_UNIX, SOCK_STREAM, $proto) or return undef;
76 for ($i = 0; $i < 4; $i++)
78 if (!connect(SOCK, sockaddr_un($controlsocket)))
91 print SOCK "$command\n";
94 return join '', @reply;
97 ##########################################################################
99 # &sendmail_running -- check if sendmail is running via SMTP
105 # 1 if running, undef otherwise
110 my $port = getservbyname("smtp", "tcp") || 25;
111 my $proto = getprotobyname("tcp");
112 my $iaddr = inet_aton("localhost");
113 my $paddr = sockaddr_in($port, $iaddr);
115 socket(SOCK, PF_INET, SOCK_STREAM, $proto) or return undef;
116 if (!connect(SOCK, $paddr))
124 if (/^(\d{3})([ -])/)
148 ##########################################################################
150 # &munge_status -- turn machine readable status into human readable text
153 # raw -- raw results from sendmail daemon STATUS query
156 # human readable text
163 my $daemonStatus = "";
165 if ($raw =~ /^(\d+)\/(\d+)$/mg)
167 $cooked .= "Current number of children: $1";
170 $cooked .= " (maximum $2)";
174 while ($raw =~ /^(\d+) (.*)$/mg)
176 if (not $daemonStatus)
178 $daemonStatus = "(process $1) " . ucfirst($2) . "\n";
182 $cooked .= "Child Process $1 Status: $2\n";
185 return ($daemonStatus, $cooked);
188 ##########################################################################
190 # &start_daemon -- fork off a sendmail daemon
193 # control -- control socket name
196 # Error message or "OK" if successful
208 waitpid $pid, 0 or return "Could not get status of created process: $!\n";
209 $exitstat = $? / 256;
212 return "sendmail daemon startup exited with exit value $exitstat";
217 exec($main::sendmailDaemon);
218 die "Unable to start sendmail daemon: $!.\n";
222 return "Could not create new process: $!\n";
227 ##########################################################################
229 # &stop_daemon -- stop the sendmail daemon using control socket
232 # control -- control socket name
235 # Error message or status message
243 if (not defined $control)
245 return "The control socket is not configured so the daemon can not be stopped.\n";
247 return &do_command($control, "SHUTDOWN");
250 ##########################################################################
252 # &restart_daemon -- restart the sendmail daemon using control socket
255 # control -- control socket name
258 # Error message or status message
266 if (not defined $control)
268 return "The control socket is not configured so the daemon can not be restarted.";
270 return &do_command($control, "RESTART");
273 ##########################################################################
275 # &help -- get help from the daemon using the control socket
278 # control -- control socket name
281 # Error message or status message
289 if (not defined $control)
291 return "The control socket is not configured so the daemon can not be queried for help.";
293 return &do_command($control, "HELP");
297 my $control = &get_controlname;
299 my $daemonStatus = undef;
301 if (not defined $control)
303 die "No control socket available.\n";
305 if (not defined $command)
307 die "Usage: $0 command\n";
309 if ($command eq "status")
311 $status = &do_command($control, "STATUS");
312 if (not defined $status)
314 # Not responding on control channel, query via SMTP
315 if (&sendmail_running)
317 $daemonStatus = "Sendmail is running but not answering status queries.";
321 $daemonStatus = "Sendmail does not appear to be running.";
326 # Munge control channel output
327 ($daemonStatus, $status) = &munge_status($status);
330 elsif (lc($command) eq "shutdown")
332 $status = &stop_daemon($control);
334 elsif (lc($command) eq "restart")
336 $status = &restart_daemon($control);
338 elsif (lc($command) eq "start")
340 $status = &start_daemon($control);
342 elsif (lc($command) eq "help")
344 $status = &help($control);
348 die "Unrecognized command $command\n";
350 if (defined $daemonStatus)
352 print "Daemon Status: $daemonStatus\n";