Apply my experimental aliases support to HEAD. This will be a bit difficult to
[Samba/gebeck_regimport.git] / source / utils / net_status.c
blob0543f457cfc0061cba5d80e3afce4a1a4b87d09f
1 /*
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. */
20 #include "includes.h"
21 #include "../utils/net.h"
23 static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
24 void *state)
26 BOOL *parseable = (BOOL *)state;
27 struct sessionid sessionid;
29 if (dbuf.dsize != sizeof(sessionid))
30 return 0;
32 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
34 if (!process_exists(sessionid.pid)) {
35 return 0;
38 if (*parseable) {
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);
43 } else {
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);
50 return 0;
53 static int net_status_sessions(int argc, const char **argv)
55 TDB_CONTEXT *tdb;
56 BOOL parseable;
58 if (argc == 0) {
59 parseable = False;
60 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
61 parseable = True;
62 } else {
63 return net_help_status(argc, argv);
66 if (!parseable) {
67 d_printf("PID Username Group Machine"
68 " \n");
69 d_printf("-------------------------------------------"
70 "------------------------\n");
73 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
74 TDB_DEFAULT, O_RDONLY, 0);
76 if (tdb == NULL) {
77 d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
78 return -1;
81 tdb_traverse(tdb, show_session, &parseable);
82 tdb_close(tdb);
84 return 0;
87 static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
88 void *state)
90 struct connections_data crec;
92 if (dbuf.dsize != sizeof(crec))
93 return 0;
95 memcpy(&crec, dbuf.dptr, sizeof(crec));
97 if (crec.cnum == -1)
98 return 0;
100 if (!process_exists(crec.pid)) {
101 return 0;
104 d_printf("%-10.10s %5d %-12s %s",
105 crec.name,(int)crec.pid,
106 crec.machine,
107 asctime(LocalTime(&crec.start)));
109 return 0;
112 struct sessionids {
113 int num_entries;
114 struct sessionid *entries;
117 static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
118 void *state)
120 struct sessionids *ids = (struct sessionids *)state;
121 struct sessionid sessionid;
123 if (dbuf.dsize != sizeof(sessionid))
124 return 0;
126 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
128 if (!process_exists(sessionid.pid))
129 return 0;
131 ids->num_entries += 1;
132 ids->entries = Realloc(ids->entries,
133 sizeof(struct sessionid) * ids->num_entries);
134 ids->entries[ids->num_entries-1] = sessionid;
136 return 0;
139 static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
140 void *state)
142 struct sessionids *ids = (struct sessionids *)state;
143 struct connections_data crec;
144 int i;
145 BOOL guest = True;
147 if (dbuf.dsize != sizeof(crec))
148 return 0;
150 memcpy(&crec, dbuf.dptr, sizeof(crec));
152 if (crec.cnum == -1)
153 return 0;
155 if (!process_exists(crec.pid)) {
156 return 0;
159 for (i=0; i<ids->num_entries; i++) {
160 if (ids->entries[i].pid == crec.pid) {
161 guest = False;
162 break;
166 d_printf("%s\\%d\\%s\\%s\\%s\\%s\\%s",
167 crec.name,(int)crec.pid,
168 guest ? "" : uidtoname(ids->entries[i].uid),
169 guest ? "" : gidtoname(ids->entries[i].gid),
170 crec.machine,
171 guest ? "" : ids->entries[i].hostname,
172 asctime(LocalTime(&crec.start)));
174 return 0;
177 static int net_status_shares_parseable(int argc, const char **argv)
179 struct sessionids ids;
180 TDB_CONTEXT *tdb;
182 ids.num_entries = 0;
183 ids.entries = NULL;
185 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
186 TDB_DEFAULT, O_RDONLY, 0);
188 if (tdb == NULL) {
189 d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
190 return -1;
193 tdb_traverse(tdb, collect_pid, &ids);
194 tdb_close(tdb);
196 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
197 TDB_DEFAULT, O_RDONLY, 0);
199 if (tdb == NULL) {
200 d_printf("%s not initialised\n", lock_path("connections.tdb"));
201 d_printf("This is normal if no SMB client has ever connected "
202 "to your server.\n");
203 return -1;
206 tdb_traverse(tdb, show_share_parseable, &ids);
207 tdb_close(tdb);
209 SAFE_FREE(ids.entries);
211 return 0;
214 static int net_status_shares(int argc, const char **argv)
216 TDB_CONTEXT *tdb;
218 if (argc == 0) {
220 d_printf("\nService pid machine "
221 "Connected at\n");
222 d_printf("-------------------------------------"
223 "------------------\n");
225 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
226 TDB_DEFAULT, O_RDONLY, 0);
228 if (tdb == NULL) {
229 d_printf("%s not initialised\n",
230 lock_path("connections.tdb"));
231 d_printf("This is normal if no SMB client has ever "
232 "connected to your server.\n");
233 return -1;
236 tdb_traverse(tdb, show_share, NULL);
237 tdb_close(tdb);
239 return 0;
242 if ((argc != 1) || !strequal(argv[0], "parseable")) {
243 return net_help_status(argc, argv);
246 return net_status_shares_parseable(argc, argv);
249 int net_status(int argc, const char **argv)
251 struct functable func[] = {
252 {"sessions", net_status_sessions},
253 {"shares", net_status_shares},
254 {NULL, NULL}
256 return net_run_function(argc, argv, func, net_help_status);