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(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
34 bool *parseable
= (bool *)state
;
35 struct sessionid sessionid
;
37 if (dbuf
.dsize
!= sizeof(sessionid
))
40 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
42 if (!process_exists(sessionid
.pid
)) {
47 d_printf("%s\\%s\\%s\\%s\\%s\n",
48 procid_str_static(&sessionid
.pid
), uidtoname(sessionid
.uid
),
49 gidtoname(sessionid
.gid
),
50 sessionid
.remote_machine
, sessionid
.hostname
);
52 d_printf("%7s %-12s %-12s %-12s (%s)\n",
53 procid_str_static(&sessionid
.pid
), uidtoname(sessionid
.uid
),
54 gidtoname(sessionid
.gid
),
55 sessionid
.remote_machine
, sessionid
.hostname
);
61 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"
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 d_printf("-------------------------------------------"
87 "------------------------\n");
90 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
91 TDB_DEFAULT
, O_RDONLY
, 0);
94 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
98 tdb_traverse(tdb
, show_session
, &parseable
);
104 static int show_share(struct db_record
*rec
,
105 const struct connections_key
*key
,
106 const struct connections_data
*crec
,
109 if (crec
->cnum
== -1)
112 if (!process_exists(crec
->pid
)) {
116 d_printf("%-10.10s %s %-12s %s",
117 crec
->servicename
, procid_str_static(&crec
->pid
),
119 time_to_asc(crec
->start
));
126 struct sessionid
*entries
;
129 static int collect_pid(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
132 struct sessionids
*ids
= (struct sessionids
*)state
;
133 struct sessionid sessionid
;
135 if (dbuf
.dsize
!= sizeof(sessionid
))
138 memcpy(&sessionid
, dbuf
.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
;
197 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
198 TDB_DEFAULT
, O_RDONLY
, 0);
201 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
205 tdb_traverse(tdb
, 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"
220 " Display open user shares.\n"
221 " If parseable is specified, output is machine-"
228 d_printf("\nService pid machine "
230 d_printf("-------------------------------------"
231 "------------------\n");
233 connections_forall(show_share
, NULL
);
238 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
239 return net_status_usage(c
, argc
, argv
);
242 return net_status_shares_parseable(c
, argc
, argv
);
245 int net_status(struct net_context
*c
, int argc
, const char **argv
)
247 struct functable func
[] = {
252 "Show list of open sessions",
253 "net status sessions [parseable]\n"
254 " If parseable is specified, output is presented "
255 "in a machine-parseable fashion."
261 "Show list of open shares",
262 "net status shares [parseable]\n"
263 " If parseable is specified, output is presented "
264 "in a machine-parseable fashion."
266 {NULL
, NULL
, 0, NULL
, NULL
}
268 return net_run_function(c
, argc
, argv
, "net status", func
);