RIP BOOL. Convert BOOL -> bool. I found a few interesting
[samba.git] / source3 / utils / smbcontrol.c
index 532b2fb8bf04952e735cb5e1ee358551afff3c2a..b79fa29cdb4ed1981e3a2e08fac0ba50069d4971 100644 (file)
@@ -46,11 +46,11 @@ static int num_replies;             /* Used by message callback fns */
 
 /* Send a message to a destination pid.  Zero means broadcast smbd. */
 
-static BOOL send_message(struct messaging_context *msg_ctx,
+static bool send_message(struct messaging_context *msg_ctx,
                         struct server_id pid, int msg_type,
                         const void *buf, int len)
 {
-       BOOL ret;
+       bool ret;
        int n_sent = 0;
 
        if (procid_to_pid(&pid) != 0)
@@ -65,23 +65,38 @@ static BOOL send_message(struct messaging_context *msg_ctx,
        return ret;
 }
 
+static void timeout_handler(struct event_context *event_ctx,
+                           struct timed_event *te,
+                           const struct timeval *now,
+                           void *private_data)
+{
+       bool *timed_out = (bool *)private_data;
+       TALLOC_FREE(te);
+       *timed_out = True;
+}
+
 /* Wait for one or more reply messages */
 
 static void wait_replies(struct messaging_context *msg_ctx,
-                        BOOL multiple_replies)
+                        bool multiple_replies)
 {
-       time_t start_time = time(NULL);
-
-       /* Wait around a bit.  This is pretty disgusting - we have to
-           busy-wait here as there is no nicer way to do it. */
+       struct timed_event *te;
+       bool timed_out = False;
+
+       if (!(te = event_add_timed(messaging_event_context(msg_ctx), NULL,
+                                  timeval_current_ofs(timeout, 0),
+                                  "smbcontrol_timeout",
+                                  timeout_handler, (void *)&timed_out))) {
+               DEBUG(0, ("event_add_timed failed\n"));
+               return;
+       }
 
-       do {
+       while (!timed_out) {
                message_dispatch(msg_ctx);
-               event_loop_once(messaging_event_context(msg_ctx));
                if (num_replies > 0 && !multiple_replies)
                        break;
-               sleep(1);
-       } while (timeout - (time(NULL) - start_time) > 0);
+               event_loop_once(messaging_event_context(msg_ctx));
+       }
 }
 
 /* Message handler callback that displays the PID and a string on stdout */
@@ -111,7 +126,7 @@ static void print_string_cb(struct messaging_context *msg,
 
 /* Send no message.  Useful for testing. */
 
-static BOOL do_noop(struct messaging_context *msg_ctx,
+static bool do_noop(struct messaging_context *msg_ctx,
                    const struct server_id pid,
                    const int argc, const char **argv)
 {
@@ -127,7 +142,7 @@ static BOOL do_noop(struct messaging_context *msg_ctx,
 
 /* Send a debug string */
 
-static BOOL do_debug(struct messaging_context *msg_ctx,
+static bool do_debug(struct messaging_context *msg_ctx,
                     const struct server_id pid,
                     const int argc, const char **argv)
 {
@@ -252,14 +267,10 @@ static int stack_trace_connection(struct db_record *rec,
        return 0;
 }
 
-static BOOL do_daemon_stack_trace(struct messaging_context *msg_ctx,
+static bool do_daemon_stack_trace(struct messaging_context *msg_ctx,
                                  const struct server_id pid,
                       const int argc, const char **argv)
 {
-       fprintf(stderr,
-               "Daemon stack tracing is not supported on this platform\n");
-       return False;
-
        pid_t   dest;
        int     count = 0;
 
@@ -286,7 +297,7 @@ static BOOL do_daemon_stack_trace(struct messaging_context *msg_ctx,
 
 #else /* defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE) */
 
-static BOOL do_daemon_stack_trace(struct messaging_context *msg_ctx,
+static bool do_daemon_stack_trace(struct messaging_context *msg_ctx,
                                  const struct server_id pid,
                       const int argc, const char **argv)
 {
@@ -299,7 +310,7 @@ static BOOL do_daemon_stack_trace(struct messaging_context *msg_ctx,
 
 /* Inject a fault (fatal signal) into a running smbd */
 
-static BOOL do_inject_fault(struct messaging_context *msg_ctx,
+static bool do_inject_fault(struct messaging_context *msg_ctx,
                            const struct server_id pid,
                       const int argc, const char **argv)
 {
@@ -341,7 +352,7 @@ static BOOL do_inject_fault(struct messaging_context *msg_ctx,
 
 /* Force a browser election */
 
-static BOOL do_election(struct messaging_context *msg_ctx,
+static bool do_election(struct messaging_context *msg_ctx,
                        const struct server_id pid,
                        const int argc, const char **argv)
 {
@@ -367,7 +378,7 @@ static void pong_cb(struct messaging_context *msg,
        num_replies++;
 }
 
-static BOOL do_ping(struct messaging_context *msg_ctx,
+static bool do_ping(struct messaging_context *msg_ctx,
                    const struct server_id pid,
                    const int argc, const char **argv)
 {
@@ -397,7 +408,7 @@ static BOOL do_ping(struct messaging_context *msg_ctx,
 
 /* Set profiling options */
 
-static BOOL do_profile(struct messaging_context *msg_ctx,
+static bool do_profile(struct messaging_context *msg_ctx,
                       const struct server_id pid,
                       const int argc, const char **argv)
 {
@@ -480,7 +491,7 @@ static void profilelevel_rqst(struct messaging_context *msg_ctx,
        send_message(msg_ctx, pid, MSG_PROFILELEVEL, &v, sizeof(int));
 }
 
-static BOOL do_profilelevel(struct messaging_context *msg_ctx,
+static bool do_profilelevel(struct messaging_context *msg_ctx,
                            const struct server_id pid,
                            const int argc, const char **argv)
 {
@@ -512,7 +523,7 @@ static BOOL do_profilelevel(struct messaging_context *msg_ctx,
 
 /* Display debug level settings */
 
-static BOOL do_debuglevel(struct messaging_context *msg_ctx,
+static bool do_debuglevel(struct messaging_context *msg_ctx,
                          const struct server_id pid,
                          const int argc, const char **argv)
 {
@@ -542,7 +553,7 @@ static BOOL do_debuglevel(struct messaging_context *msg_ctx,
 
 /* Send a print notify message */
 
-static BOOL do_printnotify(struct messaging_context *msg_ctx,
+static bool do_printnotify(struct messaging_context *msg_ctx,
                           const struct server_id pid,
                           const int argc, const char **argv)
 {
@@ -683,7 +694,7 @@ send:
 
 /* Close a share */
 
-static BOOL do_closeshare(struct messaging_context *msg_ctx,
+static bool do_closeshare(struct messaging_context *msg_ctx,
                          const struct server_id pid,
                          const int argc, const char **argv)
 {
@@ -699,7 +710,7 @@ static BOOL do_closeshare(struct messaging_context *msg_ctx,
 
 /* force a blocking lock retry */
 
-static BOOL do_lockretry(struct messaging_context *msg_ctx,
+static bool do_lockretry(struct messaging_context *msg_ctx,
                         const struct server_id pid,
                         const int argc, const char **argv)
 {
@@ -713,7 +724,7 @@ static BOOL do_lockretry(struct messaging_context *msg_ctx,
 
 /* force a validation of all brl entries, including re-sends. */
 
-static BOOL do_brl_revalidate(struct messaging_context *msg_ctx,
+static bool do_brl_revalidate(struct messaging_context *msg_ctx,
                              const struct server_id pid,
                              const int argc, const char **argv)
 {
@@ -727,7 +738,7 @@ static BOOL do_brl_revalidate(struct messaging_context *msg_ctx,
 
 /* Force a SAM synchronisation */
 
-static BOOL do_samsync(struct messaging_context *msg_ctx,
+static bool do_samsync(struct messaging_context *msg_ctx,
                       const struct server_id pid,
                       const int argc, const char **argv)
 {
@@ -741,7 +752,7 @@ static BOOL do_samsync(struct messaging_context *msg_ctx,
 
 /* Force a SAM replication */
 
-static BOOL do_samrepl(struct messaging_context *msg_ctx,
+static bool do_samrepl(struct messaging_context *msg_ctx,
                       const struct server_id pid,
                       const int argc, const char **argv)
 {
@@ -755,7 +766,7 @@ static BOOL do_samrepl(struct messaging_context *msg_ctx,
 
 /* Display talloc pool usage */
 
-static BOOL do_poolusage(struct messaging_context *msg_ctx,
+static bool do_poolusage(struct messaging_context *msg_ctx,
                         const struct server_id pid,
                         const int argc, const char **argv)
 {
@@ -785,7 +796,7 @@ static BOOL do_poolusage(struct messaging_context *msg_ctx,
 
 /* Perform a dmalloc mark */
 
-static BOOL do_dmalloc_mark(struct messaging_context *msg_ctx,
+static bool do_dmalloc_mark(struct messaging_context *msg_ctx,
                            const struct server_id pid,
                            const int argc, const char **argv)
 {
@@ -799,7 +810,7 @@ static BOOL do_dmalloc_mark(struct messaging_context *msg_ctx,
 
 /* Perform a dmalloc changed */
 
-static BOOL do_dmalloc_changed(struct messaging_context *msg_ctx,
+static bool do_dmalloc_changed(struct messaging_context *msg_ctx,
                               const struct server_id pid,
                               const int argc, const char **argv)
 {
@@ -815,7 +826,7 @@ static BOOL do_dmalloc_changed(struct messaging_context *msg_ctx,
 
 /* Shutdown a server process */
 
-static BOOL do_shutdown(struct messaging_context *msg_ctx,
+static bool do_shutdown(struct messaging_context *msg_ctx,
                        const struct server_id pid,
                        const int argc, const char **argv)
 {
@@ -829,7 +840,7 @@ static BOOL do_shutdown(struct messaging_context *msg_ctx,
 
 /* Notify a driver upgrade */
 
-static BOOL do_drvupgrade(struct messaging_context *msg_ctx,
+static bool do_drvupgrade(struct messaging_context *msg_ctx,
                          const struct server_id pid,
                          const int argc, const char **argv)
 {
@@ -843,7 +854,7 @@ static BOOL do_drvupgrade(struct messaging_context *msg_ctx,
                            strlen(argv[1]) + 1);
 }
 
-static BOOL do_winbind_online(struct messaging_context *msg_ctx,
+static bool do_winbind_online(struct messaging_context *msg_ctx,
                              const struct server_id pid,
                             const int argc, const char **argv)
 {
@@ -877,12 +888,12 @@ static BOOL do_winbind_online(struct messaging_context *msg_ctx,
        return send_message(msg_ctx, pid, MSG_WINBIND_ONLINE, NULL, 0);
 }
 
-static BOOL do_winbind_offline(struct messaging_context *msg_ctx,
+static bool do_winbind_offline(struct messaging_context *msg_ctx,
                               const struct server_id pid,
                             const int argc, const char **argv)
 {
        TDB_CONTEXT *tdb;
-       BOOL ret = False;
+       bool ret = False;
        int retry = 0;
 
        if (argc != 1) {
@@ -949,7 +960,7 @@ static BOOL do_winbind_offline(struct messaging_context *msg_ctx,
        return ret;
 }
 
-static BOOL do_winbind_onlinestatus(struct messaging_context *msg_ctx,
+static bool do_winbind_onlinestatus(struct messaging_context *msg_ctx,
                                    const struct server_id pid,
                                    const int argc, const char **argv)
 {
@@ -981,7 +992,7 @@ static BOOL do_winbind_onlinestatus(struct messaging_context *msg_ctx,
        return num_replies;
 }
 
-static BOOL do_dump_event_list(struct messaging_context *msg_ctx,
+static bool do_dump_event_list(struct messaging_context *msg_ctx,
                               const struct server_id pid,
                               const int argc, const char **argv)
 {
@@ -997,8 +1008,50 @@ static BOOL do_dump_event_list(struct messaging_context *msg_ctx,
        return send_message(msg_ctx, pid, MSG_DUMP_EVENT_LIST, NULL, 0);
 }
 
+static void winbind_validate_cache_cb(struct messaging_context *msg,
+                                     void *private_data,
+                                     uint32_t msg_type,
+                                     struct server_id pid,
+                                     DATA_BLOB *data)
+{
+       char *src_string = procid_str(NULL, &pid);
+       printf("Winbindd cache is %svalid. (answer from pid %s)\n",
+              (*(data->data) == 0 ? "" : "NOT "), src_string);
+       TALLOC_FREE(src_string);
+       num_replies++;
+}
+
+static bool do_winbind_validate_cache(struct messaging_context *msg_ctx,
+                                     const struct server_id pid,
+                                     const int argc, const char **argv)
+{
+       struct server_id myid = pid_to_procid(sys_getpid());
+
+       if (argc != 1) {
+               fprintf(stderr, "Usage: smbcontrol winbindd validate-cache\n");
+               return False;
+       }
+
+       messaging_register(msg_ctx, NULL, MSG_WINBIND_VALIDATE_CACHE,
+                          winbind_validate_cache_cb);
+
+       if (!send_message(msg_ctx, pid, MSG_WINBIND_VALIDATE_CACHE, &myid,
+                         sizeof(myid))) {
+               return False;
+       }
+
+       wait_replies(msg_ctx, procid_to_pid(&pid) == 0);
+
+       if (num_replies == 0) {
+               printf("No replies received\n");
+       }
+
+       messaging_deregister(msg_ctx, MSG_WINBIND_VALIDATE_CACHE, NULL);
+
+       return num_replies;
+}
 
-static BOOL do_reload_config(struct messaging_context *msg_ctx,
+static bool do_reload_config(struct messaging_context *msg_ctx,
                             const struct server_id pid,
                             const int argc, const char **argv)
 {
@@ -1021,7 +1074,7 @@ static void my_make_nmb_name( struct nmb_name *n, const char *name, int type)
        push_ascii(n->scope,  global_scope(), 64, STR_TERMINATE);
 }
 
-static BOOL do_nodestatus(struct messaging_context *msg_ctx,
+static bool do_nodestatus(struct messaging_context *msg_ctx,
                          const struct server_id pid,
                          const int argc, const char **argv)
 {
@@ -1062,7 +1115,7 @@ static BOOL do_nodestatus(struct messaging_context *msg_ctx,
 
 static const struct {
        const char *name;       /* Option name */
-       BOOL (*fn)(struct messaging_context *msg_ctx,
+       bool (*fn)(struct messaging_context *msg_ctx,
                   const struct server_id pid,
                   const int argc, const char **argv);
        const char *help;       /* Short help text */
@@ -1095,6 +1148,8 @@ static const struct {
        { "offline", do_winbind_offline, "Ask winbind to go into offline state"},
        { "onlinestatus", do_winbind_onlinestatus, "Request winbind online status"},
        { "dump-event-list", do_dump_event_list, "Dump event list"},
+       { "validate-cache" , do_winbind_validate_cache,
+         "Validate winbind's credential cache" },
        { "noop", do_noop, "Do nothing" },
        { NULL }
 };
@@ -1133,7 +1188,7 @@ static struct server_id parse_dest(const char *dest)
        /* Zero is a special return value for broadcast smbd */
 
        if (strequal(dest, "smbd")) {
-               return interpret_pid("0");
+               return interpret_pid(MSG_BROADCAST_PID_STR);
        }
 
        /* Try self - useful for testing */
@@ -1172,7 +1227,7 @@ static struct server_id parse_dest(const char *dest)
 
 /* Execute smbcontrol command */
 
-static BOOL do_command(struct messaging_context *msg_ctx,
+static bool do_command(struct messaging_context *msg_ctx,
                       int argc, const char **argv)
 {
        const char *dest = argv[0], *command = argv[1];
@@ -1241,6 +1296,8 @@ int main(int argc, const char **argv)
                POPT_COMMON_SAMBA
                POPT_TABLEEND
        };
+       TALLOC_CTX *frame = talloc_stackframe();
+       int ret = 0;
 
        load_case_tables();
 
@@ -1274,11 +1331,13 @@ int main(int argc, const char **argv)
 
        argv = (const char **)poptGetArgs(pc);
        argc = 0;
-       while (argv[argc] != NULL) {
-               argc++;
+       if (argv != NULL) {
+               while (argv[argc] != NULL) {
+                       argc++;
+               }
        }
 
-       if (argc == 1)
+       if (argc <= 1)
                usage(pc);
 
        lp_load(dyn_CONFIGFILE,False,False,False,True);
@@ -1290,8 +1349,11 @@ int main(int argc, const char **argv)
        if (!(evt_ctx = event_context_init(NULL)) ||
            !(msg_ctx = messaging_init(NULL, server_id_self(), evt_ctx))) {
                fprintf(stderr, "could not init messaging context\n");
+               TALLOC_FREE(frame);
                exit(1);
        }
        
-       return !do_command(msg_ctx, argc, argv);
+       ret = !do_command(msg_ctx, argc, argv);
+       TALLOC_FREE(frame);
+       return ret;
 }