s3:smb: introduce a name TID_FIELD_INVALID for the invalid value for a cnum/tid
[kai/samba.git] / source3 / utils / status.c
index 6d616149d708529f80d7662810ab4cfe509ffd2b..a6e80550628d866e1a00c11e279ba103acfdc41b 100644 (file)
  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "popt_common.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
+#include "../libcli/security/security.h"
+#include "session.h"
+#include "locking/proto.h"
+#include "messages.h"
+#include "librpc/gen_ndr/open_files.h"
+#include "smbd/smbd.h"
+#include "librpc/gen_ndr/notify.h"
+#include "lib/conn_tdb.h"
 
 #define SMB_MAXPIDS            2048
 static uid_t           Ucrit_uid = 0;               /* added by OH */
@@ -203,11 +215,13 @@ static void print_brl(struct file_id id,
 
        share_mode = fetch_share_mode_unlocked(NULL, id);
        if (share_mode) {
-               bool has_stream = share_mode->stream_name != NULL;
+               bool has_stream = share_mode->data->stream_name != NULL;
 
-               fname = talloc_asprintf(NULL, "%s%s%s", share_mode->base_name,
+               fname = talloc_asprintf(NULL, "%s%s%s",
+                                       share_mode->data->base_name,
                                        has_stream ? ":" : "",
-                                       has_stream ? share_mode->stream_name :
+                                       has_stream ?
+                                       share_mode->data->stream_name :
                                        "");
        } else {
                fname = talloc_strdup(NULL, "");
@@ -232,12 +246,11 @@ static void print_brl(struct file_id id,
        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)
 {
-       if (crec->cnum == -1)
+       if (crec->cnum == TID_FIELD_INVALID)
                return 0;
 
        if (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid)) {
@@ -252,42 +265,61 @@ 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, "%u", (unsigned int)sessionid.uid);
-       fstr_sprintf(gid_str, "%u", (unsigned int)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;
 }
 
 
+static void print_notify_recs(const char *path,
+                             struct notify_db_entry *entries,
+                             size_t num_entries,
+                             time_t deleted_time, void *private_data)
+{
+       size_t i;
+       d_printf("%s\n", path);
+
+       if (num_entries == 0) {
+               d_printf("deleted %s\n", time_to_asc(deleted_time));
+       }
 
+       for (i=0; i<num_entries; i++) {
+               struct notify_db_entry *e = &entries[i];
+               char *str;
+
+               str = server_id_str(talloc_tos(), &e->server);
+               printf("%s %x %x\n", str, (unsigned)e->filter,
+                      (unsigned)e->subdir_filter);
+               TALLOC_FREE(str);
+       }
+       printf("\n");
+}
 
  int main(int argc, char *argv[])
 {
        int c;
        int profile_only = 0;
        bool show_processes, show_locks, show_shares;
+       bool show_notify = false;
        poptContext pc;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -295,6 +327,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
                {"verbose",     'v', POPT_ARG_NONE,     NULL, 'v', "Be verbose" },
                {"locks",       'L', POPT_ARG_NONE,     NULL, 'L', "Show locks only" },
                {"shares",      'S', POPT_ARG_NONE,     NULL, 'S', "Show shares only" },
+               {"notify",      'N', POPT_ARG_NONE,     NULL, 'N', "Show notifies" },
                {"user",        'u', POPT_ARG_STRING,   &username, 'u', "Switch to user" },
                {"brief",       'b', POPT_ARG_NONE,     NULL, 'b', "Be brief" },
                {"profile",     'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
@@ -311,9 +344,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
        sec_init();
        load_case_tables();
 
-       setup_logging(argv[0],True);
-
-       dbf = x_stderr;
+       setup_logging(argv[0], DEBUG_STDERR);
 
        if (getuid() != geteuid()) {
                d_printf("smbstatus should not be run setuid\n");
@@ -338,6 +369,9 @@ static int traverse_sessionid(struct db_record *db, void *state)
                case 'S':
                        shares_only = true;
                        break;
+               case 'N':
+                       show_notify = true;
+                       break;
                case 'b':
                        brief = true;
                        break;
@@ -384,8 +418,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
                 * connection, usable by the db_open() calls further
                 * down.
                 */
-               msg_ctx = messaging_init(NULL, procid_self(),
-                                        event_context_init(NULL));
+               msg_ctx = messaging_init(NULL, event_context_init(NULL));
                if (msg_ctx == NULL) {
                        fprintf(stderr, "messaging_init failed\n");
                        ret = -1;
@@ -393,7 +426,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
                }
        }
 
-       if (!lp_load(get_dyn_CONFIGFILE(),False,False,False,True)) {
+       if (!lp_load_global(get_dyn_CONFIGFILE())) {
                fprintf(stderr, "Can't load %s - run testparm to debug it\n",
                        get_dyn_CONFIGFILE());
                ret = -1;
@@ -412,23 +445,11 @@ 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");
-                       }
+               d_printf("\nSamba version %s\n",samba_version_string());
+               d_printf("PID     Username      Group         Machine                        \n");
+               d_printf("-------------------------------------------------------------------\n");
 
-                       db->traverse_read(db, traverse_sessionid, NULL);
-                       TALLOC_FREE(db);
-               }
+               sessionid_traverse_read(traverse_sessionid, NULL);
 
                if (processes_only) {
                        goto done;
@@ -447,7 +468,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
                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");
 
@@ -460,7 +481,8 @@ static int traverse_sessionid(struct db_record *db, void *state)
                int result;
                struct db_context *db;
                db = db_open(NULL, lock_path("locking.tdb"), 0,
-                            TDB_CLEAR_IF_FIRST, O_RDONLY, 0);
+                            TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
+                            DBWRAP_LOCK_ORDER_1);
 
                if (!db) {
                        d_printf("%s not initialised\n",
@@ -482,7 +504,7 @@ static int traverse_sessionid(struct db_record *db, void *state)
 
                if (result == 0) {
                        d_printf("No locked files\n");
-               } else if (result == -1) {
+               } else if (result < 0) {
                        d_printf("locked file list truncated\n");
                }
 
@@ -495,6 +517,17 @@ static int traverse_sessionid(struct db_record *db, void *state)
                locking_end();
        }
 
+       if (show_notify) {
+               struct notify_context *n;
+
+               n = notify_init(talloc_tos(), NULL, NULL);
+               if (n == NULL) {
+                       goto done;
+               }
+               notify_walk(n, print_notify_recs, NULL);
+               TALLOC_FREE(n);
+       }
+
 done:
        TALLOC_FREE(frame);
        return ret;