lib: Add lib/util/server_id.h
[kai/samba-autobuild/.git] / source3 / utils / net_status.c
index 4e355e48b35e8ae9ae5039f241578487567c526b..0d658a0c36e0eb632a925fb4b1f2a204a802e461 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "includes.h"
+#include "lib/util/server_id.h"
 #include "utils/net.h"
+#include "session.h"
+#include "messages.h"
+#include "lib/conn_tdb.h"
 
 int net_status_usage(struct net_context *c, int argc, const char **argv)
 {
-       d_printf("  net status sessions [parseable] "
-                "Show list of open sessions\n");
-       d_printf("  net status shares [parseable]   "
-                "Show list of open shares\n");
+       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(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                       void *state)
+static int show_session(const char *key, struct sessionid *session,
+                       void *private_data)
 {
-       bool *parseable = (bool *)state;
-       struct sessionid sessionid;
+       struct server_id_buf tmp;
+       bool *parseable = (bool *)private_data;
 
-       if (dbuf.dsize != sizeof(sessionid))
-               return 0;
-
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
-
-       if (!process_exists(sessionid.pid)) {
+       if (!process_exists(session->pid)) {
                return 0;
        }
 
        if (*parseable) {
                d_printf("%s\\%s\\%s\\%s\\%s\n",
-                        procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
-                        gidtoname(sessionid.gid),
-                        sessionid.remote_machine, sessionid.hostname);
+                        server_id_str_buf(session->pid, &tmp),
+                        uidtoname(session->uid),
+                        gidtoname(session->gid),
+                        session->remote_machine, session->hostname);
        } else {
                d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
-                        procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
-                        gidtoname(sessionid.gid),
-                        sessionid.remote_machine, sessionid.hostname);
+                        server_id_str_buf(session->pid, &tmp),
+                        uidtoname(session->uid),
+                        gidtoname(session->gid),
+                        session->remote_machine, session->hostname);
        }
 
        return 0;
@@ -60,15 +61,16 @@ 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;
        bool parseable;
 
        if (c->display_usage) {
-               d_printf("Usage:\n"
-                        "net status sessions [parseable]\n"
-                        "    Display open user sessions.\n"
-                        "    If parseable is specified, output is machine-"
-                        "readable.\n");
+               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;
        }
 
@@ -81,32 +83,23 @@ static int net_status_sessions(struct net_context *c, int argc, const char **arg
        }
 
        if (!parseable) {
-               d_printf("PID     Username      Group         Machine"
-                        "                        \n");
-               d_printf("-------------------------------------------"
-                        "------------------------\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"));
-               return -1;
+               d_printf(_("PID     Username      Group         Machine"
+                          "                        \n"
+                          "-------------------------------------------"
+                          "------------------------\n"));
        }
 
-       tdb_traverse(tdb, show_session, &parseable);
-       tdb_close(tdb);
-
+       sessionid_traverse_read(show_session, &parseable);
        return 0;
 }
 
-static int show_share(struct db_record *rec,
-                     const struct connections_key *key,
+static int show_share(const struct connections_key *key,
                      const struct connections_data *crec,
                      void *state)
 {
-       if (crec->cnum == -1)
+       struct server_id_buf tmp;
+
+       if (crec->cnum == TID_FIELD_INVALID)
                return 0;
 
        if (!process_exists(crec->pid)) {
@@ -114,7 +107,7 @@ static int show_share(struct db_record *rec,
        }
 
        d_printf("%-10.10s   %s   %-12s  %s",
-              crec->servicename, procid_str_static(&crec->pid),
+              crec->servicename, server_id_str_buf(crec->pid, &tmp),
               crec->machine,
               time_to_asc(crec->start));
 
@@ -126,18 +119,12 @@ struct sessionids {
        struct sessionid *entries;
 };
 
-static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                      void *state)
+static int collect_pids(const char *key, struct sessionid *session,
+                       void *private_data)
 {
-       struct sessionids *ids = (struct sessionids *)state;
-       struct sessionid sessionid;
-
-       if (dbuf.dsize != sizeof(sessionid))
-               return 0;
-
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+       struct sessionids *ids = (struct sessionids *)private_data;
 
-       if (!process_exists(sessionid.pid))
+       if (!process_exists(session->pid))
                return 0;
 
        ids->num_entries += 1;
@@ -146,21 +133,21 @@ static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
                ids->num_entries = 0;
                return 0;
        }
-       ids->entries[ids->num_entries-1] = sessionid;
+       ids->entries[ids->num_entries-1] = *session;
 
        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)
 {
        struct sessionids *ids = (struct sessionids *)state;
+       struct server_id_buf tmp;
        int i;
        bool guest = true;
 
-       if (crec->cnum == -1)
+       if (crec->cnum == TID_FIELD_INVALID)
                return 0;
 
        if (!process_exists(crec->pid)) {
@@ -169,14 +156,14 @@ static int show_share_parseable(struct db_record *rec,
 
        for (i=0; i<ids->num_entries; i++) {
                struct server_id id = ids->entries[i].pid;
-               if (procid_equal(&id, &crec->pid)) {
+               if (serverid_equal(&id, &crec->pid)) {
                        guest = false;
                        break;
                }
        }
 
        d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
-                crec->servicename,procid_str_static(&crec->pid),
+                crec->servicename, server_id_str_buf(crec->pid, &tmp),
                 guest ? "" : uidtoname(ids->entries[i].uid),
                 guest ? "" : gidtoname(ids->entries[i].gid),
                 crec->machine,
@@ -189,23 +176,13 @@ 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;
 
        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"));
-               return -1;
-       }
-
-       tdb_traverse(tdb, collect_pid, &ids);
-       tdb_close(tdb);
+       sessionid_traverse_read(collect_pids, &ids);
 
-       connections_forall(show_share_parseable, &ids);
+       connections_forall_read(show_share_parseable, &ids);
 
        SAFE_FREE(ids.entries);
 
@@ -215,22 +192,24 @@ 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("Usage:\n"
-                        "net status shares [parseable]\n"
-                        "    Display open user shares.\n"
-                        "    If parseable is specified, output is machine-"
-                        "readable.\n");
+               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);
+               connections_forall_read(show_share, NULL);
 
                return 0;
        }
@@ -249,19 +228,19 @@ int net_status(struct net_context *c, int argc, const char **argv)
                        "sessions",
                        net_status_sessions,
                        NET_TRANSPORT_LOCAL,
-                       "Show list of open sessions",
-                       "net status sessions [parseable]\n"
-                       "    If parseable is specified, output is presented "
-                       "in a machine-parseable fashion."
+                       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,
-                       "Show list of open shares",
-                       "net status shares [parseable]\n"
-                       "    If parseable is specified, output is presented "
-                       "in a machine-parseable fashion."
+                       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}
        };