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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "utils/net.h"
23 static int show_session(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
26 BOOL
*parseable
= (BOOL
*)state
;
27 struct sessionid sessionid
;
29 if (dbuf
.dsize
!= sizeof(sessionid
))
32 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
34 if (!process_exists(sessionid
.pid
)) {
39 d_printf("%d\\%s\\%s\\%s\\%s\n",
40 (int)sessionid
.pid
, uidtoname(sessionid
.uid
),
41 gidtoname(sessionid
.gid
),
42 sessionid
.remote_machine
, sessionid
.hostname
);
44 d_printf("%5d %-12s %-12s %-12s (%s)\n",
45 (int)sessionid
.pid
, uidtoname(sessionid
.uid
),
46 gidtoname(sessionid
.gid
),
47 sessionid
.remote_machine
, sessionid
.hostname
);
53 static int net_status_sessions(int argc
, const char **argv
)
60 } else if ((argc
== 1) && strequal(argv
[0], "parseable")) {
63 return net_help_status(argc
, argv
);
67 d_printf("PID Username Group Machine"
69 d_printf("-------------------------------------------"
70 "------------------------\n");
73 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
74 TDB_DEFAULT
, O_RDONLY
, 0);
77 d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
81 tdb_traverse(tdb
, show_session
, &parseable
);
87 static int show_share(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
90 struct connections_data crec
;
92 if (dbuf
.dsize
!= sizeof(crec
))
95 memcpy(&crec
, dbuf
.dptr
, sizeof(crec
));
100 if (!process_exists(crec
.pid
)) {
104 d_printf("%-10.10s %5d %-12s %s",
105 crec
.name
,(int)crec
.pid
,
107 asctime(LocalTime(&crec
.start
)));
114 struct sessionid
*entries
;
117 static int collect_pid(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
120 struct sessionids
*ids
= (struct sessionids
*)state
;
121 struct sessionid sessionid
;
123 if (dbuf
.dsize
!= sizeof(sessionid
))
126 memcpy(&sessionid
, dbuf
.dptr
, sizeof(sessionid
));
128 if (!process_exists(sessionid
.pid
))
131 ids
->num_entries
+= 1;
132 ids
->entries
= SMB_REALLOC_ARRAY(ids
->entries
, struct sessionid
, ids
->num_entries
);
133 ids
->entries
[ids
->num_entries
-1] = sessionid
;
138 static int show_share_parseable(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
141 struct sessionids
*ids
= (struct sessionids
*)state
;
142 struct connections_data crec
;
146 if (dbuf
.dsize
!= sizeof(crec
))
149 memcpy(&crec
, dbuf
.dptr
, sizeof(crec
));
154 if (!process_exists(crec
.pid
)) {
158 for (i
=0; i
<ids
->num_entries
; i
++) {
159 if (ids
->entries
[i
].pid
== crec
.pid
) {
165 d_printf("%s\\%d\\%s\\%s\\%s\\%s\\%s",
166 crec
.name
,(int)crec
.pid
,
167 guest
? "" : uidtoname(ids
->entries
[i
].uid
),
168 guest
? "" : gidtoname(ids
->entries
[i
].gid
),
170 guest
? "" : ids
->entries
[i
].hostname
,
171 asctime(LocalTime(&crec
.start
)));
176 static int net_status_shares_parseable(int argc
, const char **argv
)
178 struct sessionids ids
;
184 tdb
= tdb_open_log(lock_path("sessionid.tdb"), 0,
185 TDB_DEFAULT
, O_RDONLY
, 0);
188 d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
192 tdb_traverse(tdb
, collect_pid
, &ids
);
195 tdb
= tdb_open_log(lock_path("connections.tdb"), 0,
196 TDB_DEFAULT
, O_RDONLY
, 0);
199 d_printf("%s not initialised\n", lock_path("connections.tdb"));
200 d_printf("This is normal if no SMB client has ever connected "
201 "to your server.\n");
205 tdb_traverse(tdb
, show_share_parseable
, &ids
);
208 SAFE_FREE(ids
.entries
);
213 static int net_status_shares(int argc
, const char **argv
)
219 d_printf("\nService pid machine "
221 d_printf("-------------------------------------"
222 "------------------\n");
224 tdb
= tdb_open_log(lock_path("connections.tdb"), 0,
225 TDB_DEFAULT
, O_RDONLY
, 0);
228 d_printf("%s not initialised\n",
229 lock_path("connections.tdb"));
230 d_printf("This is normal if no SMB client has ever "
231 "connected to your server.\n");
235 tdb_traverse(tdb
, show_share
, NULL
);
241 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
242 return net_help_status(argc
, argv
);
245 return net_status_shares_parseable(argc
, argv
);
248 int net_status(int argc
, const char **argv
)
250 struct functable func
[] = {
251 {"sessions", net_status_sessions
},
252 {"shares", net_status_shares
},
255 return net_run_function(argc
, argv
, func
, net_help_status
);