define SYSV for IRIX
[Samba.git] / source / utils / smbcontrol.c
blob190627e2a52b8c9dcbb8f983471cc32c96638dad
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 %d returned\n", len);
207 return;
210 memcpy(&level, buf, sizeof(int));
212 switch (level) {
213 case 0:
214 s = "not enabled";
215 break;
216 case 1:
217 s = "off";
218 break;
219 case 3:
220 s = "count only";
221 break;
222 case 7:
223 s = "count and time";
224 break;
225 default:
226 s = "BOGUS";
227 break;
230 printf("Profiling %s on pid %u\n",s,(unsigned int)pid);
233 static void profilelevel_rqst(int msg_type, pid_t pid, void *buf, size_t len)
235 int v = 0;
237 /* Send back a dummy reply */
239 send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
242 static BOOL do_profilelevel(const pid_t pid, const int argc, const char **argv)
244 if (argc != 1) {
245 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
246 return False;
249 /* Send a message and register our interest in a reply */
251 if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
252 return False;
254 message_register(MSG_PROFILELEVEL, profilelevel_cb);
255 message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
257 wait_replies(pid == 0);
259 /* No replies were received within the timeout period */
261 if (num_replies == 0)
262 printf("No replies received\n");
264 message_deregister(MSG_PROFILE);
266 return num_replies;
269 /* Display debug level settings */
271 static BOOL do_debuglevel(const pid_t pid, const int argc, const char **argv)
273 if (argc != 1) {
274 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
275 return False;
278 /* Send a message and register our interest in a reply */
280 if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
281 return False;
283 message_register(MSG_DEBUGLEVEL, print_string_cb);
285 wait_replies(pid == 0);
287 /* No replies were received within the timeout period */
289 if (num_replies == 0)
290 printf("No replies received\n");
292 message_deregister(MSG_DEBUGLEVEL);
294 return num_replies;
297 /* Send a print notify message */
299 static BOOL do_printnotify(const pid_t pid, const int argc, const char **argv)
301 const char *cmd;
303 /* Check for subcommand */
305 if (argc == 1) {
306 fprintf(stderr, "Must specify subcommand:\n");
307 fprintf(stderr, "\tqueuepause <printername>\n");
308 fprintf(stderr, "\tqueueresume <printername>\n");
309 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
310 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
311 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
312 fprintf(stderr, "\tprinter <printername> <comment|port|"
313 "driver> <value>\n");
315 return False;
318 cmd = argv[1];
320 if (strcmp(cmd, "queuepause") == 0) {
322 if (argc != 3) {
323 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
324 " queuepause <printername>\n");
325 return False;
328 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
330 goto send;
332 } else if (strcmp(cmd, "queueresume") == 0) {
334 if (argc != 3) {
335 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
336 " queuereume <printername>\n");
337 return False;
340 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
342 goto send;
344 } else if (strcmp(cmd, "jobpause") == 0) {
345 int jobid;
347 if (argc != 4) {
348 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
349 " jobpause <printername> <unix-jobid>\n");
350 return False;
353 jobid = atoi(argv[3]);
355 notify_job_status_byname(
356 argv[2], jobid, JOB_STATUS_PAUSED,
357 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
359 goto send;
361 } else if (strcmp(cmd, "jobresume") == 0) {
362 int jobid;
364 if (argc != 4) {
365 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
366 " jobpause <printername> <unix-jobid>\n");
367 return False;
370 jobid = atoi(argv[3]);
372 notify_job_status_byname(
373 argv[2], jobid, JOB_STATUS_QUEUED,
374 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
376 goto send;
378 } else if (strcmp(cmd, "jobdelete") == 0) {
379 int jobid;
381 if (argc != 4) {
382 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
383 " jobpause <printername> <unix-jobid>\n");
384 return False;
387 jobid = atoi(argv[3]);
389 notify_job_status_byname(
390 argv[2], jobid, JOB_STATUS_DELETING,
391 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
393 notify_job_status_byname(
394 argv[2], jobid, JOB_STATUS_DELETING|
395 JOB_STATUS_DELETED,
396 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
398 goto send;
400 } else if (strcmp(cmd, "printer") == 0) {
401 uint32 attribute;
403 if (argc != 5) {
404 fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
405 "printer <printername> <comment|port|driver> "
406 "<value>\n");
407 return False;
410 if (strcmp(argv[3], "comment") == 0) {
411 attribute = PRINTER_NOTIFY_COMMENT;
412 } else if (strcmp(argv[3], "port") == 0) {
413 attribute = PRINTER_NOTIFY_PORT_NAME;
414 } else if (strcmp(argv[3], "driver") == 0) {
415 attribute = PRINTER_NOTIFY_DRIVER_NAME;
416 } else {
417 fprintf(stderr, "Invalid printer command '%s'\n",
418 argv[3]);
419 return False;
422 notify_printer_byname(argv[2], attribute, argv[4]);
424 goto send;
427 fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
428 return False;
430 send:
431 print_notify_send_messages(0);
432 return True;
435 /* Close a share */
437 static BOOL do_closeshare(const pid_t pid, const int argc, const char **argv)
439 if (argc != 2) {
440 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
441 "<sharename>\n");
442 return False;
445 return send_message(
446 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
449 /* Force a SAM synchronisation */
451 static BOOL do_samsync(const pid_t pid, const int argc, const char **argv)
453 if (argc != 1) {
454 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
455 return False;
458 return send_message(
459 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
462 /* Force a SAM replication */
464 static BOOL do_samrepl(const pid_t pid, const int argc, const char **argv)
466 if (argc != 1) {
467 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
468 return False;
471 return send_message(
472 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
475 /* Display talloc pool usage */
477 static BOOL do_poolusage(const pid_t pid, const int argc, const char **argv)
479 if (argc != 1) {
480 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
481 return False;
484 /* Send a message and register our interest in a reply */
486 if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
487 return False;
489 message_register(MSG_POOL_USAGE, print_string_cb);
491 wait_replies(pid == 0);
493 /* No replies were received within the timeout period */
495 if (num_replies == 0)
496 printf("No replies received\n");
498 message_deregister(MSG_POOL_USAGE);
500 return num_replies;
503 /* Perform a dmalloc mark */
505 static BOOL do_dmalloc_mark(const pid_t pid, const int argc, const char **argv)
507 if (argc != 1) {
508 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
509 return False;
512 return send_message(
513 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
516 /* Perform a dmalloc changed */
518 static BOOL do_dmalloc_changed(const pid_t pid, const int argc, const char **argv)
520 if (argc != 1) {
521 fprintf(stderr, "Usage: smbcontrol <dest> "
522 "dmalloc-log-changed\n");
523 return False;
526 return send_message(
527 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
530 /* Shutdown a server process */
532 static BOOL do_shutdown(const pid_t pid, const int argc, const char **argv)
534 if (argc != 1) {
535 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
536 return False;
539 return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
542 /* Notify a driver upgrade */
544 static BOOL do_drvupgrade(const pid_t pid, const int argc, const char **argv)
546 if (argc != 2) {
547 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
548 "<driver-name>\n");
549 return False;
552 return send_message(
553 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
556 static BOOL do_reload_config(const pid_t pid, const int argc, const char **argv)
558 if (argc != 1) {
559 fprintf(stderr, "Usage: smbcontrol <dest> reload-config\n");
560 return False;
563 return send_message(pid, MSG_SMB_CONF_UPDATED, NULL, 0, False);
566 /* A list of message type supported */
568 static const struct {
569 const char *name; /* Option name */
570 BOOL (*fn)(const pid_t pid, const int argc, const char **argv);
571 const char *help; /* Short help text */
572 } msg_types[] = {
573 { "debug", do_debug, "Set debuglevel" },
574 { "force-election", do_election,
575 "Force a browse election" },
576 { "ping", do_ping, "Elicit a response" },
577 { "profile", do_profile, "" },
578 { "profilelevel", do_profilelevel, "" },
579 { "debuglevel", do_debuglevel, "Display current debuglevels" },
580 { "printnotify", do_printnotify, "Send a print notify message" },
581 { "close-share", do_closeshare, "Forcibly disconnect a share" },
582 { "samsync", do_samsync, "Initiate SAM synchronisation" },
583 { "samrepl", do_samrepl, "Initiate SAM replication" },
584 { "pool-usage", do_poolusage, "Display talloc memory usage" },
585 { "dmalloc-mark", do_dmalloc_mark, "" },
586 { "dmalloc-log-changed", do_dmalloc_changed, "" },
587 { "shutdown", do_shutdown, "Shut down daemon" },
588 { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
589 { "reload-config", do_reload_config, "Force smbd or winbindd to reload config file"},
590 { "noop", do_noop, "Do nothing" },
591 { NULL }
594 /* Display usage information */
596 static void usage(poptContext *pc)
598 int i;
600 poptPrintHelp(*pc, stderr, 0);
602 fprintf(stderr, "\n");
603 fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
604 "process ID\n");
606 fprintf(stderr, "\n");
607 fprintf(stderr, "<message-type> is one of:\n");
609 for (i = 0; msg_types[i].name; i++)
610 fprintf(stderr, "\t%-30s%s\n", msg_types[i].name,
611 msg_types[i].help);
613 fprintf(stderr, "\n");
615 exit(1);
618 /* Return the pid number for a string destination */
620 static pid_t parse_dest(const char *dest)
622 pid_t pid;
624 /* Zero is a special return value for broadcast smbd */
626 if (strequal(dest, "smbd"))
627 return 0;
629 /* Try self - useful for testing */
631 if (strequal(dest, "self"))
632 return sys_getpid();
634 /* Check for numeric pid number */
636 if ((pid = atoi(dest)) != 0)
637 return pid;
639 /* Look up other destinations in pidfile directory */
641 if ((pid = pidfile_pid(dest)) != 0)
642 return pid;
644 fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
646 return -1;
649 /* Execute smbcontrol command */
651 static BOOL do_command(int argc, const char **argv)
653 const char *dest = argv[0], *command = argv[1];
654 pid_t pid;
655 int i;
657 /* Check destination */
659 if ((pid = parse_dest(dest)) == -1)
660 return False;
662 /* Check command */
664 for (i = 0; msg_types[i].name; i++) {
665 if (strequal(command, msg_types[i].name))
666 return msg_types[i].fn(pid, argc - 1, argv + 1);
669 fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
671 return False;
674 /* Main program */
676 int main(int argc, const char **argv)
678 poptContext pc;
679 int opt;
681 static struct poptOption wbinfo_options[] = {
682 { "timeout", 't', POPT_ARG_INT, &timeout, 't',
683 "Set timeout value in seconds", "TIMEOUT" },
685 { "configfile", 's', POPT_ARG_STRING, NULL, 's',
686 "Use alternative configuration file", "CONFIGFILE" },
688 POPT_TABLEEND
691 struct poptOption options[] = {
692 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0,
693 "Options" },
695 POPT_AUTOHELP
696 POPT_COMMON_VERSION
697 POPT_TABLEEND
700 setup_logging(argv[0],True);
702 /* Parse command line arguments using popt */
704 pc = poptGetContext(
705 "smbcontrol", argc, (const char **)argv, options, 0);
707 poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
708 "<parameters>");
710 if (argc == 1)
711 usage(&pc);
713 while ((opt = poptGetNextOpt(pc)) != -1) {
714 switch(opt) {
715 case 't': /* --timeout */
716 argc -= 2;
717 break;
718 case 's': /* --configfile */
719 pstrcpy(dyn_CONFIGFILE, optarg);
720 argc -= 2;
721 break;
722 default:
723 fprintf(stderr, "Invalid option\n");
724 poptPrintHelp(pc, stderr, 0);
725 break;
729 /* We should now have the remaining command line arguments in
730 argv. The argc parameter should have been decremented to the
731 correct value in the above switch statement. */
733 argv = (const char **)poptGetArgs(pc);
734 argc--; /* Don't forget about argv[0] */
736 if (argc == 1)
737 usage(&pc);
739 lp_load(dyn_CONFIGFILE,False,False,False);
741 /* Need to invert sense of return code -- samba
742 * routines mostly return True==1 for success, but
743 * shell needs 0. */
745 return !do_command(argc, argv);