2 Samba Unix/Linux SMB client library
3 net status command -- possible replacement for smbstatus
4 Copyright (C) 2003 Volker Lendecke (vl@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "utils/net.h"
22 int net_status_usage(struct net_context *c, int argc, const char **argv)
24 d_printf(_(" net status sessions [parseable] "
25 "Show list of open sessions\n"));
26 d_printf(_(" net status shares [parseable] "
27 "Show list of open shares\n"));
31 static int show_session(struct db_record *rec, void *private_data)
33 bool *parseable = (bool *)private_data;
34 struct sessionid sessionid;
36 if (rec->value.dsize != sizeof(sessionid))
39 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
41 if (!process_exists(sessionid.pid)) {
46 d_printf("%s\\%s\\%s\\%s\\%s\n",
47 procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
48 gidtoname(sessionid.gid),
49 sessionid.remote_machine, sessionid.hostname);
51 d_printf("%7s %-12s %-12s %-12s (%s)\n",
52 procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
53 gidtoname(sessionid.gid),
54 sessionid.remote_machine, sessionid.hostname);
60 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
62 struct db_context *db;
65 if (c->display_usage) {
67 "net status sessions [parseable]\n"
70 _("Display open user sessions.\n"
71 " If parseable is specified, output is machine-"
78 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
81 return net_status_usage(c, argc, argv);
85 d_printf(_("PID Username Group Machine"
87 "-------------------------------------------"
88 "------------------------\n"));
91 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
92 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
94 d_fprintf(stderr, _("%s not initialised\n"),
95 lock_path("sessionid.tdb"));
99 db->traverse_read(db, show_session, &parseable);
105 static int show_share(struct db_record *rec,
106 const struct connections_key *key,
107 const struct connections_data *crec,
110 if (crec->cnum == -1)
113 if (!process_exists(crec->pid)) {
117 d_printf("%-10.10s %s %-12s %s",
118 crec->servicename, procid_str_static(&crec->pid),
120 time_to_asc(crec->start));
127 struct sessionid *entries;
130 static int collect_pid(struct db_record *rec, void *private_data)
132 struct sessionids *ids = (struct sessionids *)private_data;
133 struct sessionid sessionid;
135 if (rec->value.dsize != sizeof(sessionid))
138 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
140 if (!process_exists(sessionid.pid))
143 ids->num_entries += 1;
144 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
146 ids->num_entries = 0;
149 ids->entries[ids->num_entries-1] = sessionid;
154 static int show_share_parseable(struct db_record *rec,
155 const struct connections_key *key,
156 const struct connections_data *crec,
159 struct sessionids *ids = (struct sessionids *)state;
163 if (crec->cnum == -1)
166 if (!process_exists(crec->pid)) {
170 for (i=0; i<ids->num_entries; i++) {
171 struct server_id id = ids->entries[i].pid;
172 if (procid_equal(&id, &crec->pid)) {
178 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
179 crec->servicename,procid_str_static(&crec->pid),
180 guest ? "" : uidtoname(ids->entries[i].uid),
181 guest ? "" : gidtoname(ids->entries[i].gid),
183 guest ? "" : ids->entries[i].hostname,
184 time_to_asc(crec->start));
189 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
191 struct sessionids ids;
192 struct db_context *db;
197 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
198 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
200 d_fprintf(stderr, _("%s not initialised\n"),
201 lock_path("sessionid.tdb"));
205 db->traverse_read(db, collect_pid, &ids);
208 connections_forall(show_share_parseable, &ids);
210 SAFE_FREE(ids.entries);
215 static int net_status_shares(struct net_context *c, int argc, const char **argv)
217 if (c->display_usage) {
219 "net status shares [parseable]\n"
222 _("Display open user shares.\n"
223 " If parseable is specified, output is machine-"
230 d_printf(_("\nService pid machine "
232 "-------------------------------------"
233 "------------------\n"));
235 connections_forall(show_share, NULL);
240 if ((argc != 1) || !strequal(argv[0], "parseable")) {
241 return net_status_usage(c, argc, argv);
244 return net_status_shares_parseable(c, argc, argv);
247 int net_status(struct net_context *c, int argc, const char **argv)
249 struct functable func[] = {
254 N_("Show list of open sessions"),
255 N_("net status sessions [parseable]\n"
256 " If parseable is specified, output is presented "
257 "in a machine-parseable fashion.")
263 N_("Show list of open shares"),
264 N_("net status shares [parseable]\n"
265 " If parseable is specified, output is presented "
266 "in a machine-parseable fashion.")
268 {NULL, NULL, 0, NULL, NULL}
270 return net_run_function(c, argc, argv, "net status", func);