2 * Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
13 SM_RCSID("@(#)$Id: control.c,v 8.118.4.3 2002/11/14 00:15:56 ca Exp $")
17 /* values for cmd_code */
18 #define CMDERROR 0 /* bad command */
19 #define CMDRESTART 1 /* restart daemon */
20 #define CMDSHUTDOWN 2 /* end daemon */
21 #define CMDHELP 3 /* help */
22 #define CMDSTATUS 4 /* daemon status */
23 #define CMDMEMDUMP 5 /* dump memory, to find memory leaks */
24 #if _FFR_CONTROL_MSTAT
25 # define CMDMSTAT 6 /* daemon status, more info, tagged data */
26 #endif /* _FFR_CONTROL_MSTAT */
30 char *cmd_name; /* command name */
31 int cmd_code; /* internal code, see below */
34 static struct cmd CmdTab[] =
37 { "restart", CMDRESTART },
38 { "shutdown", CMDSHUTDOWN },
39 { "status", CMDSTATUS },
40 { "memdump", CMDMEMDUMP },
41 #if _FFR_CONTROL_MSTAT
42 { "mstat", CMDMSTAT },
43 #endif /* _FFR_CONTROL_MSTAT */
49 int ControlSocket = -1;
52 ** OPENCONTROLSOCKET -- create/open the daemon control named socket
54 ** Creates and opens a named socket for external control over
55 ** the sendmail daemon.
61 ** 0 if successful, -1 otherwise
70 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;
71 struct sockaddr_un controladdr;
73 if (ControlSocketName == NULL || *ControlSocketName == '\0')
76 if (strlen(ControlSocketName) >= sizeof controladdr.sun_path)
82 rval = safefile(ControlSocketName, RunAsUid, RunAsGid, RunAsUserName,
83 sff, S_IRUSR|S_IWUSR, NULL);
85 /* if not safe, don't create */
92 ControlSocket = socket(AF_UNIX, SOCK_STREAM, 0);
93 if (ControlSocket < 0)
95 if (SM_FD_SETSIZE > 0 && ControlSocket >= SM_FD_SETSIZE)
102 (void) unlink(ControlSocketName);
103 memset(&controladdr, '\0', sizeof controladdr);
104 controladdr.sun_family = AF_UNIX;
105 (void) sm_strlcpy(controladdr.sun_path, ControlSocketName,
106 sizeof controladdr.sun_path);
108 if (bind(ControlSocket, (struct sockaddr *) &controladdr,
109 sizeof controladdr) < 0)
123 else if (TrustedUid != 0)
127 chown(ControlSocketName, u, -1) < 0)
130 sm_syslog(LOG_ALERT, NOQID,
131 "ownership change on %s to uid %d failed: %s",
132 ControlSocketName, (int) u,
133 sm_errstring(save_errno));
134 message("050 ownership change on %s to uid %d failed: %s",
135 ControlSocketName, (int) u,
136 sm_errstring(save_errno));
137 closecontrolsocket(true);
143 if (chmod(ControlSocketName, S_IRUSR|S_IWUSR) < 0)
146 closecontrolsocket(true);
151 if (listen(ControlSocket, 8) < 0)
154 closecontrolsocket(true);
158 # endif /* NETUNIX */
162 ** CLOSECONTROLSOCKET -- close the daemon control named socket
164 ** Close a named socket.
167 ** fullclose -- if set, close the socket and remove it;
168 ** otherwise, just remove it
175 closecontrolsocket(fullclose)
179 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;
181 if (ControlSocket >= 0)
187 (void) close(ControlSocket);
191 rval = safefile(ControlSocketName, RunAsUid, RunAsGid,
192 RunAsUserName, sff, S_IRUSR|S_IWUSR, NULL);
194 /* if not safe, don't unlink */
198 if (unlink(ControlSocketName) < 0)
200 sm_syslog(LOG_WARNING, NOQID,
201 "Could not remove control socket: %s",
202 sm_errstring(errno));
206 # endif /* NETUNIX */
210 ** CLRCONTROL -- reset the control connection
219 ** releases any resources used by the control interface.
226 if (ControlSocket >= 0)
227 (void) close(ControlSocket);
229 # endif /* NETUNIX */
232 ** CONTROL_COMMAND -- read and process command from named socket
234 ** Read and process the command from the opened socket.
235 ** Exits when done since it is running in a forked child.
238 ** sock -- the opened socket from getrequests()
239 ** e -- the current envelope
245 static jmp_buf CtxControlTimeout;
249 controltimeout(timeout)
253 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
254 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
259 longjmp(CtxControlTimeout, 1);
263 control_command(sock, e)
267 volatile int exitstat = EX_OK;
275 char cmdbuf[MAXLINE];
278 sm_setproctitle(false, e, "control cmd read");
280 if (TimeOuts.to_control > 0)
282 /* handle possible input timeout */
283 if (setjmp(CtxControlTimeout) != 0)
286 sm_syslog(LOG_NOTICE, e->e_id,
287 "timeout waiting for input during control command");
290 ev = sm_setevent(TimeOuts.to_control, controltimeout,
291 TimeOuts.to_control);
294 s = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &sock,
298 int save_errno = errno;
304 (void) sm_io_setvbuf(s, SM_TIME_DEFAULT, NULL,
305 SM_IO_NBF, SM_IO_BUFSIZ);
307 if (sm_io_fgets(s, SM_TIME_DEFAULT, inp, sizeof inp) == NULL)
309 (void) sm_io_close(s, SM_TIME_DEFAULT);
312 (void) sm_io_flush(s, SM_TIME_DEFAULT);
314 /* clean up end of line */
317 sm_setproctitle(false, e, "control: %s", inp);
319 /* break off command */
320 for (p = inp; isascii(*p) && isspace(*p); p++)
324 !(isascii(*p) && isspace(*p)) &&
325 cmd < &cmdbuf[sizeof cmdbuf - 2])
329 /* throw away leading whitespace */
330 while (isascii(*p) && isspace(*p))
334 for (c = CmdTab; c->cmd_name != NULL; c++)
336 if (sm_strcasecmp(c->cmd_name, cmdbuf) == 0)
342 case CMDHELP: /* get help */
343 traffic = TrafficLogFile;
344 TrafficLogFile = NULL;
348 TrafficLogFile = traffic;
352 case CMDRESTART: /* restart the daemon */
353 (void) sm_io_fprintf(s, SM_TIME_DEFAULT, "OK\r\n");
354 exitstat = EX_RESTART;
357 case CMDSHUTDOWN: /* kill the daemon */
358 (void) sm_io_fprintf(s, SM_TIME_DEFAULT, "OK\r\n");
359 exitstat = EX_SHUTDOWN;
362 case CMDSTATUS: /* daemon status */
369 /* XXX need to deal with different partitions */
371 if (!ISVALIDQGRP(qgrp))
373 free = freediskspace(Queue[qgrp]->qg_qdir, &bsize);
376 ** Prevent overflow and don't lose
377 ** precision (if bsize == 512)
381 free = (long)((double) free *
382 ((double) bsize / 1024));
384 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
386 CurChildren, MaxChildren,
389 proc_list_display(s, "");
392 # if _FFR_CONTROL_MSTAT
393 case CMDMSTAT: /* daemon status, extended, tagged format */
395 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
396 "C:%d\r\nM:%d\r\nL:%d\r\n",
397 CurChildren, MaxChildren,
400 disk_status(s, "D:");
401 proc_list_display(s, "P:");
403 # endif /* _FFR_CONTROL_MSTAT */
405 case CMDMEMDUMP: /* daemon memory dump, to find memory leaks */
407 /* dump the heap, if we are checking for memory leaks */
408 if (sm_debug_active(&SmHeapCheck, 2))
410 sm_heap_report(s, sm_debug_level(&SmHeapCheck) - 1);
414 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
415 "Memory dump unavailable.\r\n");
416 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
417 "To fix, run sendmail with -dsm_check_heap.4\r\n");
419 # else /* SM_HEAP_CHECK */
420 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
421 "Memory dump unavailable.\r\n");
422 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
423 "To fix, rebuild with -DSM_HEAP_CHECK\r\n");
424 # endif /* SM_HEAP_CHECK */
427 case CMDERROR: /* unknown command */
428 (void) sm_io_fprintf(s, SM_TIME_DEFAULT,
429 "Bad command (%s)\r\n", cmdbuf);
432 (void) sm_io_close(s, SM_TIME_DEFAULT);