s3-popt: Only include popt-common.h when needed.
[nivanova/samba-autobuild/.git] / source3 / utils / status.c
index 831bdcca185746249db8f001a35fc337a8a2170a..fb23789d0eea202c781f0dd68b2dc49a2144d885 100644 (file)
@@ -2,17 +2,17 @@
    Unix SMB/CIFS implementation.
    status reporting
    Copyright (C) Andrew Tridgell 1994-1998
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -31,6 +31,7 @@
  */
 
 #include "includes.h"
+#include "popt_common.h"
 
 #define SMB_MAXPIDS            2048
 static uid_t           Ucrit_uid = 0;               /* added by OH */
@@ -61,25 +62,25 @@ static unsigned int Ucrit_checkUid(uid_t uid)
 {
        if ( !Ucrit_IsActive ) 
                return 1;
-       
+
        if ( uid == Ucrit_uid ) 
                return 1;
-       
+
        return 0;
 }
 
 static unsigned int Ucrit_checkPid(struct server_id pid)
 {
        int i;
-       
+
        if ( !Ucrit_IsActive ) 
                return 1;
-       
+
        for (i=0;i<Ucrit_MaxPid;i++) {
                if (cluster_id_equal(&pid, &Ucrit_pid[i])) 
                        return 1;
        }
-       
+
        return 0;
 }
 
@@ -96,7 +97,7 @@ static bool Ucrit_addPid( struct server_id pid )
        }
 
        Ucrit_pid[Ucrit_MaxPid++] = pid;
-       
+
        return True;
 }
 
@@ -191,7 +192,7 @@ static void print_brl(struct file_id id,
        };
        const char *desc="X";
        const char *sharepath = "";
-       const char *fname = "";
+       char *fname = NULL;
        struct share_mode_lock *share_mode;
 
        if (count==0) {
@@ -201,10 +202,19 @@ static void print_brl(struct file_id id,
        }
        count++;
 
-       share_mode = fetch_share_mode_unlocked(NULL, id, "__unspecified__", "__unspecified__");
+       share_mode = fetch_share_mode_unlocked(NULL, id);
        if (share_mode) {
-               sharepath = share_mode->servicepath;
-               fname = share_mode->filename;
+               bool has_stream = share_mode->stream_name != NULL;
+
+               fname = talloc_asprintf(NULL, "%s%s%s", share_mode->base_name,
+                                       has_stream ? ":" : "",
+                                       has_stream ? share_mode->stream_name :
+                                       "");
+       } else {
+               fname = talloc_strdup(NULL, "");
+               if (fname == NULL) {
+                       return;
+               }
        }
 
        for (i=0;i<ARRAY_SIZE(lock_types);i++) {
@@ -219,11 +229,11 @@ static void print_brl(struct file_id id,
                 (double)start, (double)size,
                 sharepath, fname);
 
+       TALLOC_FREE(fname);
        TALLOC_FREE(share_mode);
 }
 
-static int traverse_fn1(struct db_record *rec,
-                       const struct connections_key *key,
+static int traverse_fn1(const struct connections_key *key,
                        const struct connections_data *crec,
                        void *state)
 {
@@ -242,31 +252,27 @@ static int traverse_fn1(struct db_record *rec,
        return 0;
 }
 
-static int traverse_sessionid(struct db_record *db, void *state)
+static int traverse_sessionid(const char *key, struct sessionid *session,
+                             void *private_data)
 {
-       struct sessionid sessionid;
        fstring uid_str, gid_str;
 
-       if (db->value.dsize != sizeof(sessionid))
-               return 0;
-
-       memcpy(&sessionid, db->value.dptr, sizeof(sessionid));
-
-       if (!process_exists(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) {
+       if (!process_exists(session->pid)
+           || !Ucrit_checkUid(session->uid)) {
                return 0;
        }
 
-       Ucrit_addPid( sessionid.pid );
+       Ucrit_addPid(session->pid);
 
-       fstr_sprintf(uid_str, "%d", sessionid.uid);
-       fstr_sprintf(gid_str, "%d", sessionid.gid);
+       fstr_sprintf(uid_str, "%u", (unsigned int)session->uid);
+       fstr_sprintf(gid_str, "%u", (unsigned int)session->gid);
 
        d_printf("%-7s   %-12s  %-12s  %-12s (%s)\n",
-                procid_str_static(&sessionid.pid),
-                numeric_only ? uid_str : uidtoname(sessionid.uid),
-                numeric_only ? gid_str : gidtoname(sessionid.gid), 
-                sessionid.remote_machine, sessionid.hostname);
-       
+                procid_str_static(&session->pid),
+                numeric_only ? uid_str : uidtoname(session->uid),
+                numeric_only ? gid_str : gidtoname(session->gid),
+                session->remote_machine, session->hostname);
+
        return 0;
 }
 
@@ -302,9 +308,9 @@ static int traverse_sessionid(struct db_record *db, void *state)
        load_case_tables();
 
        setup_logging(argv[0],True);
-       
+
        dbf = x_stderr;
-       
+
        if (getuid() != geteuid()) {
                d_printf("smbstatus should not be run setuid\n");
                ret = 1;
@@ -313,7 +319,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
 
        pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
                            POPT_CONTEXT_KEEP_FIRST);
-       
+
        while ((c = poptGetNextOpt(pc)) != -1) {
                switch (c) {
                case 'p':
@@ -367,18 +373,20 @@ static int traverse_sessionid(struct db_record *db, void *state)
                goto done;
        }
 
-       /*
-        * This implicitly initializes the global ctdbd connection, usable by
-        * the db_open() calls further down.
-        */
-
-       msg_ctx = messaging_init(NULL, procid_self(),
-                                event_context_init(NULL));
 
-       if (msg_ctx == NULL) {
-               fprintf(stderr, "messaging_init failed\n");
-               ret = -1;
-               goto done;
+       if (lp_clustering()) {
+               /*
+                * This implicitly initializes the global ctdbd
+                * connection, usable by the db_open() calls further
+                * down.
+                */
+               msg_ctx = messaging_init(NULL, procid_self(),
+                                        event_context_init(NULL));
+               if (msg_ctx == NULL) {
+                       fprintf(stderr, "messaging_init failed\n");
+                       ret = -1;
+                       goto done;
+               }
        }
 
        if (!lp_load(get_dyn_CONFIGFILE(),False,False,False,True)) {
@@ -400,29 +408,21 @@ static int traverse_sessionid(struct db_record *db, void *state)
        }
 
        if ( show_processes ) {
-               struct db_context *db;
-               db = db_open(NULL, lock_path("sessionid.tdb"), 0,
-                            TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
-               if (!db) {
-                       d_printf("sessionid.tdb not initialised\n");
-               } else {
-                       d_printf("\nSamba version %s\n",samba_version_string());
-                       d_printf("PID     Username      Group         Machine                        \n");
-                       d_printf("-------------------------------------------------------------------\n");
-                       if (lp_security() == SEC_SHARE) {
-                               d_printf(" <processes do not show up in "
-                                   "anonymous mode>\n");
-                       }
-
-                       db->traverse_read(db, traverse_sessionid, NULL);
-                       TALLOC_FREE(db);
+               d_printf("\nSamba version %s\n",samba_version_string());
+               d_printf("PID     Username      Group         Machine                        \n");
+               d_printf("-------------------------------------------------------------------\n");
+               if (lp_security() == SEC_SHARE) {
+                       d_printf(" <processes do not show up in "
+                                "anonymous mode>\n");
                }
 
+               sessionid_traverse_read(traverse_sessionid, NULL);
+
                if (processes_only) {
                        goto done;
                }
        }
-  
+
        if ( show_shares ) {
                if (verbose) {
                        d_printf("Opened %s\n", lock_path("connections.tdb"));
@@ -431,11 +431,11 @@ static int traverse_sessionid(struct db_record *db, void *state)
                if (brief) {
                        goto done;
                }
-               
+
                d_printf("\nService      pid     machine       Connected at\n");
                d_printf("-------------------------------------------------------\n");
-       
-               connections_forall(traverse_fn1, NULL);
+
+               connections_forall_read(traverse_fn1, NULL);
 
                d_printf("\n");
 
@@ -465,7 +465,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
                        ret = 1;
                        goto done;
                }
-               
+
                result = share_mode_forall(print_share_mode, NULL);
 
                if (result == 0) {
@@ -473,13 +473,13 @@ static int traverse_sessionid(struct db_record *db, void *state)
                } else if (result == -1) {
                        d_printf("locked file list truncated\n");
                }
-               
+
                d_printf("\n");
 
                if (show_brl) {
                        brl_forall(print_brl, NULL);
                }
-               
+
                locking_end();
        }