Add comment explaining the previous fix.
[Samba.git] / source / utils / net_status.c
blob3e6d3fde4ebf2a5cf57bd862873b0aded90d10e5
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 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))
28 return 0;
30 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
32 if (!process_exists(sessionid.pid)) {
33 return 0;
36 if (*parseable) {
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);
41 } else {
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);
48 return 0;
51 static int net_status_sessions(int argc, const char **argv)
53 struct db_context *db;
54 bool parseable;
56 if (argc == 0) {
57 parseable = False;
58 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
59 parseable = True;
60 } else {
61 return net_help_status(argc, argv);
64 if (!parseable) {
65 d_printf("PID Username Group Machine"
66 " \n");
67 d_printf("-------------------------------------------"
68 "------------------------\n");
71 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
72 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
73 if (db == NULL) {
74 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
75 return -1;
78 db->traverse_read(db, show_session, &parseable);
79 TALLOC_FREE(db);
81 return 0;
84 static int show_share(struct db_record *rec,
85 const struct connections_key *key,
86 const struct connections_data *crec,
87 void *state)
89 if (crec->cnum == -1)
90 return 0;
92 if (!process_exists(crec->pid)) {
93 return 0;
96 d_printf("%-10.10s %s %-12s %s",
97 crec->servicename, procid_str_static(&crec->pid),
98 crec->machine,
99 time_to_asc(crec->start));
101 return 0;
104 struct sessionids {
105 int num_entries;
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))
115 return 0;
117 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
119 if (!process_exists(sessionid.pid))
120 return 0;
122 ids->num_entries += 1;
123 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
124 if (!ids->entries) {
125 ids->num_entries = 0;
126 return 0;
128 ids->entries[ids->num_entries-1] = sessionid;
130 return 0;
133 static int show_share_parseable(struct db_record *rec,
134 const struct connections_key *key,
135 const struct connections_data *crec,
136 void *state)
138 struct sessionids *ids = (struct sessionids *)state;
139 int i;
140 bool guest = True;
142 if (crec->cnum == -1)
143 return 0;
145 if (!process_exists(crec->pid)) {
146 return 0;
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)) {
152 guest = False;
153 break;
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),
161 crec->machine,
162 guest ? "" : ids->entries[i].hostname,
163 time_to_asc(crec->start));
165 return 0;
168 static int net_status_shares_parseable(int argc, const char **argv)
170 struct sessionids ids;
171 struct db_context *db;
173 ids.num_entries = 0;
174 ids.entries = NULL;
176 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
177 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
178 if (db == NULL) {
179 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
180 return -1;
183 db->traverse_read(db, collect_pid, &ids);
184 TALLOC_FREE(db);
186 connections_forall(show_share_parseable, &ids);
188 SAFE_FREE(ids.entries);
190 return 0;
193 static int net_status_shares(int argc, const char **argv)
195 if (argc == 0) {
197 d_printf("\nService pid machine "
198 "Connected at\n");
199 d_printf("-------------------------------------"
200 "------------------\n");
202 connections_forall(show_share, NULL);
204 return 0;
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},
219 {NULL, NULL}
221 return net_run_function(argc, argv, func, net_help_status);