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 static int show_session(struct db_record
*rec
, void *private_data
)
24 bool *parseable
= (bool *)private_data
;
25 struct sessionid sessionid
;
27 if (rec
->value
.dsize
!= sizeof(sessionid
))
30 memcpy(&sessionid
, rec
->value
.dptr
, sizeof(sessionid
));
32 if (!process_exists(sessionid
.pid
)) {
37 d_printf("%s\\%s\\%s\\%s\\%s\n",
38 procid_str_static(&sessionid
.pid
), uidtoname(sessionid
.uid
),
39 gidtoname(sessionid
.gid
),
40 sessionid
.remote_machine
, sessionid
.hostname
);
42 d_printf("%7s %-12s %-12s %-12s (%s)\n",
43 procid_str_static(&sessionid
.pid
), uidtoname(sessionid
.uid
),
44 gidtoname(sessionid
.gid
),
45 sessionid
.remote_machine
, sessionid
.hostname
);
51 static int net_status_sessions(int argc
, const char **argv
)
53 struct db_context
*db
;
58 } else if ((argc
== 1) && strequal(argv
[0], "parseable")) {
61 return net_help_status(argc
, argv
);
65 d_printf("PID Username Group Machine"
67 d_printf("-------------------------------------------"
68 "------------------------\n");
71 db
= db_open(NULL
, lock_path("sessionid.tdb"), 0,
72 TDB_CLEAR_IF_FIRST
, O_RDONLY
, 0644);
74 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
78 db
->traverse_read(db
, show_session
, &parseable
);
84 static int show_share(struct db_record
*rec
,
85 const struct connections_key
*key
,
86 const struct connections_data
*crec
,
92 if (!process_exists(crec
->pid
)) {
96 d_printf("%-10.10s %s %-12s %s",
97 crec
->servicename
, procid_str_static(&crec
->pid
),
99 time_to_asc(crec
->start
));
106 struct sessionid
*entries
;
109 static int collect_pid(struct db_record
*rec
, void *private_data
)
111 struct sessionids
*ids
= (struct sessionids
*)private_data
;
112 struct sessionid sessionid
;
114 if (rec
->value
.dsize
!= sizeof(sessionid
))
117 memcpy(&sessionid
, rec
->value
.dptr
, sizeof(sessionid
));
119 if (!process_exists(sessionid
.pid
))
122 ids
->num_entries
+= 1;
123 ids
->entries
= SMB_REALLOC_ARRAY(ids
->entries
, struct sessionid
, ids
->num_entries
);
125 ids
->num_entries
= 0;
128 ids
->entries
[ids
->num_entries
-1] = sessionid
;
133 static int show_share_parseable(struct db_record
*rec
,
134 const struct connections_key
*key
,
135 const struct connections_data
*crec
,
138 struct sessionids
*ids
= (struct sessionids
*)state
;
142 if (crec
->cnum
== -1)
145 if (!process_exists(crec
->pid
)) {
149 for (i
=0; i
<ids
->num_entries
; i
++) {
150 struct server_id id
= ids
->entries
[i
].pid
;
151 if (procid_equal(&id
, &crec
->pid
)) {
157 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
158 crec
->servicename
,procid_str_static(&crec
->pid
),
159 guest
? "" : uidtoname(ids
->entries
[i
].uid
),
160 guest
? "" : gidtoname(ids
->entries
[i
].gid
),
162 guest
? "" : ids
->entries
[i
].hostname
,
163 time_to_asc(crec
->start
));
168 static int net_status_shares_parseable(int argc
, const char **argv
)
170 struct sessionids ids
;
171 struct db_context
*db
;
176 db
= db_open(NULL
, lock_path("sessionid.tdb"), 0,
177 TDB_CLEAR_IF_FIRST
, O_RDONLY
, 0644);
179 d_fprintf(stderr
, "%s not initialised\n", lock_path("sessionid.tdb"));
183 db
->traverse_read(db
, collect_pid
, &ids
);
186 connections_forall(show_share_parseable
, &ids
);
188 SAFE_FREE(ids
.entries
);
193 static int net_status_shares(int argc
, const char **argv
)
197 d_printf("\nService pid machine "
199 d_printf("-------------------------------------"
200 "------------------\n");
202 connections_forall(show_share
, NULL
);
207 if ((argc
!= 1) || !strequal(argv
[0], "parseable")) {
208 return net_help_status(argc
, argv
);
211 return net_status_shares_parseable(argc
, argv
);
214 int net_status(int argc
, const char **argv
)
216 struct functable func
[] = {
217 {"sessions", net_status_sessions
},
218 {"shares", net_status_shares
},
221 return net_run_function(argc
, argv
, func
, net_help_status
);