2 Unix SMB/CIFS implementation.
4 Send messages to other Samba daemons
6 Copyright (C) Tim Potter 2003
7 Copyright (C) Andrew Tridgell 1994-1998
8 Copyright (C) Martin Pool 2001-2002
9 Copyright (C) Simo Sorce 2002
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* Default timeout value when waiting for replies (in seconds) */
30 #define DEFAULT_TIMEOUT 10
32 static int timeout = DEFAULT_TIMEOUT;
33 static int num_replies; /* Used by message callback fns */
35 /* Send a message to a destination pid. Zero means broadcast smbd. */
37 static BOOL send_message(pid_t pid, int msg_type, const void *buf, int len,
48 return message_send_pid(pid, msg_type, buf, len, duplicates);
50 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
51 TDB_DEFAULT, O_RDWR, 0);
53 fprintf(stderr,"Failed to open connections database"
54 ": %s\n", strerror(errno));
58 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
60 DEBUG(10,("smbcontrol/send_message: broadcast message to "
61 "%d processes\n", n_sent));
68 /* Wait for one or more reply messages */
70 static void wait_replies(BOOL multiple_replies)
72 time_t start_time = time(NULL);
74 /* Wait around a bit. This is pretty disgusting - we have to
75 busy-wait here as there is no nicer way to do it. */
79 if (num_replies > 0 && !multiple_replies)
82 } while (timeout - (time(NULL) - start_time) > 0);
85 /* Message handler callback that displays the PID and a string on stdout */
87 static void print_pid_string_cb(int msg_type, pid_t pid, void *buf, size_t len)
89 printf("PID %u: %.*s", (unsigned int)pid, (int)len, (const char *)buf);
93 /* Message handler callback that displays a string on stdout */
95 static void print_string_cb(int msg_type, pid_t pid, void *buf, size_t len)
97 printf("%.*s", (int)len, (const char *)buf);
101 /* Send no message. Useful for testing. */
103 static BOOL do_noop(const pid_t pid, const int argc, const char **argv)
106 fprintf(stderr, "Usage: smbcontrol <dest> noop\n");
110 /* Move along, nothing to see here */
115 /* Send a debug string */
117 static BOOL do_debug(const pid_t pid, const int argc, const char **argv)
120 fprintf(stderr, "Usage: smbcontrol <dest> debug "
126 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
129 /* Force a browser election */
131 static BOOL do_election(const pid_t pid, const int argc, const char **argv)
134 fprintf(stderr, "Usage: smbcontrol <dest> force-election\n");
139 pid, MSG_FORCE_ELECTION, NULL, 0, False);
142 /* Ping a samba daemon process */
144 static void pong_cb(int msg_type, pid_t pid, void *buf, size_t len)
146 printf("PONG from pid %u\n", (unsigned int)pid);
150 static BOOL do_ping(const pid_t pid, const int argc, const char **argv)
153 fprintf(stderr, "Usage: smbcontrol <dest> ping\n");
157 /* Send a message and register our interest in a reply */
159 if (!send_message(pid, MSG_PING, NULL, 0, False))
162 message_register(MSG_PONG, pong_cb);
164 wait_replies(pid == 0);
166 /* No replies were received within the timeout period */
168 if (num_replies == 0)
169 printf("No replies received\n");
171 message_deregister(MSG_PONG);
176 /* Set profiling options */
178 static BOOL do_profile(const pid_t pid, const int argc, const char **argv)
183 fprintf(stderr, "Usage: smbcontrol <dest> profile "
184 "<off|count|on|flush>\n");
188 if (strcmp(argv[1], "off") == 0) {
190 } else if (strcmp(argv[1], "count") == 0) {
192 } else if (strcmp(argv[1], "on") == 0) {
194 } else if (strcmp(argv[1], "flush") == 0) {
197 fprintf(stderr, "Unknown profile command '%s'\n", argv[1]);
201 return send_message(pid, MSG_PROFILE, &v, sizeof(int), False);
204 /* Return the profiling level */
206 static void profilelevel_cb(int msg_type, pid_t pid, void *buf, size_t len)
213 if (len != sizeof(int)) {
214 fprintf(stderr, "invalid message length %ld returned\n",
219 memcpy(&level, buf, sizeof(int));
232 s = "count and time";
239 printf("Profiling %s on pid %u\n",s,(unsigned int)pid);
242 static void profilelevel_rqst(int msg_type, pid_t pid, void *buf, size_t len)
246 /* Send back a dummy reply */
248 send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
251 static BOOL do_profilelevel(const pid_t pid, const int argc, const char **argv)
254 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
258 /* Send a message and register our interest in a reply */
260 if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
263 message_register(MSG_PROFILELEVEL, profilelevel_cb);
264 message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
266 wait_replies(pid == 0);
268 /* No replies were received within the timeout period */
270 if (num_replies == 0)
271 printf("No replies received\n");
273 message_deregister(MSG_PROFILE);
278 /* Display debug level settings */
280 static BOOL do_debuglevel(const pid_t pid, const int argc, const char **argv)
283 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
287 /* Send a message and register our interest in a reply */
289 if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
292 message_register(MSG_DEBUGLEVEL, print_pid_string_cb);
294 wait_replies(pid == 0);
296 /* No replies were received within the timeout period */
298 if (num_replies == 0)
299 printf("No replies received\n");
301 message_deregister(MSG_DEBUGLEVEL);
306 /* Send a print notify message */
308 static BOOL do_printnotify(const pid_t pid, const int argc, const char **argv)
312 /* Check for subcommand */
315 fprintf(stderr, "Must specify subcommand:\n");
316 fprintf(stderr, "\tqueuepause <printername>\n");
317 fprintf(stderr, "\tqueueresume <printername>\n");
318 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
319 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
320 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
321 fprintf(stderr, "\tprinter <printername> <comment|port|"
322 "driver> <value>\n");
329 if (strcmp(cmd, "queuepause") == 0) {
332 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
333 " queuepause <printername>\n");
337 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
341 } else if (strcmp(cmd, "queueresume") == 0) {
344 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
345 " queuereume <printername>\n");
349 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
353 } else if (strcmp(cmd, "jobpause") == 0) {
357 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
358 " jobpause <printername> <unix-jobid>\n");
362 jobid = atoi(argv[3]);
364 notify_job_status_byname(
365 argv[2], jobid, JOB_STATUS_PAUSED,
366 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
370 } else if (strcmp(cmd, "jobresume") == 0) {
374 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
375 " jobpause <printername> <unix-jobid>\n");
379 jobid = atoi(argv[3]);
381 notify_job_status_byname(
382 argv[2], jobid, JOB_STATUS_QUEUED,
383 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
387 } else if (strcmp(cmd, "jobdelete") == 0) {
391 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
392 " jobpause <printername> <unix-jobid>\n");
396 jobid = atoi(argv[3]);
398 notify_job_status_byname(
399 argv[2], jobid, JOB_STATUS_DELETING,
400 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
402 notify_job_status_byname(
403 argv[2], jobid, JOB_STATUS_DELETING|
405 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
409 } else if (strcmp(cmd, "printer") == 0) {
413 fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
414 "printer <printername> <comment|port|driver> "
419 if (strcmp(argv[3], "comment") == 0) {
420 attribute = PRINTER_NOTIFY_COMMENT;
421 } else if (strcmp(argv[3], "port") == 0) {
422 attribute = PRINTER_NOTIFY_PORT_NAME;
423 } else if (strcmp(argv[3], "driver") == 0) {
424 attribute = PRINTER_NOTIFY_DRIVER_NAME;
426 fprintf(stderr, "Invalid printer command '%s'\n",
431 notify_printer_byname(argv[2], attribute,
432 CONST_DISCARD(char *, argv[4]));
437 fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
441 print_notify_send_messages(0);
447 static BOOL do_closeshare(const pid_t pid, const int argc, const char **argv)
450 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
456 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
459 /* Force a SAM synchronisation */
461 static BOOL do_samsync(const pid_t pid, const int argc, const char **argv)
464 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
469 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
472 /* Force a SAM replication */
474 static BOOL do_samrepl(const pid_t pid, const int argc, const char **argv)
477 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
482 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
485 /* Display talloc pool usage */
487 static BOOL do_poolusage(const pid_t pid, const int argc, const char **argv)
490 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
494 /* Send a message and register our interest in a reply */
496 if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
499 message_register(MSG_POOL_USAGE, print_string_cb);
501 wait_replies(pid == 0);
503 /* No replies were received within the timeout period */
505 if (num_replies == 0)
506 printf("No replies received\n");
508 message_deregister(MSG_POOL_USAGE);
513 /* Perform a dmalloc mark */
515 static BOOL do_dmalloc_mark(const pid_t pid, const int argc, const char **argv)
518 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
523 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
526 /* Perform a dmalloc changed */
528 static BOOL do_dmalloc_changed(const pid_t pid, const int argc, const char **argv)
531 fprintf(stderr, "Usage: smbcontrol <dest> "
532 "dmalloc-log-changed\n");
537 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
540 /* Shutdown a server process */
542 static BOOL do_shutdown(const pid_t pid, const int argc, const char **argv)
545 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
549 return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
552 /* Notify a driver upgrade */
554 static BOOL do_drvupgrade(const pid_t pid, const int argc, const char **argv)
557 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
563 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
566 static BOOL do_reload_config(const pid_t pid, const int argc, const char **argv)
569 fprintf(stderr, "Usage: smbcontrol <dest> reload-config\n");
573 return send_message(pid, MSG_SMB_CONF_UPDATED, NULL, 0, False);
576 /* A list of message type supported */
578 static const struct {
579 const char *name; /* Option name */
580 BOOL (*fn)(const pid_t pid, const int argc, const char **argv);
581 const char *help; /* Short help text */
583 { "debug", do_debug, "Set debuglevel" },
584 { "force-election", do_election,
585 "Force a browse election" },
586 { "ping", do_ping, "Elicit a response" },
587 { "profile", do_profile, "" },
588 { "profilelevel", do_profilelevel, "" },
589 { "debuglevel", do_debuglevel, "Display current debuglevels" },
590 { "printnotify", do_printnotify, "Send a print notify message" },
591 { "close-share", do_closeshare, "Forcibly disconnect a share" },
592 { "samsync", do_samsync, "Initiate SAM synchronisation" },
593 { "samrepl", do_samrepl, "Initiate SAM replication" },
594 { "pool-usage", do_poolusage, "Display talloc memory usage" },
595 { "dmalloc-mark", do_dmalloc_mark, "" },
596 { "dmalloc-log-changed", do_dmalloc_changed, "" },
597 { "shutdown", do_shutdown, "Shut down daemon" },
598 { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
599 { "reload-config", do_reload_config, "Force smbd or winbindd to reload config file"},
600 { "noop", do_noop, "Do nothing" },
604 /* Display usage information */
606 static void usage(poptContext *pc)
610 poptPrintHelp(*pc, stderr, 0);
612 fprintf(stderr, "\n");
613 fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
616 fprintf(stderr, "\n");
617 fprintf(stderr, "<message-type> is one of:\n");
619 for (i = 0; msg_types[i].name; i++)
620 fprintf(stderr, "\t%-30s%s\n", msg_types[i].name,
623 fprintf(stderr, "\n");
628 /* Return the pid number for a string destination */
630 static pid_t parse_dest(const char *dest)
634 /* Zero is a special return value for broadcast smbd */
636 if (strequal(dest, "smbd"))
639 /* Try self - useful for testing */
641 if (strequal(dest, "self"))
644 /* Check for numeric pid number */
646 if ((pid = atoi(dest)) != 0)
649 /* Look up other destinations in pidfile directory */
651 if ((pid = pidfile_pid(dest)) != 0)
654 fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
659 /* Execute smbcontrol command */
661 static BOOL do_command(int argc, const char **argv)
663 const char *dest = argv[0], *command = argv[1];
667 /* Check destination */
669 if ((pid = parse_dest(dest)) == -1)
674 for (i = 0; msg_types[i].name; i++) {
675 if (strequal(command, msg_types[i].name))
676 return msg_types[i].fn(pid, argc - 1, argv + 1);
679 fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
686 int main(int argc, const char **argv)
691 static struct poptOption wbinfo_options[] = {
692 { "timeout", 't', POPT_ARG_INT, &timeout, 't',
693 "Set timeout value in seconds", "TIMEOUT" },
695 { "configfile", 's', POPT_ARG_STRING, NULL, 's',
696 "Use alternative configuration file", "CONFIGFILE" },
701 struct poptOption options[] = {
702 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0,
710 setup_logging(argv[0],True);
712 /* Parse command line arguments using popt */
715 "smbcontrol", argc, (const char **)argv, options, 0);
717 poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
723 while ((opt = poptGetNextOpt(pc)) != -1) {
725 case 't': /* --timeout */
728 case 's': /* --configfile */
729 pstrcpy(dyn_CONFIGFILE, poptGetOptArg(pc));
733 fprintf(stderr, "Invalid option\n");
734 poptPrintHelp(pc, stderr, 0);
739 /* We should now have the remaining command line arguments in
740 argv. The argc parameter should have been decremented to the
741 correct value in the above switch statement. */
743 argv = (const char **)poptGetArgs(pc);
744 argc--; /* Don't forget about argv[0] */
749 lp_load(dyn_CONFIGFILE,False,False,False);
751 /* Need to invert sense of return code -- samba
752 * routines mostly return True==1 for success, but
755 return !do_command(argc, argv);