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 "lib/util/server_id.h"
21 #include "utils/net.h"
24 #include "lib/conn_tdb.h"
26 int net_status_usage(struct net_context
*c
, int argc
, const char **argv
)
28 d_printf(_(" net status sessions [parseable] "
29 "Show list of open sessions\n"));
30 d_printf(_(" net status shares [parseable] "
31 "Show list of open shares\n"));
35 static int show_session(const char *key
, struct sessionid
*session
,
38 struct server_id_buf tmp
;
39 bool *parseable
= (bool *)private_data
;
41 if (!process_exists(session
->pid
)) {
46 d_printf("%s\\%s\\%s\\%s\\%s\n",
47 server_id_str_buf(session
->pid
, &tmp
),
48 uidtoname(session
->uid
),
49 gidtoname(session
->gid
),
50 session
->remote_machine
, session
->hostname
);
52 d_printf("%7s %-12s %-12s %-12s (%s)\n",
53 server_id_str_buf(session
->pid
, &tmp
),
54 uidtoname(session
->uid
),
55 gidtoname(session
->gid
),
56 session
->remote_machine
, session
->hostname
);
62 static int net_status_sessions(struct net_context
*c
, int argc
, const char **argv
)
66 if (c
->display_usage
) {
68 "net status sessions [parseable]\n"
71 _("Display open user sessions.\n"
72 " If parseable is specified, output is machine-"
79 } else if ((argc
== 1) && strequal(argv
[0], "parseable")) {
82 return net_status_usage(c
, argc
, argv
);
86 d_printf(_("PID Username Group Machine"
88 "-------------------------------------------"
89 "------------------------\n"));
92 sessionid_traverse_read(show_session
, &parseable
);
96 static int show_share(const struct connections_key
*key
,
97 const struct connections_data
*crec
,
100 struct server_id_buf tmp
;
102 if (crec
->cnum
== TID_FIELD_INVALID
)
105 if (!process_exists(crec
->pid
)) {
109 d_printf("%-10.10s %s %-12s %s",
110 crec
->servicename
, server_id_str_buf(crec
->pid
, &tmp
),
112 time_to_asc(crec
->start
));
119 struct sessionid
*entries
;
122 static int collect_pids(const char *key
, struct sessionid
*session
,
125 struct sessionids
*ids
= (struct sessionids
*)private_data
;
127 if (!process_exists(session
->pid
))
130 ids
->num_entries
+= 1;
131 ids
->entries
= SMB_REALLOC_ARRAY(ids
->entries
, struct sessionid
, ids
->num_entries
);
133 ids
->num_entries
= 0;
136 ids
->entries
[ids
->num_entries
-1] = *session
;
141 static int show_share_parseable(const struct connections_key
*key
,
142 const struct connections_data
*crec
,
145 struct sessionids
*ids
= (struct sessionids
*)state
;
146 struct server_id_buf tmp
;
150 if (crec
->cnum
== TID_FIELD_INVALID
)
153 if (!process_exists(crec
->pid
)) {
157 for (i
=0; i
<ids
->num_entries
; i
++) {
158 struct server_id id
= ids
->entries
[i
].pid
;
159 if (serverid_equal(&id
, &crec
->pid
)) {
165 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
166 crec
->servicename
, server_id_str_buf(crec
->pid
, &tmp
),
167 guest
? "" : uidtoname(ids
->entries
[i
].uid
),
168 guest
? "" : gidtoname(ids
->entries
[i
].gid
),
170 guest
? "" : ids
->entries
[i
].hostname
,
171 time_to_asc(crec
->start
));
176 static int net_status_shares_parseable(struct net_context
*c
, int argc
, const char **argv
)
178 struct sessionids ids
;
183 sessionid_traverse_read(collect_pids
, &ids
);
185 connections_forall_read(show_share_parseable
, &ids
);
187 SAFE_FREE(ids
.entries
);
192 static int net_status_shares(struct net_context
*c
, int argc
, const char **argv
)
194 if (c
->display_usage
) {
196 "net status shares [parseable]\n"
199 _("Display open user shares.\n"
200 " If parseable is specified, output is machine-"
207 d_printf(_("\nService pid machine "
209 "-------------------------------------"
210 "------------------\n"));
212 connections_forall_read(show_share
, NULL
);
217 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
218 return net_status_usage(c
, argc
, argv
);
221 return net_status_shares_parseable(c
, argc
, argv
);
224 int net_status(struct net_context
*c
, int argc
, const char **argv
)
226 struct functable func
[] = {
231 N_("Show list of open sessions"),
232 N_("net status sessions [parseable]\n"
233 " If parseable is specified, output is presented "
234 "in a machine-parseable fashion.")
240 N_("Show list of open shares"),
241 N_("net status shares [parseable]\n"
242 " If parseable is specified, output is presented "
243 "in a machine-parseable fashion.")
245 {NULL
, NULL
, 0, NULL
, NULL
}
247 return net_run_function(c
, argc
, argv
, "net status", func
);