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 {"change_id", MSG_PRINTER_DRVUPGRADE},
53 static void usage(BOOL doexit)
57 printf("Usage: smbcontrol -i -s configfile\n");
58 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
60 printf("<destination> <message-type> <parameters>\n\n");
62 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
63 printf("\t<message-type> is one of: ");
64 for (i=0; msg_types[i].name; i++)
65 printf("%s%s", i?", ":"",msg_types[i].name);
70 static int pong_count;
71 static BOOL got_level;
72 static BOOL pong_registered = False;
73 static BOOL debuglevel_registered = False;
74 static BOOL profilelevel_registered = False;
78 * Wait for replies for up to @p *max_secs seconds, or until @p
79 * max_replies are received. max_replies may be NULL in which case it
82 * @note This is a pretty lame timeout; all it means is that after
83 * max_secs we won't look for any more messages.
85 static void wait_for_replies(int max_secs, int *max_replies)
87 time_t timeout_end = time(NULL) + max_secs;
89 while ((!max_replies || (*max_replies)-- > 0)
90 && (time(NULL) < timeout_end)) {
96 /****************************************************************************
97 a useful function for testing the message system
98 ****************************************************************************/
99 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
102 printf("PONG from PID %u\n",(unsigned int)src);
105 /****************************************************************************
106 Prints out the current Debug level returned by MSG_DEBUGLEVEL
107 ****************************************************************************/
108 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
110 const char *levels = (char *)buf;
113 printf("Current debug levels of PID %u are:\n",(unsigned int)src);
115 while(next_token(&levels, dbgcl, " ", sizeof(pstring)))
116 printf("%s\n", dbgcl);
121 /****************************************************************************
122 Prints out the current Profile level returned by MSG_PROFILELEVEL
123 ****************************************************************************/
124 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
128 memcpy(&level, buf, sizeof(int));
139 s = "count and time";
145 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
147 printf("Profiling not available on PID %u\n",(unsigned int)src);
153 * Handle reply from POOL_USAGE.
155 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
157 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
158 (unsigned int) src_pid, (int) len, (const char *) buf);
163 * Send a message to a named destination
165 * @return False if an error occurred.
167 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
170 /* "smbd" is the only broadcast operation */
171 if (strequal(dest,"smbd")) {
176 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
178 fprintf(stderr,"Failed to open connections database in send_message.\n");
182 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
184 DEBUG(10,("smbcontrol/send_message: broadcast message to "
185 "%d processes\n", n_sent));
189 } else if (strequal(dest,"nmbd")) {
190 pid = pidfile_pid(dest);
192 fprintf(stderr,"Can't find pid for nmbd\n");
195 } else if (strequal(dest,"self")) {
200 fprintf(stderr,"Not a valid pid\n");
205 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
206 return message_send_pid(pid, msg_type, buf, len, duplicates);
209 /****************************************************************************
210 evaluate a message type string
211 ****************************************************************************/
212 static int parse_type(char *mtype)
215 for (i=0;msg_types[i].name;i++) {
216 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
222 static void register_all(void)
224 message_register(MSG_POOL_USAGE, pool_usage_cb);
227 /* This guy is here so we can link printing/notify.c to the smbcontrol
228 binary without having to pull in tons of other crap. */
230 TDB_CONTEXT *conn_tdb_ctx(void)
232 static TDB_CONTEXT *tdb;
237 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
240 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
245 /****************************************************************************
247 ****************************************************************************/
248 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
254 mtype = parse_type(msg_name);
256 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
266 if (!params || !params[0]) {
267 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
271 /* first pass retrieve total lenght */
272 for (p = params; p && *p ; p++)
273 dim += (strnlen(*p, 1024) +1); /* lenght + space */
274 b = buf = malloc(dim);
276 fprintf(stderr, "Out of memory!");
279 /* now build a single string with all parameters */
280 for(p = params; p && *p; p++) {
281 int l = strnlen(*p, 1024);
288 send_message(dest, MSG_DEBUG, buf, dim, False);
296 if (!params || !params[0]) {
297 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
300 if (strequal(params[0], "off")) {
302 } else if (strequal(params[0], "count")) {
304 } else if (strequal(params[0], "on")) {
306 } else if (strequal(params[0], "flush")) {
310 "MSG_PROFILE parameter must be off, count, on, or flush\n");
313 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
316 case MSG_FORCE_ELECTION:
317 if (!strequal(dest, "nmbd")) {
318 fprintf(stderr,"force-election can only be sent to nmbd\n");
321 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
324 case MSG_REQ_PROFILELEVEL:
325 if (!profilelevel_registered) {
326 message_register(MSG_PROFILELEVEL, profilelevel_function);
327 profilelevel_registered = True;
330 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
332 timeout_start = time(NULL);
335 if ((time(NULL) - timeout_start) > MAX_WAIT) {
336 fprintf(stderr,"profilelevel timeout\n");
343 case MSG_REQ_DEBUGLEVEL:
344 if (!debuglevel_registered) {
345 message_register(MSG_DEBUGLEVEL, debuglevel_function);
346 debuglevel_registered = True;
349 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
351 timeout_start = time(NULL);
354 if ((time(NULL) - timeout_start) > MAX_WAIT) {
355 fprintf(stderr,"debuglevel timeout\n");
362 /* Send a notification message to a printer */
363 /* NB. None of these currently work due to changes in the printing notify mechanisms. */
366 case MSG_PRINTER_NOTIFY2: {
369 /* Read subcommand */
371 if (!params || !params[0]) {
372 fprintf(stderr, "Must specify subcommand:\n");
373 fprintf(stderr, "\tqueuepause <printername>\n");
374 fprintf(stderr, "\tqueueresume <printername>\n");
375 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
376 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
377 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
383 /* Pause a print queue */
385 if (strequal(cmd, "queuepause")) {
388 fprintf(stderr, "queuepause command requires a printer name\n");
392 notify_printer_status_byname(params[1], PRINTER_STATUS_PAUSED);
396 /* Resume a print queue */
398 if (strequal(cmd, "queueresume")) {
401 fprintf(stderr, "queueresume command requires a printer name\n");
405 notify_printer_status_byname(params[1], PRINTER_STATUS_OK);
409 /* Pause a print job */
411 if (strequal(cmd, "jobpause")) {
414 if (!params[1] || !params[2]) {
415 fprintf(stderr, "jobpause command requires a printer name and a jobid\n");
419 jobid = atoi(params[2]);
421 notify_job_status_byname(
422 params[1], jobid, JOB_STATUS_PAUSED,
423 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
426 /* Resume a print job */
428 if (strequal(cmd, "jobresume")) {
431 if (!params[1] || !params[2]) {
432 fprintf(stderr, "jobresume command requires a printer name and a jobid\n");
436 jobid = atoi(params[2]);
438 notify_job_status_byname(
439 params[1], jobid, JOB_STATUS_QUEUED,
440 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
443 /* Delete a print job */
445 if (strequal(cmd, "jobdelete")) {
448 if (!params[1] || !params[2]) {
449 fprintf(stderr, "jobdelete command requires a printer name and a jobid\n");
453 jobid = atoi(params[2]);
455 notify_job_status_byname(
456 params[1], jobid, JOB_STATUS_DELETING,
457 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
459 notify_job_status_byname(
460 params[1], jobid, JOB_STATUS_DELETING|
462 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
469 case MSG_SMB_FORCE_TDIS:
470 if (!strequal(dest, "smbd")) {
471 fprintf(stderr,"close-share can only be sent to smbd\n");
474 if (!params || !params[0]) {
475 fprintf(stderr, "close-share needs a share name or '*'\n");
478 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
479 strlen(params[0]) + 1, False);
482 case MSG_SMB_SAM_SYNC:
483 if (!strequal(dest, "smbd")) {
484 fprintf(stderr, "samsync can only be sent to smbd\n");
489 fprintf(stderr, "samsync does not take any parameters\n");
493 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
497 case MSG_SMB_SAM_REPL: {
500 if (!strequal(dest, "smbd")) {
501 fprintf(stderr, "sam repl can only be sent to smbd\n");
505 if (!params || !params[0]) {
506 fprintf(stderr, "SAM_REPL needs a parameter\n");
510 seqnum = atoi(params[0]);
512 retval = send_message(dest, MSG_SMB_SAM_SYNC,
513 (char *)&seqnum, sizeof(uint32), False);
519 if (!pong_registered) {
520 message_register(MSG_PONG, pong_function);
521 pong_registered = True;
523 if (!params || !params[0]) {
524 fprintf(stderr,"MSG_PING needs a parameter\n");
531 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
533 retval = send_message(dest, MSG_PING, NULL, 0, True);
537 wait_for_replies(MAX_WAIT, &n);
539 fprintf(stderr,"PING timeout\n");
543 case MSG_REQ_POOL_USAGE:
544 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
546 wait_for_replies(MAX_WAIT, NULL);
550 case MSG_REQ_DMALLOC_LOG_CHANGED:
551 case MSG_REQ_DMALLOC_MARK:
552 if (!send_message(dest, mtype, NULL, 0, False))
557 if (!send_message(dest, MSG_SHUTDOWN, NULL, 0, False))
560 case MSG_PRINTER_DRVUPGRADE:
561 if (!send_message(dest, MSG_PRINTER_DRVUPGRADE, params[0], 0, False))
569 int main(int argc, char *argv[])
574 BOOL interactive = False;
576 AllowDebugChange = False;
579 setup_logging(argv[0],True);
581 if (argc < 2) usage(True);
583 while ((opt = getopt(argc, argv,"is:")) != EOF) {
589 pstrcpy(dyn_CONFIGFILE, optarg);
592 printf("Unknown option %c (%d)\n", (char)opt, opt);
597 lp_load(dyn_CONFIGFILE,False,False,False);
599 if (!message_init()) exit(1);
602 argv = &argv[optind];
607 if (argc < 2) usage(True);
608 /* Need to invert sense of return code -- samba
609 * routines mostly return True==1 for success, but
611 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
618 printf("smbcontrol> ");
619 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
621 while ((myargc < 4) &&
622 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
626 if (strequal(myargv[0],"q")) break;
629 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))