r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[abartlet/samba.git/.git] / source3 / utils / net_status.c
index 21e6bffb2cfe6b3766a3b46efea2fac098d9a636..eae1fe7e3f09f50c22dfac1fb9235211910c1068 100644 (file)
@@ -5,7 +5,7 @@
 
    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
 
    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 2 of the License, or
+   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,
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "includes.h"
 #include "utils/net.h"
 
 #include "includes.h"
 #include "utils/net.h"
@@ -36,13 +35,13 @@ static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
        }
 
        if (*parseable) {
        }
 
        if (*parseable) {
-               d_printf("%d\\%s\\%s\\%s\\%s\n",
-                        (int)sessionid.pid, uidtoname(sessionid.uid),
+               d_printf("%s\\%s\\%s\\%s\\%s\n",
+                        procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
                         gidtoname(sessionid.gid), 
                         sessionid.remote_machine, sessionid.hostname);
        } else {
                         gidtoname(sessionid.gid), 
                         sessionid.remote_machine, sessionid.hostname);
        } else {
-               d_printf("%5d   %-12s  %-12s  %-12s (%s)\n",
-                        (int)sessionid.pid, uidtoname(sessionid.uid),
+               d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
+                        procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
                         gidtoname(sessionid.gid), 
                         sessionid.remote_machine, sessionid.hostname);
        }
                         gidtoname(sessionid.gid), 
                         sessionid.remote_machine, sessionid.hostname);
        }
@@ -74,7 +73,7 @@ static int net_status_sessions(int argc, const char **argv)
                           TDB_DEFAULT, O_RDONLY, 0);
 
        if (tdb == NULL) {
                           TDB_DEFAULT, O_RDONLY, 0);
 
        if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
+               d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
                return -1;
        }
 
                return -1;
        }
 
@@ -84,27 +83,22 @@ static int net_status_sessions(int argc, const char **argv)
        return 0;
 }
 
        return 0;
 }
 
-static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share(struct db_record *rec,
+                     const struct connections_key *key,
+                     const struct connections_data *crec,
                      void *state)
 {
                      void *state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
+       if (crec->cnum == -1)
                return 0;
 
                return 0;
 
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
                return 0;
        }
 
-       d_printf("%-10.10s   %5d   %-12s  %s",
-              crec.name,(int)crec.pid,
-              crec.machine,
-              asctime(LocalTime(&crec.start)));
+       d_printf("%-10.10s   %s   %-12s  %s",
+              crec->servicename, procid_str_static(&crec->pid),
+              crec->machine,
+              time_to_asc(crec->start));
 
        return 0;
 }
 
        return 0;
 }
@@ -125,51 +119,51 @@ static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
 
        memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
 
 
        memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
 
-       if (!process_exists(sessionid.pid))
+       if (!process_exists(sessionid.pid)) 
                return 0;
 
        ids->num_entries += 1;
                return 0;
 
        ids->num_entries += 1;
-       ids->entries = Realloc(ids->entries,
-                              sizeof(struct sessionid) * ids->num_entries);
+       ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
+       if (!ids->entries) {
+               ids->num_entries = 0;
+               return 0;
+       }
        ids->entries[ids->num_entries-1] = sessionid;
 
        return 0;
 }
 
        ids->entries[ids->num_entries-1] = sessionid;
 
        return 0;
 }
 
-static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share_parseable(struct db_record *rec,
+                               const struct connections_key *key,
+                               const struct connections_data *crec,
                                void *state)
 {
        struct sessionids *ids = (struct sessionids *)state;
                                void *state)
 {
        struct sessionids *ids = (struct sessionids *)state;
-       struct connections_data crec;
        int i;
        BOOL guest = True;
 
        int i;
        BOOL guest = True;
 
-       if (dbuf.dsize != sizeof(crec))
+       if (crec->cnum == -1)
                return 0;
 
                return 0;
 
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
-               return 0;
-
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
        for (i=0; i<ids->num_entries; i++) {
                return 0;
        }
 
        for (i=0; i<ids->num_entries; i++) {
-               if (ids->entries[i].pid == crec.pid) {
+               struct server_id id = ids->entries[i].pid;
+               if (procid_equal(&id, &crec->pid)) {
                        guest = False;
                        break;
                }
        }
 
                        guest = False;
                        break;
                }
        }
 
-       d_printf("%s\\%d\\%s\\%s\\%s\\%s\\%s",
-                crec.name,(int)crec.pid,
+       d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
+                crec->servicename,procid_str_static(&crec->pid),
                 guest ? "" : uidtoname(ids->entries[i].uid),
                 guest ? "" : gidtoname(ids->entries[i].gid),
                 guest ? "" : uidtoname(ids->entries[i].uid),
                 guest ? "" : gidtoname(ids->entries[i].gid),
-                crec.machine, 
+                crec->machine, 
                 guest ? "" : ids->entries[i].hostname,
                 guest ? "" : ids->entries[i].hostname,
-                asctime(LocalTime(&crec.start)));
+                time_to_asc(crec->start));
 
        return 0;
 }
 
        return 0;
 }
@@ -186,25 +180,14 @@ static int net_status_shares_parseable(int argc, const char **argv)
                           TDB_DEFAULT, O_RDONLY, 0);
 
        if (tdb == NULL) {
                           TDB_DEFAULT, O_RDONLY, 0);
 
        if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
+               d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
                return -1;
        }
 
        tdb_traverse(tdb, collect_pid, &ids);
        tdb_close(tdb);
 
                return -1;
        }
 
        tdb_traverse(tdb, collect_pid, &ids);
        tdb_close(tdb);
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("connections.tdb"));
-               d_printf("This is normal if no SMB client has ever connected "
-                        "to your server.\n");
-               return -1;
-       }
-
-       tdb_traverse(tdb, show_share_parseable, &ids);
-       tdb_close(tdb);
+       connections_forall(show_share_parseable, &ids);
 
        SAFE_FREE(ids.entries);
 
 
        SAFE_FREE(ids.entries);
 
@@ -213,8 +196,6 @@ static int net_status_shares_parseable(int argc, const char **argv)
 
 static int net_status_shares(int argc, const char **argv)
 {
 
 static int net_status_shares(int argc, const char **argv)
 {
-       TDB_CONTEXT *tdb;
-
        if (argc == 0) {
 
                d_printf("\nService      pid     machine       "
        if (argc == 0) {
 
                d_printf("\nService      pid     machine       "
@@ -222,19 +203,7 @@ static int net_status_shares(int argc, const char **argv)
                d_printf("-------------------------------------"
                         "------------------\n");
 
                d_printf("-------------------------------------"
                         "------------------\n");
 
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                                  TDB_DEFAULT, O_RDONLY, 0);
-
-               if (tdb == NULL) {
-                       d_printf("%s not initialised\n",
-                                lock_path("connections.tdb"));
-                       d_printf("This is normal if no SMB client has ever "
-                                "connected to your server.\n");
-                       return -1;
-               }
-
-               tdb_traverse(tdb, show_share, NULL);
-               tdb_close(tdb);
+               connections_forall(show_share, NULL);
 
                return 0;
        }
 
                return 0;
        }