net: Move help for "net status" to net_status.c
[Samba/vfs_proxy.git] / source3 / utils / net_status.c
blob2835b2084d92af3503ccbc176e424ba07017a078
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 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/>. */
19 #include "includes.h"
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");
28 return -1;
31 static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
32 void *state)
34 bool *parseable = (bool *)state;
35 struct sessionid sessionid;
37 if (dbuf.dsize != sizeof(sessionid))
38 return 0;
40 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
42 if (!process_exists(sessionid.pid)) {
43 return 0;
46 if (*parseable) {
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);
51 } else {
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);
58 return 0;
61 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
63 TDB_CONTEXT *tdb;
64 bool parseable;
66 if (argc == 0) {
67 parseable = false;
68 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
69 parseable = true;
70 } else {
71 return net_status_usage(c, argc, argv);
74 if (!parseable) {
75 d_printf("PID Username Group Machine"
76 " \n");
77 d_printf("-------------------------------------------"
78 "------------------------\n");
81 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
82 TDB_DEFAULT, O_RDONLY, 0);
84 if (tdb == NULL) {
85 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
86 return -1;
89 tdb_traverse(tdb, show_session, &parseable);
90 tdb_close(tdb);
92 return 0;
95 static int show_share(struct db_record *rec,
96 const struct connections_key *key,
97 const struct connections_data *crec,
98 void *state)
100 if (crec->cnum == -1)
101 return 0;
103 if (!process_exists(crec->pid)) {
104 return 0;
107 d_printf("%-10.10s %s %-12s %s",
108 crec->servicename, procid_str_static(&crec->pid),
109 crec->machine,
110 time_to_asc(crec->start));
112 return 0;
115 struct sessionids {
116 int num_entries;
117 struct sessionid *entries;
120 static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
121 void *state)
123 struct sessionids *ids = (struct sessionids *)state;
124 struct sessionid sessionid;
126 if (dbuf.dsize != sizeof(sessionid))
127 return 0;
129 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
131 if (!process_exists(sessionid.pid))
132 return 0;
134 ids->num_entries += 1;
135 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
136 if (!ids->entries) {
137 ids->num_entries = 0;
138 return 0;
140 ids->entries[ids->num_entries-1] = sessionid;
142 return 0;
145 static int show_share_parseable(struct db_record *rec,
146 const struct connections_key *key,
147 const struct connections_data *crec,
148 void *state)
150 struct sessionids *ids = (struct sessionids *)state;
151 int i;
152 bool guest = true;
154 if (crec->cnum == -1)
155 return 0;
157 if (!process_exists(crec->pid)) {
158 return 0;
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)) {
164 guest = false;
165 break;
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),
173 crec->machine,
174 guest ? "" : ids->entries[i].hostname,
175 time_to_asc(crec->start));
177 return 0;
180 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
182 struct sessionids ids;
183 TDB_CONTEXT *tdb;
185 ids.num_entries = 0;
186 ids.entries = NULL;
188 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
189 TDB_DEFAULT, O_RDONLY, 0);
191 if (tdb == NULL) {
192 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
193 return -1;
196 tdb_traverse(tdb, collect_pid, &ids);
197 tdb_close(tdb);
199 connections_forall(show_share_parseable, &ids);
201 SAFE_FREE(ids.entries);
203 return 0;
206 static int net_status_shares(struct net_context *c, int argc, const char **argv)
208 if (argc == 0) {
210 d_printf("\nService pid machine "
211 "Connected at\n");
212 d_printf("-------------------------------------"
213 "------------------\n");
215 connections_forall(show_share, NULL);
217 return 0;
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},
233 {NULL, NULL}
235 return net_run_function(c, argc, argv, func, net_status_usage);