s3: Add connections_forall_read()
[ira/wip.git] / source3 / utils / net_status.c
index 121577f165df30b839b96be12faeeee747d0f37b..47860cb5849dc591d9b60d6ac1b2031de148d53a 100644 (file)
 #include "includes.h"
 #include "utils/net.h"
 
-static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                       void *state)
+int net_status_usage(struct net_context *c, int argc, const char **argv)
 {
-       bool *parseable = (bool *)state;
+       d_printf(_("  net status sessions [parseable] "
+                  "Show list of open sessions\n"));
+       d_printf(_("  net status shares [parseable]   "
+                  "Show list of open shares\n"));
+       return -1;
+}
+
+static int show_session(struct db_record *rec, void *private_data)
+{
+       bool *parseable = (bool *)private_data;
        struct sessionid sessionid;
 
-       if (dbuf.dsize != sizeof(sessionid))
+       if (rec->value.dsize != sizeof(sessionid))
                return 0;
 
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+       memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
 
        if (!process_exists(sessionid.pid)) {
                return 0;
@@ -51,34 +59,45 @@ static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
 
 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
 {
-       TDB_CONTEXT *tdb;
+       struct db_context *db;
        bool parseable;
 
+       if (c->display_usage) {
+               d_printf(  "%s\n"
+                          "net status sessions [parseable]\n"
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Display open user sessions.\n"
+                          "    If parseable is specified, output is machine-"
+                          "readable."));
+               return 0;
+       }
+
        if (argc == 0) {
                parseable = false;
        } else if ((argc == 1) && strequal(argv[0], "parseable")) {
                parseable = true;
        } else {
-               return net_help_status(c, argc, argv);
+               return net_status_usage(c, argc, argv);
        }
 
        if (!parseable) {
-               d_printf("PID     Username      Group         Machine"
-                        "                        \n");
-               d_printf("-------------------------------------------"
-                        "------------------------\n");
+               d_printf(_("PID     Username      Group         Machine"
+                          "                        \n"
+                          "-------------------------------------------"
+                          "------------------------\n"));
        }
 
-       tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
+       db = db_open(NULL, lock_path("sessionid.tdb"), 0,
+                    TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
+       if (db == NULL) {
+               d_fprintf(stderr, _("%s not initialised\n"),
+                         lock_path("sessionid.tdb"));
                return -1;
        }
 
-       tdb_traverse(tdb, show_session, &parseable);
-       tdb_close(tdb);
+       db->traverse_read(db, show_session, &parseable);
+       TALLOC_FREE(db);
 
        return 0;
 }
@@ -108,16 +127,15 @@ struct sessionids {
        struct sessionid *entries;
 };
 
-static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                      void *state)
+static int collect_pid(struct db_record *rec, void *private_data)
 {
-       struct sessionids *ids = (struct sessionids *)state;
+       struct sessionids *ids = (struct sessionids *)private_data;
        struct sessionid sessionid;
 
-       if (dbuf.dsize != sizeof(sessionid))
+       if (rec->value.dsize != sizeof(sessionid))
                return 0;
 
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+       memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
 
        if (!process_exists(sessionid.pid))
                return 0;
@@ -133,8 +151,7 @@ static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
        return 0;
 }
 
-static int show_share_parseable(struct db_record *rec,
-                               const struct connections_key *key,
+static int show_share_parseable(const struct connections_key *key,
                                const struct connections_data *crec,
                                void *state)
 {
@@ -171,23 +188,23 @@ static int show_share_parseable(struct db_record *rec,
 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
 {
        struct sessionids ids;
-       TDB_CONTEXT *tdb;
+       struct db_context *db;
 
        ids.num_entries = 0;
        ids.entries = NULL;
 
-       tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
+       db = db_open(NULL, lock_path("sessionid.tdb"), 0,
+                    TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
+       if (db == NULL) {
+               d_fprintf(stderr, _("%s not initialised\n"),
+                         lock_path("sessionid.tdb"));
                return -1;
        }
 
-       tdb_traverse(tdb, collect_pid, &ids);
-       tdb_close(tdb);
+       db->traverse_read(db, collect_pid, &ids);
+       TALLOC_FREE(db);
 
-       connections_forall(show_share_parseable, &ids);
+       connections_forall_read(show_share_parseable, &ids);
 
        SAFE_FREE(ids.entries);
 
@@ -196,12 +213,23 @@ static int net_status_shares_parseable(struct net_context *c, int argc, const ch
 
 static int net_status_shares(struct net_context *c, int argc, const char **argv)
 {
+       if (c->display_usage) {
+               d_printf(  "%s\n"
+                          "net status shares [parseable]\n"
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Display open user shares.\n"
+                          "    If parseable is specified, output is machine-"
+                          "readable."));
+               return 0;
+       }
+
        if (argc == 0) {
 
-               d_printf("\nService      pid     machine       "
-                        "Connected at\n");
-               d_printf("-------------------------------------"
-                        "------------------\n");
+               d_printf(_("\nService      pid     machine       "
+                          "Connected at\n"
+                          "-------------------------------------"
+                          "------------------\n"));
 
                connections_forall(show_share, NULL);
 
@@ -209,7 +237,7 @@ static int net_status_shares(struct net_context *c, int argc, const char **argv)
        }
 
        if ((argc != 1) || !strequal(argv[0], "parseable")) {
-               return net_help_status(c, argc, argv);
+               return net_status_usage(c, argc, argv);
        }
 
        return net_status_shares_parseable(c, argc, argv);
@@ -218,9 +246,25 @@ static int net_status_shares(struct net_context *c, int argc, const char **argv)
 int net_status(struct net_context *c, int argc, const char **argv)
 {
        struct functable func[] = {
-               {"sessions", net_status_sessions},
-               {"shares", net_status_shares},
-               {NULL, NULL}
+               {
+                       "sessions",
+                       net_status_sessions,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Show list of open sessions"),
+                       N_("net status sessions [parseable]\n"
+                          "    If parseable is specified, output is presented "
+                          "in a machine-parseable fashion.")
+               },
+               {
+                       "shares",
+                       net_status_shares,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Show list of open shares"),
+                       N_("net status shares [parseable]\n"
+                          "    If parseable is specified, output is presented "
+                          "in a machine-parseable fashion.")
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
-       return net_run_function(c, argc, argv, func, net_help_status);
+       return net_run_function(c, argc, argv, "net status", func);
 }