s4-drs: additional delete test cases
[Samba/eduardoll.git] / source3 / utils / net_status.c
blob54ad7864364bdf45b9a583276771043e676d62cc
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(const char *key, struct sessionid *session,
32 void *private_data)
34 bool *parseable = (bool *)private_data;
36 if (!process_exists(session->pid)) {
37 return 0;
40 if (*parseable) {
41 d_printf("%s\\%s\\%s\\%s\\%s\n",
42 procid_str_static(&session->pid),
43 uidtoname(session->uid),
44 gidtoname(session->gid),
45 session->remote_machine, session->hostname);
46 } else {
47 d_printf("%7s %-12s %-12s %-12s (%s)\n",
48 procid_str_static(&session->pid),
49 uidtoname(session->uid),
50 gidtoname(session->gid),
51 session->remote_machine, session->hostname);
54 return 0;
57 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
59 bool parseable;
61 if (c->display_usage) {
62 d_printf( "%s\n"
63 "net status sessions [parseable]\n"
64 " %s\n",
65 _("Usage:"),
66 _("Display open user sessions.\n"
67 " If parseable is specified, output is machine-"
68 "readable."));
69 return 0;
72 if (argc == 0) {
73 parseable = false;
74 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
75 parseable = true;
76 } else {
77 return net_status_usage(c, argc, argv);
80 if (!parseable) {
81 d_printf(_("PID Username Group Machine"
82 " \n"
83 "-------------------------------------------"
84 "------------------------\n"));
87 sessionid_traverse_read(show_session, &parseable);
88 return 0;
91 static int show_share(struct db_record *rec,
92 const struct connections_key *key,
93 const struct connections_data *crec,
94 void *state)
96 if (crec->cnum == -1)
97 return 0;
99 if (!process_exists(crec->pid)) {
100 return 0;
103 d_printf("%-10.10s %s %-12s %s",
104 crec->servicename, procid_str_static(&crec->pid),
105 crec->machine,
106 time_to_asc(crec->start));
108 return 0;
111 struct sessionids {
112 int num_entries;
113 struct sessionid *entries;
116 static int collect_pids(const char *key, struct sessionid *session,
117 void *private_data)
119 struct sessionids *ids = (struct sessionids *)private_data;
121 if (!process_exists(session->pid))
122 return 0;
124 ids->num_entries += 1;
125 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
126 if (!ids->entries) {
127 ids->num_entries = 0;
128 return 0;
130 ids->entries[ids->num_entries-1] = *session;
132 return 0;
135 static int show_share_parseable(const struct connections_key *key,
136 const struct connections_data *crec,
137 void *state)
139 struct sessionids *ids = (struct sessionids *)state;
140 int i;
141 bool guest = true;
143 if (crec->cnum == -1)
144 return 0;
146 if (!process_exists(crec->pid)) {
147 return 0;
150 for (i=0; i<ids->num_entries; i++) {
151 struct server_id id = ids->entries[i].pid;
152 if (procid_equal(&id, &crec->pid)) {
153 guest = false;
154 break;
158 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
159 crec->servicename,procid_str_static(&crec->pid),
160 guest ? "" : uidtoname(ids->entries[i].uid),
161 guest ? "" : gidtoname(ids->entries[i].gid),
162 crec->machine,
163 guest ? "" : ids->entries[i].hostname,
164 time_to_asc(crec->start));
166 return 0;
169 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
171 struct sessionids ids;
173 ids.num_entries = 0;
174 ids.entries = NULL;
176 sessionid_traverse_read(collect_pids, &ids);
178 connections_forall_read(show_share_parseable, &ids);
180 SAFE_FREE(ids.entries);
182 return 0;
185 static int net_status_shares(struct net_context *c, int argc, const char **argv)
187 if (c->display_usage) {
188 d_printf( "%s\n"
189 "net status shares [parseable]\n"
190 " %s\n",
191 _("Usage:"),
192 _("Display open user shares.\n"
193 " If parseable is specified, output is machine-"
194 "readable."));
195 return 0;
198 if (argc == 0) {
200 d_printf(_("\nService pid machine "
201 "Connected at\n"
202 "-------------------------------------"
203 "------------------\n"));
205 connections_forall(show_share, NULL);
207 return 0;
210 if ((argc != 1) || !strequal(argv[0], "parseable")) {
211 return net_status_usage(c, argc, argv);
214 return net_status_shares_parseable(c, argc, argv);
217 int net_status(struct net_context *c, int argc, const char **argv)
219 struct functable func[] = {
221 "sessions",
222 net_status_sessions,
223 NET_TRANSPORT_LOCAL,
224 N_("Show list of open sessions"),
225 N_("net status sessions [parseable]\n"
226 " If parseable is specified, output is presented "
227 "in a machine-parseable fashion.")
230 "shares",
231 net_status_shares,
232 NET_TRANSPORT_LOCAL,
233 N_("Show list of open shares"),
234 N_("net status shares [parseable]\n"
235 " If parseable is specified, output is presented "
236 "in a machine-parseable fashion.")
238 {NULL, NULL, 0, NULL, NULL}
240 return net_run_function(c, argc, argv, "net status", func);