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(const char *key
, struct sessionid
*session
,
34 bool *parseable
= (bool *)private_data
;
36 if (!process_exists(session
->pid
)) {
41 d_printf("%s\\%s\\%s\\%s\\%s\n",
42 procid_str_static(&session
->pid
),
43 uidtoname(session
->uid
),
44 gidtoname(session
->gid
),
45 session
->remote_machine
, session
->hostname
);
47 d_printf("%7s %-12s %-12s %-12s (%s)\n",
48 procid_str_static(&session
->pid
),
49 uidtoname(session
->uid
),
50 gidtoname(session
->gid
),
51 session
->remote_machine
, session
->hostname
);
57 static int net_status_sessions(struct net_context
*c
, int argc
, const char **argv
)
61 if (c
->display_usage
) {
63 "net status sessions [parseable]\n"
66 _("Display open user sessions.\n"
67 " If parseable is specified, output is machine-"
74 } else if ((argc
== 1) && strequal(argv
[0], "parseable")) {
77 return net_status_usage(c
, argc
, argv
);
81 d_printf(_("PID Username Group Machine"
83 "-------------------------------------------"
84 "------------------------\n"));
87 sessionid_traverse_read(show_session
, &parseable
);
91 static int show_share(struct db_record
*rec
,
92 const struct connections_key
*key
,
93 const struct connections_data
*crec
,
99 if (!process_exists(crec
->pid
)) {
103 d_printf("%-10.10s %s %-12s %s",
104 crec
->servicename
, procid_str_static(&crec
->pid
),
106 time_to_asc(crec
->start
));
113 struct sessionid
*entries
;
116 static int collect_pids(const char *key
, struct sessionid
*session
,
119 struct sessionids
*ids
= (struct sessionids
*)private_data
;
121 if (!process_exists(session
->pid
))
124 ids
->num_entries
+= 1;
125 ids
->entries
= SMB_REALLOC_ARRAY(ids
->entries
, struct sessionid
, ids
->num_entries
);
127 ids
->num_entries
= 0;
130 ids
->entries
[ids
->num_entries
-1] = *session
;
135 static int show_share_parseable(const struct connections_key
*key
,
136 const struct connections_data
*crec
,
139 struct sessionids
*ids
= (struct sessionids
*)state
;
143 if (crec
->cnum
== -1)
146 if (!process_exists(crec
->pid
)) {
150 for (i
=0; i
<ids
->num_entries
; i
++) {
151 struct server_id id
= ids
->entries
[i
].pid
;
152 if (procid_equal(&id
, &crec
->pid
)) {
158 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
159 crec
->servicename
,procid_str_static(&crec
->pid
),
160 guest
? "" : uidtoname(ids
->entries
[i
].uid
),
161 guest
? "" : gidtoname(ids
->entries
[i
].gid
),
163 guest
? "" : ids
->entries
[i
].hostname
,
164 time_to_asc(crec
->start
));
169 static int net_status_shares_parseable(struct net_context
*c
, int argc
, const char **argv
)
171 struct sessionids ids
;
176 sessionid_traverse_read(collect_pids
, &ids
);
178 connections_forall_read(show_share_parseable
, &ids
);
180 SAFE_FREE(ids
.entries
);
185 static int net_status_shares(struct net_context
*c
, int argc
, const char **argv
)
187 if (c
->display_usage
) {
189 "net status shares [parseable]\n"
192 _("Display open user shares.\n"
193 " If parseable is specified, output is machine-"
200 d_printf(_("\nService pid machine "
202 "-------------------------------------"
203 "------------------\n"));
205 connections_forall(show_share
, NULL
);
210 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
211 return net_status_usage(c
, argc
, argv
);
214 return net_status_shares_parseable(c
, argc
, argv
);
217 int net_status(struct net_context
*c
, int argc
, const char **argv
)
219 struct functable func
[] = {
224 N_("Show list of open sessions"),
225 N_("net status sessions [parseable]\n"
226 " If parseable is specified, output is presented "
227 "in a machine-parseable fashion.")
233 N_("Show list of open shares"),
234 N_("net status shares [parseable]\n"
235 " If parseable is specified, output is presented "
236 "in a machine-parseable fashion.")
238 {NULL
, NULL
, 0, NULL
, NULL
}
240 return net_run_function(c
, argc
, argv
, "net status", func
);