r85: Update the winbind interface version, as I just extended the struct.
[Samba/gebeck_regimport.git] / source3 / utils / smbcontrol.c
blob8a27684a4d377142cbef4a8d1b670dda42846148
1 /*
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.
26 #include "includes.h"
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,
38 BOOL duplicates)
40 TDB_CONTEXT *tdb;
41 BOOL ret;
42 int n_sent = 0;
44 if (!message_init())
45 return False;
47 if (pid != 0)
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);
52 if (!tdb) {
53 fprintf(stderr,"Failed to open connections database"
54 ": %s\n", strerror(errno));
55 return False;
58 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
59 &n_sent);
60 DEBUG(10,("smbcontrol/send_message: broadcast message to "
61 "%d processes\n", n_sent));
63 tdb_close(tdb);
65 return ret;
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. */
77 do {
78 message_dispatch();
79 if (num_replies > 0 && !multiple_replies)
80 break;
81 sleep(1);
82 } while (timeout - (time(NULL) - start_time) > 0);
85 /* Message handler callback that displays a string on stdout */
87 static void print_string_cb(int msg_type, pid_t pid, void *buf, size_t len)
89 printf("%.*s", (int)len, (const char *)buf);
90 num_replies++;
93 /* Send no message. Useful for testing. */
95 static BOOL do_noop(const pid_t pid, const int argc, const char **argv)
97 if (argc != 1) {
98 fprintf(stderr, "Usage: smbcontrol <dest> noop\n");
99 return False;
102 /* Move along, nothing to see here */
104 return True;
107 /* Send a debug string */
109 static BOOL do_debug(const pid_t pid, const int argc, const char **argv)
111 if (argc != 2) {
112 fprintf(stderr, "Usage: smbcontrol <dest> debug "
113 "<debug-string>\n");
114 return False;
117 return send_message(
118 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
121 /* Force a browser election */
123 static BOOL do_election(const pid_t pid, const int argc, const char **argv)
125 if (argc != 1) {
126 fprintf(stderr, "Usage: smbcontrol <dest> force-election\n");
127 return False;
130 return send_message(
131 pid, MSG_FORCE_ELECTION, NULL, 0, False);
134 /* Ping a samba daemon process */
136 static void pong_cb(int msg_type, pid_t pid, void *buf, size_t len)
138 printf("PONG from pid %u\n", (unsigned int)pid);
139 num_replies++;
142 static BOOL do_ping(const pid_t pid, const int argc, const char **argv)
144 if (argc != 1) {
145 fprintf(stderr, "Usage: smbcontrol <dest> ping\n");
146 return False;
149 /* Send a message and register our interest in a reply */
151 if (!send_message(pid, MSG_PING, NULL, 0, False))
152 return False;
154 message_register(MSG_PONG, pong_cb);
156 wait_replies(pid == 0);
158 /* No replies were received within the timeout period */
160 if (num_replies == 0)
161 printf("No replies received\n");
163 message_deregister(MSG_PONG);
165 return num_replies;
168 /* Set profiling options */
170 static BOOL do_profile(const pid_t pid, const int argc, const char **argv)
172 int v;
174 if (argc != 2) {
175 fprintf(stderr, "Usage: smbcontrol <dest> profile "
176 "<off|count|on|flush>\n");
177 return False;
180 if (strcmp(argv[1], "off") == 0) {
181 v = 0;
182 } else if (strcmp(argv[1], "count") == 0) {
183 v = 1;
184 } else if (strcmp(argv[1], "on") == 0) {
185 v = 2;
186 } else if (strcmp(argv[1], "flush") == 0) {
187 v = 3;
188 } else {
189 fprintf(stderr, "Unknown profile command '%s'\n", argv[1]);
190 return False;
193 return send_message(pid, MSG_PROFILE, &v, sizeof(int), False);
196 /* Return the profiling level */
198 static void profilelevel_cb(int msg_type, pid_t pid, void *buf, size_t len)
200 int level;
201 const char *s;
203 num_replies++;
205 if (len != sizeof(int)) {
206 fprintf(stderr, "invalid message length %ld returned\n",
207 (unsigned long)len);
208 return;
211 memcpy(&level, buf, sizeof(int));
213 switch (level) {
214 case 0:
215 s = "not enabled";
216 break;
217 case 1:
218 s = "off";
219 break;
220 case 3:
221 s = "count only";
222 break;
223 case 7:
224 s = "count and time";
225 break;
226 default:
227 s = "BOGUS";
228 break;
231 printf("Profiling %s on pid %u\n",s,(unsigned int)pid);
234 static void profilelevel_rqst(int msg_type, pid_t pid, void *buf, size_t len)
236 int v = 0;
238 /* Send back a dummy reply */
240 send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
243 static BOOL do_profilelevel(const pid_t pid, const int argc, const char **argv)
245 if (argc != 1) {
246 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
247 return False;
250 /* Send a message and register our interest in a reply */
252 if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
253 return False;
255 message_register(MSG_PROFILELEVEL, profilelevel_cb);
256 message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
258 wait_replies(pid == 0);
260 /* No replies were received within the timeout period */
262 if (num_replies == 0)
263 printf("No replies received\n");
265 message_deregister(MSG_PROFILE);
267 return num_replies;
270 /* Display debug level settings */
272 static BOOL do_debuglevel(const pid_t pid, const int argc, const char **argv)
274 if (argc != 1) {
275 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
276 return False;
279 /* Send a message and register our interest in a reply */
281 if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
282 return False;
284 message_register(MSG_DEBUGLEVEL, print_string_cb);
286 wait_replies(pid == 0);
288 /* No replies were received within the timeout period */
290 if (num_replies == 0)
291 printf("No replies received\n");
293 message_deregister(MSG_DEBUGLEVEL);
295 return num_replies;
298 /* Send a print notify message */
300 static BOOL do_printnotify(const pid_t pid, const int argc, const char **argv)
302 const char *cmd;
304 /* Check for subcommand */
306 if (argc == 1) {
307 fprintf(stderr, "Must specify subcommand:\n");
308 fprintf(stderr, "\tqueuepause <printername>\n");
309 fprintf(stderr, "\tqueueresume <printername>\n");
310 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
311 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
312 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
313 fprintf(stderr, "\tprinter <printername> <comment|port|"
314 "driver> <value>\n");
316 return False;
319 cmd = argv[1];
321 if (strcmp(cmd, "queuepause") == 0) {
323 if (argc != 3) {
324 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
325 " queuepause <printername>\n");
326 return False;
329 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
331 goto send;
333 } else if (strcmp(cmd, "queueresume") == 0) {
335 if (argc != 3) {
336 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
337 " queuereume <printername>\n");
338 return False;
341 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
343 goto send;
345 } else if (strcmp(cmd, "jobpause") == 0) {
346 int jobid;
348 if (argc != 4) {
349 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
350 " jobpause <printername> <unix-jobid>\n");
351 return False;
354 jobid = atoi(argv[3]);
356 notify_job_status_byname(
357 argv[2], jobid, JOB_STATUS_PAUSED,
358 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
360 goto send;
362 } else if (strcmp(cmd, "jobresume") == 0) {
363 int jobid;
365 if (argc != 4) {
366 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
367 " jobpause <printername> <unix-jobid>\n");
368 return False;
371 jobid = atoi(argv[3]);
373 notify_job_status_byname(
374 argv[2], jobid, JOB_STATUS_QUEUED,
375 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
377 goto send;
379 } else if (strcmp(cmd, "jobdelete") == 0) {
380 int jobid;
382 if (argc != 4) {
383 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
384 " jobpause <printername> <unix-jobid>\n");
385 return False;
388 jobid = atoi(argv[3]);
390 notify_job_status_byname(
391 argv[2], jobid, JOB_STATUS_DELETING,
392 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
394 notify_job_status_byname(
395 argv[2], jobid, JOB_STATUS_DELETING|
396 JOB_STATUS_DELETED,
397 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
399 goto send;
401 } else if (strcmp(cmd, "printer") == 0) {
402 uint32 attribute;
404 if (argc != 5) {
405 fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
406 "printer <printername> <comment|port|driver> "
407 "<value>\n");
408 return False;
411 if (strcmp(argv[3], "comment") == 0) {
412 attribute = PRINTER_NOTIFY_COMMENT;
413 } else if (strcmp(argv[3], "port") == 0) {
414 attribute = PRINTER_NOTIFY_PORT_NAME;
415 } else if (strcmp(argv[3], "driver") == 0) {
416 attribute = PRINTER_NOTIFY_DRIVER_NAME;
417 } else {
418 fprintf(stderr, "Invalid printer command '%s'\n",
419 argv[3]);
420 return False;
423 notify_printer_byname(argv[2], attribute, argv[4]);
425 goto send;
428 fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
429 return False;
431 send:
432 print_notify_send_messages(0);
433 return True;
436 /* Close a share */
438 static BOOL do_closeshare(const pid_t pid, const int argc, const char **argv)
440 if (argc != 2) {
441 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
442 "<sharename>\n");
443 return False;
446 return send_message(
447 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
450 /* Force a SAM synchronisation */
452 static BOOL do_samsync(const pid_t pid, const int argc, const char **argv)
454 if (argc != 1) {
455 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
456 return False;
459 return send_message(
460 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
463 /* Force a SAM replication */
465 static BOOL do_samrepl(const pid_t pid, const int argc, const char **argv)
467 if (argc != 1) {
468 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
469 return False;
472 return send_message(
473 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
476 /* Display talloc pool usage */
478 static BOOL do_poolusage(const pid_t pid, const int argc, const char **argv)
480 if (argc != 1) {
481 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
482 return False;
485 /* Send a message and register our interest in a reply */
487 if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
488 return False;
490 message_register(MSG_POOL_USAGE, print_string_cb);
492 wait_replies(pid == 0);
494 /* No replies were received within the timeout period */
496 if (num_replies == 0)
497 printf("No replies received\n");
499 message_deregister(MSG_POOL_USAGE);
501 return num_replies;
504 /* Perform a dmalloc mark */
506 static BOOL do_dmalloc_mark(const pid_t pid, const int argc, const char **argv)
508 if (argc != 1) {
509 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
510 return False;
513 return send_message(
514 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
517 /* Perform a dmalloc changed */
519 static BOOL do_dmalloc_changed(const pid_t pid, const int argc, const char **argv)
521 if (argc != 1) {
522 fprintf(stderr, "Usage: smbcontrol <dest> "
523 "dmalloc-log-changed\n");
524 return False;
527 return send_message(
528 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
531 /* Shutdown a server process */
533 static BOOL do_shutdown(const pid_t pid, const int argc, const char **argv)
535 if (argc != 1) {
536 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
537 return False;
540 return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
543 /* Notify a driver upgrade */
545 static BOOL do_drvupgrade(const pid_t pid, const int argc, const char **argv)
547 if (argc != 2) {
548 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
549 "<driver-name>\n");
550 return False;
553 return send_message(
554 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
557 static BOOL do_reload_config(const pid_t pid, const int argc, const char **argv)
559 if (argc != 1) {
560 fprintf(stderr, "Usage: smbcontrol <dest> reload-config\n");
561 return False;
564 return send_message(pid, MSG_SMB_CONF_UPDATED, NULL, 0, False);
567 /* A list of message type supported */
569 static const struct {
570 const char *name; /* Option name */
571 BOOL (*fn)(const pid_t pid, const int argc, const char **argv);
572 const char *help; /* Short help text */
573 } msg_types[] = {
574 { "debug", do_debug, "Set debuglevel" },
575 { "force-election", do_election,
576 "Force a browse election" },
577 { "ping", do_ping, "Elicit a response" },
578 { "profile", do_profile, "" },
579 { "profilelevel", do_profilelevel, "" },
580 { "debuglevel", do_debuglevel, "Display current debuglevels" },
581 { "printnotify", do_printnotify, "Send a print notify message" },
582 { "close-share", do_closeshare, "Forcibly disconnect a share" },
583 { "samsync", do_samsync, "Initiate SAM synchronisation" },
584 { "samrepl", do_samrepl, "Initiate SAM replication" },
585 { "pool-usage", do_poolusage, "Display talloc memory usage" },
586 { "dmalloc-mark", do_dmalloc_mark, "" },
587 { "dmalloc-log-changed", do_dmalloc_changed, "" },
588 { "shutdown", do_shutdown, "Shut down daemon" },
589 { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
590 { "reload-config", do_reload_config, "Force smbd or winbindd to reload config file"},
591 { "noop", do_noop, "Do nothing" },
592 { NULL }
595 /* Display usage information */
597 static void usage(poptContext *pc)
599 int i;
601 poptPrintHelp(*pc, stderr, 0);
603 fprintf(stderr, "\n");
604 fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
605 "process ID\n");
607 fprintf(stderr, "\n");
608 fprintf(stderr, "<message-type> is one of:\n");
610 for (i = 0; msg_types[i].name; i++)
611 fprintf(stderr, "\t%-30s%s\n", msg_types[i].name,
612 msg_types[i].help);
614 fprintf(stderr, "\n");
616 exit(1);
619 /* Return the pid number for a string destination */
621 static pid_t parse_dest(const char *dest)
623 pid_t pid;
625 /* Zero is a special return value for broadcast smbd */
627 if (strequal(dest, "smbd"))
628 return 0;
630 /* Try self - useful for testing */
632 if (strequal(dest, "self"))
633 return sys_getpid();
635 /* Check for numeric pid number */
637 if ((pid = atoi(dest)) != 0)
638 return pid;
640 /* Look up other destinations in pidfile directory */
642 if ((pid = pidfile_pid(dest)) != 0)
643 return pid;
645 fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
647 return -1;
650 /* Execute smbcontrol command */
652 static BOOL do_command(int argc, const char **argv)
654 const char *dest = argv[0], *command = argv[1];
655 pid_t pid;
656 int i;
658 /* Check destination */
660 if ((pid = parse_dest(dest)) == -1)
661 return False;
663 /* Check command */
665 for (i = 0; msg_types[i].name; i++) {
666 if (strequal(command, msg_types[i].name))
667 return msg_types[i].fn(pid, argc - 1, argv + 1);
670 fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
672 return False;
675 /* Main program */
677 int main(int argc, const char **argv)
679 poptContext pc;
680 int opt;
682 static struct poptOption wbinfo_options[] = {
683 { "timeout", 't', POPT_ARG_INT, &timeout, 't',
684 "Set timeout value in seconds", "TIMEOUT" },
686 { "configfile", 's', POPT_ARG_STRING, NULL, 's',
687 "Use alternative configuration file", "CONFIGFILE" },
689 POPT_TABLEEND
692 struct poptOption options[] = {
693 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0,
694 "Options" },
696 POPT_AUTOHELP
697 POPT_COMMON_VERSION
698 POPT_TABLEEND
701 setup_logging(argv[0],True);
703 /* Parse command line arguments using popt */
705 pc = poptGetContext(
706 "smbcontrol", argc, (const char **)argv, options, 0);
708 poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
709 "<parameters>");
711 if (argc == 1)
712 usage(&pc);
714 while ((opt = poptGetNextOpt(pc)) != -1) {
715 switch(opt) {
716 case 't': /* --timeout */
717 argc -= 2;
718 break;
719 case 's': /* --configfile */
720 pstrcpy(dyn_CONFIGFILE, poptGetOptArg(pc));
721 argc -= 2;
722 break;
723 default:
724 fprintf(stderr, "Invalid option\n");
725 poptPrintHelp(pc, stderr, 0);
726 break;
730 /* We should now have the remaining command line arguments in
731 argv. The argc parameter should have been decremented to the
732 correct value in the above switch statement. */
734 argv = (const char **)poptGetArgs(pc);
735 argc--; /* Don't forget about argv[0] */
737 if (argc == 1)
738 usage(&pc);
740 lp_load(dyn_CONFIGFILE,False,False,False);
742 /* Need to invert sense of return code -- samba
743 * routines mostly return True==1 for success, but
744 * shell needs 0. */
746 return !do_command(argc, argv);