2 Unix SMB/CIFS implementation.
3 program to send control messages to Samba processes
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) 2001, 2002 by Martin Pool
6 Copyright (C) Simo Sorce 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern BOOL AllowDebugChange;
32 {"force-election", MSG_FORCE_ELECTION},
34 {"profile", MSG_PROFILE},
35 {"profilelevel", MSG_REQ_PROFILELEVEL},
36 {"debuglevel", MSG_REQ_DEBUGLEVEL},
37 {"printnotify", MSG_PRINTER_NOTIFY2 },
38 {"close-share", MSG_SMB_FORCE_TDIS},
39 {"samsync", MSG_SMB_SAM_SYNC},
40 {"samrepl", MSG_SMB_SAM_REPL},
41 {"pool-usage", MSG_REQ_POOL_USAGE },
42 {"dmalloc-mark", MSG_REQ_DMALLOC_MARK },
43 {"dmalloc-log-changed", MSG_REQ_DMALLOC_LOG_CHANGED },
44 {"shutdown", MSG_SHUTDOWN },
45 {"drvupgrade", MSG_PRINTER_DRVUPGRADE},
46 {"tallocdump", MSG_REQ_TALLOC_USAGE},
54 /* we need these because we link to printing*.o */
56 void become_root(void) {}
57 void unbecome_root(void) {}
60 static void usage(BOOL doexit)
64 printf("Usage: smbcontrol -i -s configfile\n");
65 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
67 printf("<destination> <message-type> <parameters>\n\n");
69 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
70 printf("\t<message-type> is one of:\n");
71 for (i=0; msg_types[i].name; i++)
72 printf("\t\t%s\n", msg_types[i].name);
77 static int pong_count;
78 static BOOL got_level;
80 static BOOL pong_registered = False;
81 static BOOL debuglevel_registered = False;
82 static BOOL poolusage_registered = False;
83 static BOOL profilelevel_registered = False;
87 * Wait for replies for up to @p *max_secs seconds, or until @p
88 * max_replies are received. max_replies may be NULL in which case it
91 * @note This is a pretty lame timeout; all it means is that after
92 * max_secs we won't look for any more messages.
94 static void wait_for_replies(int max_secs, int *max_replies)
96 time_t timeout_end = time(NULL) + max_secs;
98 while ((!max_replies || (*max_replies)-- > 0)
99 && (time(NULL) < timeout_end)) {
105 /****************************************************************************
106 a useful function for testing the message system
107 ****************************************************************************/
108 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
111 printf("PONG from PID %u\n",(unsigned int)src);
114 /****************************************************************************
115 Prints out the current talloc list.
116 ****************************************************************************/
117 void tallocdump_function(int msg_type, pid_t src, void *buf, size_t len)
119 char *info = (char *)buf;
121 printf("Current talloc contexts for process %u\n", (unsigned int)src );
123 printf("None returned\n");
130 /****************************************************************************
131 Prints out the current Debug level returned by MSG_DEBUGLEVEL
132 ****************************************************************************/
133 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
135 const char *levels = (char *)buf;
137 printf("Current debug levels of PID %u are:\n",(unsigned int)src);
138 printf("%s\n", levels);
143 /****************************************************************************
144 Prints out the current Profile level returned by MSG_PROFILELEVEL
145 ****************************************************************************/
146 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
150 memcpy(&level, buf, sizeof(int));
161 s = "count and time";
167 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
169 printf("Profiling not available on PID %u\n",(unsigned int)src);
175 * Handle reply from POOL_USAGE.
177 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
179 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
180 (unsigned int) src_pid, (int) len, (const char *) buf);
185 * Send a message to a named destination
187 * @return False if an error occurred.
189 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
192 /* "smbd" is the only broadcast operation */
193 if (strequal(dest,"smbd")) {
198 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
200 fprintf(stderr,"Failed to open connections database in send_message.\n");
204 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
206 DEBUG(10,("smbcontrol/send_message: broadcast message to "
207 "%d processes\n", n_sent));
211 } else if (strequal(dest,"nmbd")) {
212 pid = pidfile_pid(dest);
214 fprintf(stderr,"Can't find pid for nmbd\n");
217 } else if (strequal(dest,"self")) {
222 fprintf(stderr,"Not a valid pid\n");
227 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
228 return message_send_pid(pid, msg_type, buf, len, duplicates);
231 /****************************************************************************
232 evaluate a message type string
233 ****************************************************************************/
234 static int parse_type(char *mtype)
237 for (i=0;msg_types[i].name;i++) {
238 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
244 static void register_all(void)
246 message_register(MSG_POOL_USAGE, pool_usage_cb);
249 /* This guy is here so we can link printing/notify.c to the smbcontrol
250 binary without having to pull in tons of other crap. */
252 TDB_CONTEXT *conn_tdb_ctx(void)
254 static TDB_CONTEXT *tdb;
259 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
262 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
267 /****************************************************************************
269 ****************************************************************************/
270 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
275 BOOL check_notify_msgs = False;
277 mtype = parse_type(msg_name);
279 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
289 if (!params || !params[0]) {
290 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
294 /* first pass retrieve total lenght */
295 for (p = params; p && *p ; p++)
296 dim += (strnlen(*p, 1024) +1); /* lenght + space */
297 b = buf = malloc(dim);
299 fprintf(stderr, "Out of memory!");
302 /* now build a single string with all parameters */
303 for(p = params; p && *p; p++) {
304 int l = strnlen(*p, 1024);
311 send_message(dest, MSG_DEBUG, buf, dim, False);
319 if (!params || !params[0]) {
320 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
323 if (strequal(params[0], "off")) {
325 } else if (strequal(params[0], "count")) {
327 } else if (strequal(params[0], "on")) {
329 } else if (strequal(params[0], "flush")) {
333 "MSG_PROFILE parameter must be off, count, on, or flush\n");
336 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
339 case MSG_FORCE_ELECTION:
340 if (!strequal(dest, "nmbd")) {
341 fprintf(stderr,"force-election can only be sent to nmbd\n");
344 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
347 case MSG_REQ_PROFILELEVEL:
348 if (!profilelevel_registered) {
349 message_register(MSG_PROFILELEVEL, profilelevel_function);
350 profilelevel_registered = True;
353 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
355 timeout_start = time(NULL);
358 if ((time(NULL) - timeout_start) > MAX_WAIT) {
359 fprintf(stderr,"profilelevel timeout\n");
366 case MSG_REQ_TALLOC_USAGE:
367 if (!poolusage_registered) {
368 message_register(MSG_TALLOC_USAGE, tallocdump_function);
369 poolusage_registered = True;
372 retval = send_message(dest, MSG_REQ_TALLOC_USAGE, NULL, 0, True);
374 timeout_start = time(NULL);
377 if ((time(NULL) - timeout_start) > MAX_WAIT) {
378 fprintf(stderr,"tallocdump timeout\n");
385 case MSG_REQ_DEBUGLEVEL:
386 if (!debuglevel_registered) {
387 message_register(MSG_DEBUGLEVEL, debuglevel_function);
388 debuglevel_registered = True;
391 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
393 timeout_start = time(NULL);
396 if ((time(NULL) - timeout_start) > MAX_WAIT) {
397 fprintf(stderr,"debuglevel timeout\n");
404 /* Send a notification message to a printer */
406 case MSG_PRINTER_NOTIFY2: {
409 /* Read subcommand */
411 if (!params || !params[0]) {
412 fprintf(stderr, "Must specify subcommand:\n");
413 fprintf(stderr, "\tqueuepause <printername>\n");
414 fprintf(stderr, "\tqueueresume <printername>\n");
415 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
416 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
417 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
418 fprintf(stderr, "\tprinter <printername> <comment|port|driver> <new value>\n");
424 check_notify_msgs = True;
426 /* Pause a print queue */
428 if (strequal(cmd, "queuepause")) {
431 fprintf(stderr, "queuepause command requires a printer name\n");
435 notify_printer_status_byname(params[1], PRINTER_STATUS_PAUSED);
439 /* Resume a print queue */
441 if (strequal(cmd, "queueresume")) {
444 fprintf(stderr, "queueresume command requires a printer name\n");
448 notify_printer_status_byname(params[1], PRINTER_STATUS_OK);
452 /* Pause a print job */
454 if (strequal(cmd, "jobpause")) {
457 if (!params[1] || !params[2]) {
458 fprintf(stderr, "jobpause command requires a printer name and a jobid\n");
462 jobid = atoi(params[2]);
464 notify_job_status_byname(
465 params[1], jobid, JOB_STATUS_PAUSED,
466 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
470 /* Resume a print job */
472 if (strequal(cmd, "jobresume")) {
475 if (!params[1] || !params[2]) {
476 fprintf(stderr, "jobresume command requires a printer name and a jobid\n");
480 jobid = atoi(params[2]);
482 notify_job_status_byname(
483 params[1], jobid, JOB_STATUS_QUEUED,
484 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
488 /* Delete a print job */
490 if (strequal(cmd, "jobdelete")) {
493 if (!params[1] || !params[2]) {
494 fprintf(stderr, "jobdelete command requires a printer name and a jobid\n");
498 jobid = atoi(params[2]);
500 notify_job_status_byname(
501 params[1], jobid, JOB_STATUS_DELETING,
502 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
504 notify_job_status_byname(
505 params[1], jobid, JOB_STATUS_DELETING|
507 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
510 /* printer change notify */
512 if (strequal(cmd, "printer")) {
515 if (!params[1] || !params[2] || !params[3]) {
516 fprintf(stderr, "printer command requires an and attribute name and value!\n");
517 fprintf(stderr, "supported attributes:\n");
518 fprintf(stderr, "\tcomment:\n");
519 fprintf(stderr, "\tport:\n");
520 fprintf(stderr, "\tdriver:\n");
523 if ( strequal(params[2], "comment") )
524 attribute = PRINTER_NOTIFY_COMMENT;
525 else if ( strequal(params[2], "port") )
526 attribute = PRINTER_NOTIFY_PORT_NAME;
527 else if ( strequal(params[2], "driver") )
528 attribute = PRINTER_NOTIFY_DRIVER_NAME;
530 if ( attribute == -1 ) {
531 fprintf(stderr, "bad attribute!\n");
535 notify_printer_byname( params[1], attribute, params[3]);
544 case MSG_SMB_FORCE_TDIS:
545 if (!strequal(dest, "smbd")) {
546 fprintf(stderr,"close-share can only be sent to smbd\n");
549 if (!params || !params[0]) {
550 fprintf(stderr, "close-share needs a share name or '*'\n");
553 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
554 strlen(params[0]) + 1, False);
557 case MSG_SMB_SAM_SYNC:
558 if (!strequal(dest, "smbd")) {
559 fprintf(stderr, "samsync can only be sent to smbd\n");
564 fprintf(stderr, "samsync does not take any parameters\n");
568 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
572 case MSG_SMB_SAM_REPL: {
575 if (!strequal(dest, "smbd")) {
576 fprintf(stderr, "sam repl can only be sent to smbd\n");
580 if (!params || !params[0]) {
581 fprintf(stderr, "SAM_REPL needs a parameter\n");
585 seqnum = atoi(params[0]);
587 retval = send_message(dest, MSG_SMB_SAM_SYNC,
588 (char *)&seqnum, sizeof(uint32), False);
594 if (!pong_registered) {
595 message_register(MSG_PONG, pong_function);
596 pong_registered = True;
598 if (!params || !params[0]) {
599 fprintf(stderr,"MSG_PING needs a parameter\n");
606 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
608 retval = send_message(dest, MSG_PING, NULL, 0, True);
612 wait_for_replies(MAX_WAIT, &n);
614 fprintf(stderr,"PING timeout\n");
618 case MSG_REQ_POOL_USAGE:
619 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
621 wait_for_replies(MAX_WAIT, NULL);
625 case MSG_REQ_DMALLOC_LOG_CHANGED:
626 case MSG_REQ_DMALLOC_MARK:
627 if (!send_message(dest, mtype, NULL, 0, False))
632 if (!send_message(dest, MSG_SHUTDOWN, NULL, 0, False))
635 case MSG_PRINTER_DRVUPGRADE:
636 if (!send_message(dest, MSG_PRINTER_DRVUPGRADE, params[0], 0, False))
641 /* check if we have any pending print notify messages */
643 if ( check_notify_msgs )
644 print_notify_send_messages();
649 int main(int argc, char *argv[])
654 BOOL interactive = False;
656 AllowDebugChange = False;
659 setup_logging(argv[0],True);
661 if (argc < 2) usage(True);
663 while ((opt = getopt(argc, argv,"is:")) != EOF) {
669 pstrcpy(dyn_CONFIGFILE, optarg);
672 printf("Unknown option %c (%d)\n", (char)opt, opt);
677 lp_load(dyn_CONFIGFILE,False,False,False);
679 if (!message_init()) exit(1);
682 argv = &argv[optind];
687 if (argc < 2) usage(True);
688 /* Need to invert sense of return code -- samba
689 * routines mostly return True==1 for success, but
691 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
698 printf("smbcontrol> ");
699 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
701 while ((myargc < 4) &&
702 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
706 if (strequal(myargv[0],"q")) break;
709 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))