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},
53 /* we need these because we link to printing*.o */
55 void become_root(void) {}
56 void unbecome_root(void) {}
59 static void usage(BOOL doexit)
63 printf("Usage: smbcontrol -i -s configfile\n");
64 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
66 printf("<destination> <message-type> <parameters>\n\n");
68 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
69 printf("\t<message-type> is one of:\n");
70 for (i=0; msg_types[i].name; i++)
71 printf("\t\t%s\n", msg_types[i].name);
76 static int pong_count;
77 static BOOL got_level;
79 static BOOL pong_registered = False;
80 static BOOL debuglevel_registered = False;
81 static BOOL poolusage_registered = False;
82 static BOOL profilelevel_registered = False;
86 * Wait for replies for up to @p *max_secs seconds, or until @p
87 * max_replies are received. max_replies may be NULL in which case it
90 * @note This is a pretty lame timeout; all it means is that after
91 * max_secs we won't look for any more messages.
93 static void wait_for_replies(int max_secs, int *max_replies)
95 time_t timeout_end = time(NULL) + max_secs;
97 while ((!max_replies || (*max_replies)-- > 0)
98 && (time(NULL) < timeout_end)) {
104 /****************************************************************************
105 a useful function for testing the message system
106 ****************************************************************************/
107 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
110 printf("PONG from PID %u\n",(unsigned int)src);
113 /****************************************************************************
114 Prints out the current talloc list.
115 ****************************************************************************/
116 void tallocdump_function(int msg_type, pid_t src, void *buf, size_t len)
118 char *info = (char *)buf;
120 printf("Current talloc contexts for process %u\n", (unsigned int)src );
122 printf("None returned\n");
129 /****************************************************************************
130 Prints out the current Debug level returned by MSG_DEBUGLEVEL
131 ****************************************************************************/
132 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
134 const char *levels = (char *)buf;
136 printf("Current debug levels of PID %u are:\n",(unsigned int)src);
137 printf("%s\n", levels);
142 /****************************************************************************
143 Prints out the current Profile level returned by MSG_PROFILELEVEL
144 ****************************************************************************/
145 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
149 memcpy(&level, buf, sizeof(int));
160 s = "count and time";
166 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
168 printf("Profiling not available on PID %u\n",(unsigned int)src);
174 * Handle reply from POOL_USAGE.
176 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
178 printf("Got POOL_USAGE reply from pid %u:\n%.*s",
179 (unsigned int) src_pid, (int) len, (const char *) buf);
184 * Send a message to a named destination
186 * @return False if an error occurred.
188 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
191 /* "smbd" is the only broadcast operation */
192 if (strequal(dest,"smbd")) {
197 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
199 fprintf(stderr,"Failed to open connections database in send_message.\n");
203 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
205 DEBUG(10,("smbcontrol/send_message: broadcast message to "
206 "%d processes\n", n_sent));
210 } else if (strequal(dest,"nmbd")) {
211 pid = pidfile_pid(dest);
213 fprintf(stderr,"Can't find pid for nmbd\n");
216 } else if (strequal(dest,"self")) {
221 fprintf(stderr,"Not a valid pid\n");
226 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
227 return message_send_pid(pid, msg_type, buf, len, duplicates);
230 /****************************************************************************
231 evaluate a message type string
232 ****************************************************************************/
233 static int parse_type(char *mtype)
236 for (i=0;msg_types[i].name;i++) {
237 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
243 static void register_all(void)
245 message_register(MSG_POOL_USAGE, pool_usage_cb);
248 /* This guy is here so we can link printing/notify.c to the smbcontrol
249 binary without having to pull in tons of other crap. */
251 TDB_CONTEXT *conn_tdb_ctx(void)
253 static TDB_CONTEXT *tdb;
258 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
261 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
266 /****************************************************************************
268 ****************************************************************************/
269 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
274 BOOL check_notify_msgs = False;
276 mtype = parse_type(msg_name);
278 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
288 if (!params || !params[0]) {
289 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
293 /* first pass retrieve total lenght */
294 for (p = params; p && *p ; p++)
295 dim += (strnlen(*p, 1024) +1); /* lenght + space */
296 b = buf = malloc(dim);
298 fprintf(stderr, "Out of memory!");
301 /* now build a single string with all parameters */
302 for(p = params; p && *p; p++) {
303 int l = strnlen(*p, 1024);
310 send_message(dest, MSG_DEBUG, buf, dim, False);
318 if (!params || !params[0]) {
319 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
322 if (strequal(params[0], "off")) {
324 } else if (strequal(params[0], "count")) {
326 } else if (strequal(params[0], "on")) {
328 } else if (strequal(params[0], "flush")) {
332 "MSG_PROFILE parameter must be off, count, on, or flush\n");
335 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
338 case MSG_FORCE_ELECTION:
339 if (!strequal(dest, "nmbd")) {
340 fprintf(stderr,"force-election can only be sent to nmbd\n");
343 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
346 case MSG_REQ_PROFILELEVEL:
347 if (!profilelevel_registered) {
348 message_register(MSG_PROFILELEVEL, profilelevel_function);
349 profilelevel_registered = True;
352 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
354 timeout_start = time(NULL);
357 if ((time(NULL) - timeout_start) > MAX_WAIT) {
358 fprintf(stderr,"profilelevel timeout\n");
365 case MSG_REQ_DEBUGLEVEL:
366 if (!debuglevel_registered) {
367 message_register(MSG_DEBUGLEVEL, debuglevel_function);
368 debuglevel_registered = True;
371 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
373 timeout_start = time(NULL);
376 if ((time(NULL) - timeout_start) > MAX_WAIT) {
377 fprintf(stderr,"debuglevel timeout\n");
384 /* Send a notification message to a printer */
386 case MSG_PRINTER_NOTIFY2: {
389 /* Read subcommand */
391 if (!params || !params[0]) {
392 fprintf(stderr, "Must specify subcommand:\n");
393 fprintf(stderr, "\tqueuepause <printername>\n");
394 fprintf(stderr, "\tqueueresume <printername>\n");
395 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
396 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
397 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
398 fprintf(stderr, "\tprinter <printername> <comment|port|driver> <new value>\n");
404 check_notify_msgs = True;
406 /* Pause a print queue */
408 if (strequal(cmd, "queuepause")) {
411 fprintf(stderr, "queuepause command requires a printer name\n");
415 notify_printer_status_byname(params[1], PRINTER_STATUS_PAUSED);
419 /* Resume a print queue */
421 if (strequal(cmd, "queueresume")) {
424 fprintf(stderr, "queueresume command requires a printer name\n");
428 notify_printer_status_byname(params[1], PRINTER_STATUS_OK);
432 /* Pause a print job */
434 if (strequal(cmd, "jobpause")) {
437 if (!params[1] || !params[2]) {
438 fprintf(stderr, "jobpause command requires a printer name and a jobid\n");
442 jobid = atoi(params[2]);
444 notify_job_status_byname(
445 params[1], jobid, JOB_STATUS_PAUSED,
446 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
450 /* Resume a print job */
452 if (strequal(cmd, "jobresume")) {
455 if (!params[1] || !params[2]) {
456 fprintf(stderr, "jobresume command requires a printer name and a jobid\n");
460 jobid = atoi(params[2]);
462 notify_job_status_byname(
463 params[1], jobid, JOB_STATUS_QUEUED,
464 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
468 /* Delete a print job */
470 if (strequal(cmd, "jobdelete")) {
473 if (!params[1] || !params[2]) {
474 fprintf(stderr, "jobdelete command requires a printer name and a jobid\n");
478 jobid = atoi(params[2]);
480 notify_job_status_byname(
481 params[1], jobid, JOB_STATUS_DELETING,
482 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
484 notify_job_status_byname(
485 params[1], jobid, JOB_STATUS_DELETING|
487 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
490 /* printer change notify */
492 if (strequal(cmd, "printer")) {
495 if (!params[1] || !params[2] || !params[3]) {
496 fprintf(stderr, "printer command requires an and attribute name and value!\n");
497 fprintf(stderr, "supported attributes:\n");
498 fprintf(stderr, "\tcomment:\n");
499 fprintf(stderr, "\tport:\n");
500 fprintf(stderr, "\tdriver:\n");
503 if ( strequal(params[2], "comment") )
504 attribute = PRINTER_NOTIFY_COMMENT;
505 else if ( strequal(params[2], "port") )
506 attribute = PRINTER_NOTIFY_PORT_NAME;
507 else if ( strequal(params[2], "driver") )
508 attribute = PRINTER_NOTIFY_DRIVER_NAME;
510 if ( attribute == -1 ) {
511 fprintf(stderr, "bad attribute!\n");
515 notify_printer_byname( params[1], attribute, params[3]);
524 case MSG_SMB_FORCE_TDIS:
525 if (!strequal(dest, "smbd")) {
526 fprintf(stderr,"close-share can only be sent to smbd\n");
529 if (!params || !params[0]) {
530 fprintf(stderr, "close-share needs a share name or '*'\n");
533 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
534 strlen(params[0]) + 1, False);
537 case MSG_SMB_SAM_SYNC:
538 if (!strequal(dest, "smbd")) {
539 fprintf(stderr, "samsync can only be sent to smbd\n");
544 fprintf(stderr, "samsync does not take any parameters\n");
548 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
552 case MSG_SMB_SAM_REPL: {
555 if (!strequal(dest, "smbd")) {
556 fprintf(stderr, "sam repl can only be sent to smbd\n");
560 if (!params || !params[0]) {
561 fprintf(stderr, "SAM_REPL needs a parameter\n");
565 seqnum = atoi(params[0]);
567 retval = send_message(dest, MSG_SMB_SAM_SYNC,
568 (char *)&seqnum, sizeof(uint32), False);
574 if (!pong_registered) {
575 message_register(MSG_PONG, pong_function);
576 pong_registered = True;
578 if (!params || !params[0]) {
579 fprintf(stderr,"MSG_PING needs a parameter\n");
586 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
588 retval = send_message(dest, MSG_PING, NULL, 0, True);
592 wait_for_replies(MAX_WAIT, &n);
594 fprintf(stderr,"PING timeout\n");
598 case MSG_REQ_POOL_USAGE:
599 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
601 wait_for_replies(MAX_WAIT, NULL);
605 case MSG_REQ_DMALLOC_LOG_CHANGED:
606 case MSG_REQ_DMALLOC_MARK:
607 if (!send_message(dest, mtype, NULL, 0, False))
612 if (!send_message(dest, MSG_SHUTDOWN, NULL, 0, False))
615 case MSG_PRINTER_DRVUPGRADE:
616 if (!params || !params[0]) {
617 fprintf(stderr,"drvupgrade needs a parameter\n");
621 if (!send_message(dest, MSG_PRINTER_DRVUPGRADE, params[0], 0, False))
626 /* check if we have any pending print notify messages */
628 if ( check_notify_msgs )
629 print_notify_send_messages(0);
634 int main(int argc, char *argv[])
639 BOOL interactive = False;
641 AllowDebugChange = False;
644 setup_logging(argv[0],True);
646 if (argc < 2) usage(True);
648 while ((opt = getopt(argc, argv,"is:")) != EOF) {
654 pstrcpy(dyn_CONFIGFILE, optarg);
657 printf("Unknown option %c (%d)\n", (char)opt, opt);
662 lp_load(dyn_CONFIGFILE,False,False,False);
664 if (!message_init()) exit(1);
667 argv = &argv[optind];
672 if (argc < 2) usage(True);
673 /* Need to invert sense of return code -- samba
674 * routines mostly return True==1 for success, but
676 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
683 printf("smbcontrol> ");
684 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
686 while ((myargc < 4) &&
687 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
691 if (strequal(myargv[0],"q")) break;
694 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))