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 struct server_id_buf tmp;
38 bool *parseable = (bool *)private_data;
40 if (!process_exists(session->pid)) {
45 d_printf("%s\\%s\\%s\\%s\\%s\n",
46 server_id_str_buf(session->pid, &tmp),
47 uidtoname(session->uid),
48 gidtoname(session->gid),
49 session->remote_machine, session->hostname);
51 d_printf("%7s %-12s %-12s %-12s (%s)\n",
52 server_id_str_buf(session->pid, &tmp),
53 uidtoname(session->uid),
54 gidtoname(session->gid),
55 session->remote_machine, session->hostname);
61 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
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 sessionid_traverse_read(show_session, &parseable);
95 static int show_share(const struct connections_key *key,
96 const struct connections_data *crec,
99 struct server_id_buf tmp;
101 if (crec->cnum == TID_FIELD_INVALID)
104 if (!process_exists(crec->pid)) {
108 d_printf("%-10.10s %s %-12s %s",
109 crec->servicename, server_id_str_buf(crec->pid, &tmp),
111 time_to_asc(crec->start));
118 struct sessionid *entries;
121 static int collect_pids(const char *key, struct sessionid *session,
124 struct sessionids *ids = (struct sessionids *)private_data;
126 if (!process_exists(session->pid))
129 ids->num_entries += 1;
130 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
132 ids->num_entries = 0;
135 ids->entries[ids->num_entries-1] = *session;
140 static int show_share_parseable(const struct connections_key *key,
141 const struct connections_data *crec,
144 struct sessionids *ids = (struct sessionids *)state;
145 struct server_id_buf tmp;
149 if (crec->cnum == TID_FIELD_INVALID)
152 if (!process_exists(crec->pid)) {
156 for (i=0; i<ids->num_entries; i++) {
157 struct server_id id = ids->entries[i].pid;
158 if (serverid_equal(&id, &crec->pid)) {
164 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
165 crec->servicename, server_id_str_buf(crec->pid, &tmp),
166 guest ? "" : uidtoname(ids->entries[i].uid),
167 guest ? "" : gidtoname(ids->entries[i].gid),
169 guest ? "" : ids->entries[i].hostname,
170 time_to_asc(crec->start));
175 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
177 struct sessionids ids;
182 sessionid_traverse_read(collect_pids, &ids);
184 connections_forall_read(show_share_parseable, &ids);
186 SAFE_FREE(ids.entries);
191 static int net_status_shares(struct net_context *c, int argc, const char **argv)
193 if (c->display_usage) {
195 "net status shares [parseable]\n"
198 _("Display open user shares.\n"
199 " If parseable is specified, output is machine-"
206 d_printf(_("\nService pid machine "
208 "-------------------------------------"
209 "------------------\n"));
211 connections_forall_read(show_share, NULL);
216 if ((argc != 1) || !strequal(argv[0], "parseable")) {
217 return net_status_usage(c, argc, argv);
220 return net_status_shares_parseable(c, argc, argv);
223 int net_status(struct net_context *c, int argc, const char **argv)
225 struct functable func[] = {
230 N_("Show list of open sessions"),
231 N_("net status sessions [parseable]\n"
232 " If parseable is specified, output is presented "
233 "in a machine-parseable fashion.")
239 N_("Show list of open shares"),
240 N_("net status shares [parseable]\n"
241 " If parseable is specified, output is presented "
242 "in a machine-parseable fashion.")
244 {NULL, NULL, 0, NULL, NULL}
246 return net_run_function(c, argc, argv, "net status", func);