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"
23 #include "lib/conn_tdb.h"
25 int net_status_usage(struct net_context *c, int argc, const char **argv)
27 d_printf(_(" net status sessions [parseable] "
28 "Show list of open sessions\n"));
29 d_printf(_(" net status shares [parseable] "
30 "Show list of open shares\n"));
34 static int show_session(const char *key, struct sessionid *session,
37 bool *parseable = (bool *)private_data;
39 if (!process_exists(session->pid)) {
44 d_printf("%s\\%s\\%s\\%s\\%s\n",
45 procid_str_static(&session->pid),
46 uidtoname(session->uid),
47 gidtoname(session->gid),
48 session->remote_machine, session->hostname);
50 d_printf("%7s %-12s %-12s %-12s (%s)\n",
51 procid_str_static(&session->pid),
52 uidtoname(session->uid),
53 gidtoname(session->gid),
54 session->remote_machine, session->hostname);
60 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
64 if (c->display_usage) {
66 "net status sessions [parseable]\n"
69 _("Display open user sessions.\n"
70 " If parseable is specified, output is machine-"
77 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
80 return net_status_usage(c, argc, argv);
84 d_printf(_("PID Username Group Machine"
86 "-------------------------------------------"
87 "------------------------\n"));
90 sessionid_traverse_read(show_session, &parseable);
94 static int show_share(const struct connections_key *key,
95 const struct connections_data *crec,
98 if (crec->cnum == TID_FIELD_INVALID)
101 if (!process_exists(crec->pid)) {
105 d_printf("%-10.10s %s %-12s %s",
106 crec->servicename, procid_str_static(&crec->pid),
108 time_to_asc(crec->start));
115 struct sessionid *entries;
118 static int collect_pids(const char *key, struct sessionid *session,
121 struct sessionids *ids = (struct sessionids *)private_data;
123 if (!process_exists(session->pid))
126 ids->num_entries += 1;
127 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
129 ids->num_entries = 0;
132 ids->entries[ids->num_entries-1] = *session;
137 static int show_share_parseable(const struct connections_key *key,
138 const struct connections_data *crec,
141 struct sessionids *ids = (struct sessionids *)state;
145 if (crec->cnum == TID_FIELD_INVALID)
148 if (!process_exists(crec->pid)) {
152 for (i=0; i<ids->num_entries; i++) {
153 struct server_id id = ids->entries[i].pid;
154 if (serverid_equal(&id, &crec->pid)) {
160 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
161 crec->servicename,procid_str_static(&crec->pid),
162 guest ? "" : uidtoname(ids->entries[i].uid),
163 guest ? "" : gidtoname(ids->entries[i].gid),
165 guest ? "" : ids->entries[i].hostname,
166 time_to_asc(crec->start));
171 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
173 struct sessionids ids;
178 sessionid_traverse_read(collect_pids, &ids);
180 connections_forall_read(show_share_parseable, &ids);
182 SAFE_FREE(ids.entries);
187 static int net_status_shares(struct net_context *c, int argc, const char **argv)
189 if (c->display_usage) {
191 "net status shares [parseable]\n"
194 _("Display open user shares.\n"
195 " If parseable is specified, output is machine-"
202 d_printf(_("\nService pid machine "
204 "-------------------------------------"
205 "------------------\n"));
207 connections_forall_read(show_share, NULL);
212 if ((argc != 1) || !strequal(argv[0], "parseable")) {
213 return net_status_usage(c, argc, argv);
216 return net_status_shares_parseable(c, argc, argv);
219 int net_status(struct net_context *c, int argc, const char **argv)
221 struct functable func[] = {
226 N_("Show list of open sessions"),
227 N_("net status sessions [parseable]\n"
228 " If parseable is specified, output is presented "
229 "in a machine-parseable fashion.")
235 N_("Show list of open shares"),
236 N_("net status shares [parseable]\n"
237 " If parseable is specified, output is presented "
238 "in a machine-parseable fashion.")
240 {NULL, NULL, 0, NULL, NULL}
242 return net_run_function(c, argc, argv, "net status", func);