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 #include "module_dummy.h"
30 /* Default timeout value when waiting for replies (in seconds) */
32 #define DEFAULT_TIMEOUT 10
34 static int timeout = DEFAULT_TIMEOUT;
35 static int num_replies; /* Used by message callback fns */
37 /* Send a message to a destination pid. Zero means broadcast smbd. */
39 static BOOL send_message(pid_t pid, int msg_type, void *buf, int len,
50 return message_send_pid(pid, msg_type, buf, len, duplicates);
52 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
53 TDB_DEFAULT, O_RDWR, 0);
55 fprintf(stderr,"Failed to open connections database"
56 ": %s\n", strerror(errno));
60 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
62 DEBUG(10,("smbcontrol/send_message: broadcast message to "
63 "%d processes\n", n_sent));
70 /* Wait for one or more reply messages */
72 static void wait_replies(BOOL multiple_replies)
74 time_t start_time = time(NULL);
76 /* Wait around a bit. This is pretty disgusting - we have to
77 busy-wait here as there is no nicer way to do it. */
81 if (num_replies > 0 && !multiple_replies)
84 } while (timeout - (time(NULL) - start_time) > 0);
87 /* Message handler callback that displays a string on stdout */
89 static void print_string_cb(int msg_type, pid_t pid, void *buf, size_t len)
91 printf("%.*s", (int)len, (const char *)buf);
95 /* Send no message. Useful for testing. */
97 static BOOL do_noop(const pid_t pid, const int argc, char **argv)
100 fprintf(stderr, "Usage: smbcontrol <dest> noop\n");
104 /* Move along, nothing to see here */
109 /* Send a debug string */
111 static BOOL do_debug(const pid_t pid, const int argc, char **argv)
114 fprintf(stderr, "Usage: smbcontrol <dest> debug "
120 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
123 /* Force a browser election */
125 static BOOL do_election(const pid_t pid, const int argc, char **argv)
128 fprintf(stderr, "Usage: smbcontrol <dest> force-election\n");
133 pid, MSG_FORCE_ELECTION, NULL, 0, False);
136 /* Ping a samba daemon process */
138 static void pong_cb(int msg_type, pid_t pid, void *buf, size_t len)
140 printf("PONG from pid %u\n", (unsigned int)pid);
144 static BOOL do_ping(const pid_t pid, const int argc, char **argv)
147 fprintf(stderr, "Usage: smbcontrol <dest> ping\n");
151 /* Send a message and register our interest in a reply */
153 if (!send_message(pid, MSG_PING, NULL, 0, False))
156 message_register(MSG_PONG, pong_cb);
158 wait_replies(pid == 0);
160 /* No replies were received within the timeout period */
162 if (num_replies == 0)
163 printf("No replies received\n");
165 message_deregister(MSG_PONG);
170 /* Set profiling options */
172 static BOOL do_profile(const pid_t pid, const int argc, char **argv)
177 fprintf(stderr, "Usage: smbcontrol <dest> profile "
178 "<off|count|on|flush>\n");
182 if (strcmp(argv[1], "off") == 0) {
184 } else if (strcmp(argv[1], "count") == 0) {
186 } else if (strcmp(argv[1], "on") == 0) {
188 } else if (strcmp(argv[1], "flush") == 0) {
191 fprintf(stderr, "Unknown profile command '%s'\n", argv[1]);
195 return send_message(pid, MSG_PROFILE, &v, sizeof(int), False);
198 /* Return the profiling level */
200 static void profilelevel_cb(int msg_type, pid_t pid, void *buf, size_t len)
207 if (len != sizeof(int)) {
208 fprintf(stderr, "invalid message length %d returned\n", len);
212 memcpy(&level, buf, sizeof(int));
225 s = "count and time";
232 printf("Profiling %s on pid %u\n",s,(unsigned int)pid);
235 static void profilelevel_rqst(int msg_type, pid_t pid, void *buf, size_t len)
239 /* Send back a dummy reply */
241 send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
244 static BOOL do_profilelevel(const pid_t pid, const int argc, char **argv)
247 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
251 /* Send a message and register our interest in a reply */
253 if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
256 message_register(MSG_PROFILELEVEL, profilelevel_cb);
257 message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
259 wait_replies(pid == 0);
261 /* No replies were received within the timeout period */
263 if (num_replies == 0)
264 printf("No replies received\n");
266 message_deregister(MSG_PROFILE);
271 /* Display debug level settings */
273 static BOOL do_debuglevel(const pid_t pid, const int argc, char **argv)
276 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
280 /* Send a message and register our interest in a reply */
282 if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
285 message_register(MSG_DEBUGLEVEL, print_string_cb);
287 wait_replies(pid == 0);
289 /* No replies were received within the timeout period */
291 if (num_replies == 0)
292 printf("No replies received\n");
294 message_deregister(MSG_DEBUGLEVEL);
299 /* Send a print notify message */
301 static BOOL do_printnotify(const pid_t pid, const int argc, char **argv)
305 /* Check for subcommand */
308 fprintf(stderr, "Must specify subcommand:\n");
309 fprintf(stderr, "\tqueuepause <printername>\n");
310 fprintf(stderr, "\tqueueresume <printername>\n");
311 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
312 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
313 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
314 fprintf(stderr, "\tprinter <printername> <comment|port|"
315 "driver> <value>\n");
322 if (strcmp(cmd, "queuepause") == 0) {
325 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
326 " queuepause <printername>\n");
330 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
334 } else if (strcmp(cmd, "queueresume") == 0) {
337 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
338 " queuereume <printername>\n");
342 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
346 } else if (strcmp(cmd, "jobpause") == 0) {
350 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
351 " jobpause <printername> <unix-jobid>\n");
355 jobid = atoi(argv[3]);
357 notify_job_status_byname(
358 argv[2], jobid, JOB_STATUS_PAUSED,
359 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
363 } else if (strcmp(cmd, "jobresume") == 0) {
367 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
368 " jobpause <printername> <unix-jobid>\n");
372 jobid = atoi(argv[3]);
374 notify_job_status_byname(
375 argv[2], jobid, JOB_STATUS_QUEUED,
376 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
380 } else if (strcmp(cmd, "jobdelete") == 0) {
384 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
385 " jobpause <printername> <unix-jobid>\n");
389 jobid = atoi(argv[3]);
391 notify_job_status_byname(
392 argv[2], jobid, JOB_STATUS_DELETING,
393 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
395 notify_job_status_byname(
396 argv[2], jobid, JOB_STATUS_DELETING|
398 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
402 } else if (strcmp(cmd, "printer") == 0) {
406 fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
407 "printer <printername> <comment|port|driver> "
412 if (strcmp(argv[3], "comment") == 0) {
413 attribute = PRINTER_NOTIFY_COMMENT;
414 } else if (strcmp(argv[3], "port") == 0) {
415 attribute = PRINTER_NOTIFY_PORT_NAME;
416 } else if (strcmp(argv[3], "driver") == 0) {
417 attribute = PRINTER_NOTIFY_DRIVER_NAME;
419 fprintf(stderr, "Invalid printer command '%s'\n",
424 notify_printer_byname(argv[2], attribute, argv[4]);
429 fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
433 print_notify_send_messages(0);
439 static BOOL do_closeshare(const pid_t pid, const int argc, char **argv)
442 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
448 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
451 /* Force a SAM synchronisation */
453 static BOOL do_samsync(const pid_t pid, const int argc, char **argv)
456 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
461 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
464 /* Force a SAM replication */
466 static BOOL do_samrepl(const pid_t pid, const int argc, char **argv)
469 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
474 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
477 /* Display talloc pool usage */
479 static BOOL do_poolusage(const pid_t pid, const int argc, char **argv)
482 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
486 /* Send a message and register our interest in a reply */
488 if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
491 message_register(MSG_POOL_USAGE, print_string_cb);
493 wait_replies(pid == 0);
495 /* No replies were received within the timeout period */
497 if (num_replies == 0)
498 printf("No replies received\n");
500 message_deregister(MSG_POOL_USAGE);
505 /* Perform a dmalloc mark */
507 static BOOL do_dmalloc_mark(const pid_t pid, const int argc, char **argv)
510 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
515 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
518 /* Perform a dmalloc changed */
520 static BOOL do_dmalloc_changed(const pid_t pid, const int argc,
524 fprintf(stderr, "Usage: smbcontrol <dest> "
525 "dmalloc-log-changed\n");
530 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
533 /* Shutdown a server process */
535 static BOOL do_shutdown(const pid_t pid, const int argc, char **argv)
538 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
542 return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
545 /* Notify a driver upgrade */
547 static BOOL do_drvupgrade(const pid_t pid, const int argc, char **argv)
550 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
556 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
559 /* A list of message type supported */
561 static const struct {
562 const char *name; /* Option name */
563 BOOL (*fn)(const pid_t pid, const int argc, char **argv);
564 const char *help; /* Short help text */
566 { "debug", do_debug, "Set debuglevel" },
567 { "force-election", do_election,
568 "Force a browse election" },
569 { "ping", do_ping, "Elicit a response" },
570 { "profile", do_profile, "" },
571 { "profilelevel", do_profilelevel, "" },
572 { "debuglevel", do_debuglevel, "Display current debuglevels" },
573 { "printnotify", do_printnotify, "Send a print notify message" },
574 { "close-share", do_closeshare, "Forcibly disconnect a share" },
575 { "samsync", do_samsync, "Initiate SAM synchronisation" },
576 { "samrepl", do_samrepl, "Initiate SAM replication" },
577 { "pool-usage", do_poolusage, "Display talloc memory usage" },
578 { "dmalloc-mark", do_dmalloc_mark, "" },
579 { "dmalloc-log-changed", do_dmalloc_changed, "" },
580 { "shutdown", do_shutdown, "Shut down daemon" },
581 { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
582 { "noop", do_noop, "Do nothing" },
586 /* Yuck - we need these because we link to printing*.o even though
589 void become_root(void) {}
590 void unbecome_root(void) {}
592 /* Display usage information */
594 static void usage(poptContext *pc)
598 poptPrintHelp(*pc, stderr, 0);
600 fprintf(stderr, "\n");
601 fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
604 fprintf(stderr, "\n");
605 fprintf(stderr, "<message-type> is one of:\n");
607 for (i = 0; msg_types[i].name; i++)
608 fprintf(stderr, "\t%-30s%s\n", msg_types[i].name,
611 fprintf(stderr, "\n");
616 /* Return the pid number for a string destination */
618 static pid_t parse_dest(char *dest)
622 /* Zero is a special return value for broadcast smbd */
624 if (strequal(dest, "smbd"))
627 /* Try self - useful for testing */
629 if (strequal(dest, "self"))
632 /* Check for numeric pid number */
634 if ((pid = atoi(dest)) != 0)
637 /* Look up other destinations in pidfile directory */
639 if ((pid = pidfile_pid(dest)) != 0)
642 fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
647 /* Execute smbcontrol command */
649 static BOOL do_command(int argc, char **argv)
651 char *dest = argv[0], *command = argv[1];
655 /* Check destination */
657 if ((pid = parse_dest(dest)) == -1)
662 for (i = 0; msg_types[i].name; i++) {
663 if (strequal(command, msg_types[i].name))
664 return msg_types[i].fn(pid, argc - 1, argv + 1);
667 fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
674 int main(int argc, char **argv)
679 static struct poptOption wbinfo_options[] = {
680 { "timeout", 't', POPT_ARG_INT, &timeout, 't',
681 "Set timeout value in seconds", "TIMEOUT" },
683 { "configfile", 's', POPT_ARG_STRING, NULL, 's',
684 "Use alternative configuration file", "CONFIGFILE" },
689 struct poptOption options[] = {
690 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0,
698 setup_logging(argv[0],True);
700 /* Parse command line arguments using popt */
703 "smbcontrol", argc, (const char **)argv, options, 0);
705 poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
711 while ((opt = poptGetNextOpt(pc)) != -1) {
713 case 't': /* --timeout */
716 case 's': /* --configfile */
717 pstrcpy(dyn_CONFIGFILE, optarg);
721 fprintf(stderr, "Invalid option\n");
722 poptPrintHelp(pc, stderr, 0);
727 /* We should now have the remaining command line arguments in
728 argv. The argc parameter should have been decremented to the
729 correct value in the above switch statement. */
731 argv = (char **)poptGetArgs(pc);
732 argc--; /* Don't forget about argv[0] */
737 lp_load(dyn_CONFIGFILE,False,False,False);
739 /* Need to invert sense of return code -- samba
740 * routines mostly return True==1 for success, but
743 return !do_command(argc, argv);