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
)
68 } else if ((argc
== 1) && strequal(argv
[0], "parseable")) {
71 return net_status_usage(c
, argc
, argv
);
75 d_printf("PID Username Group Machine"
77 d_printf("-------------------------------------------"
78 "------------------------\n");
81 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
82 TDB_DEFAULT
, O_RDONLY
, 0);
85 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
89 tdb_traverse(tdb
, show_session
, &parseable
);
95 static int show_share(struct db_record
*rec
,
96 const struct connections_key
*key
,
97 const struct connections_data
*crec
,
100 if (crec
->cnum
== -1)
103 if (!process_exists(crec
->pid
)) {
107 d_printf("%-10.10s %s %-12s %s",
108 crec
->servicename
, procid_str_static(&crec
->pid
),
110 time_to_asc(crec
->start
));
117 struct sessionid
*entries
;
120 static int collect_pid(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
123 struct sessionids
*ids
= (struct sessionids
*)state
;
124 struct sessionid sessionid
;
126 if (dbuf
.dsize
!= sizeof(sessionid
))
129 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
131 if (!process_exists(sessionid
.pid
))
134 ids
->num_entries
+= 1;
135 ids
->entries
= SMB_REALLOC_ARRAY(ids
->entries
, struct sessionid
, ids
->num_entries
);
137 ids
->num_entries
= 0;
140 ids
->entries
[ids
->num_entries
-1] = sessionid
;
145 static int show_share_parseable(struct db_record
*rec
,
146 const struct connections_key
*key
,
147 const struct connections_data
*crec
,
150 struct sessionids
*ids
= (struct sessionids
*)state
;
154 if (crec
->cnum
== -1)
157 if (!process_exists(crec
->pid
)) {
161 for (i
=0; i
<ids
->num_entries
; i
++) {
162 struct server_id id
= ids
->entries
[i
].pid
;
163 if (procid_equal(&id
, &crec
->pid
)) {
169 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
170 crec
->servicename
,procid_str_static(&crec
->pid
),
171 guest
? "" : uidtoname(ids
->entries
[i
].uid
),
172 guest
? "" : gidtoname(ids
->entries
[i
].gid
),
174 guest
? "" : ids
->entries
[i
].hostname
,
175 time_to_asc(crec
->start
));
180 static int net_status_shares_parseable(struct net_context
*c
, int argc
, const char **argv
)
182 struct sessionids ids
;
188 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
189 TDB_DEFAULT
, O_RDONLY
, 0);
192 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
196 tdb_traverse(tdb
, collect_pid
, &ids
);
199 connections_forall(show_share_parseable
, &ids
);
201 SAFE_FREE(ids
.entries
);
206 static int net_status_shares(struct net_context
*c
, int argc
, const char **argv
)
210 d_printf("\nService pid machine "
212 d_printf("-------------------------------------"
213 "------------------\n");
215 connections_forall(show_share
, NULL
);
220 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
221 return net_status_usage(c
, argc
, argv
);
224 return net_status_shares_parseable(c
, argc
, argv
);
227 int net_status(struct net_context
*c
, int argc
, const char **argv
)
229 struct functable func
[] = {
230 {"sessions", net_status_sessions
},
231 {"shares", net_status_shares
},
232 {"help", net_status_usage
},
235 return net_run_function(c
, argc
, argv
, func
, net_status_usage
);