2 * Simple Mr. Mean that can manipulate and torch sessions
4 * This code was written by Peter Lawthers, and placed in the public
5 * domain for use by DMAPI implementors and app writers.
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
12 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 #include <sys/types.h>
24 #include <sys/errno.h>
26 #include <lib/dmport.h>
32 extern int optind, opterr, optopt;
38 extern void err_msg(char *, ...);
39 extern void errno_msg(char *, ...);
41 int get_sessions(dm_sessid_t **, u_int *);
42 int get_tokens(dm_sessid_t, dm_token_t **, u_int *);
43 void print_session(dm_sessid_t);
44 void print_tokens(dm_sessid_t);
45 void kill_session(dm_sessid_t);
50 fprintf(stderr, "Usage: %s <options>\n", s);
51 fprintf(stderr, "\t-t list tokens\n");
52 fprintf(stderr, "\t-l list sessions\n");
53 fprintf(stderr, "\t-k kill sessions\n");
54 fprintf(stderr, "\t-s <specific_sid>\n");
55 fprintf(stderr, "\t-v verbose (for kill)\n");
66 int list_flag, kill_flag, token_flag, sid_flag;
67 dm_sessid_t *sidbuf, *sidp, onesid;
72 list_flag = sid_flag = kill_flag = token_flag = 0;
74 while ((c = getopt(argc, argv, "vlkts:")) != EOF) {
86 if (sscanf(optarg, "%d", &onesid) <=0 ) {
87 err_msg("Can't convert sid %s", optarg);
101 if (!list_flag && !sid_flag && !kill_flag && !token_flag) {
106 if (dm_init_service(&cp) == -1) {
107 err_msg("Can't init dmapi");
110 if (strcmp(cp, DM_VER_STR_CONTENTS)) {
111 err_msg("Compiled for a different version");
117 * Get the list of all sessions in the system
119 error = get_sessions(&sidbuf, &nsids);
124 * Walk through the list of sessions do what is right
127 for (i=0; i<nsids; i++, sidp++) {
130 * If we're only looking for one sid, then
131 * we can skip this one if there's no match
137 print_session(*sidp);
147 * Print out info about a sessions
150 print_session(dm_sessid_t sid)
152 char buf[DM_SESSION_INFO_LEN];
155 if (dm_query_session(sid,DM_SESSION_INFO_LEN,(void *)buf, &ret) == -1) {
156 errno_msg("Can't get session info");
160 printf("Session (%d) name: %s\n", sid, buf);
165 * Get all the tokens for a session
168 print_tokens(dm_sessid_t sid)
174 error = get_tokens(sid, &tbuf, &ntokens);
178 printf("\tTokens (%d): ", ntokens);
179 for (i=0; i<ntokens; i++)
180 printf("%d ", *tbuf++);
189 * Try and kill a session
192 kill_session(dm_sessid_t sid)
199 * Get all the tokens in the system so we can respond to them
201 error = get_tokens(sid, &tbuf, &ntokens);
205 if (ntokens && Verbose)
206 printf("\tResponding to events for sid %d, tokens: \n", sid);
208 for (i=0; i<ntokens; i++) {
210 printf("\t\t%d ", *tbuf);
212 if (dm_respond_event(sid, *tbuf, DM_RESP_ABORT, EIO, 0, NULL) == -1)
213 errno_msg("Can't respond to event, sid %d token %d",
219 printf("\tDestroying session %d\n", sid);
220 if (dm_destroy_session(sid) == -1)
221 errno_msg("Can't shut down session %d", sid);
235 * Pick an arbitrary number of sessions to get info for.
236 * If it's not enough, then we can always resize our buffer
240 sidbuf = malloc(nsids * sizeof(dm_sessid_t));
241 if (sidbuf == NULL) {
242 err_msg("Can't malloc memory");
247 if (dm_getall_sessions(nsids, sidbuf, &nret) == -1) {
248 if (errno != E2BIG) {
249 errno_msg("Can't get list of sessions");
255 sidbuf = malloc(nsids * sizeof(dm_sessid_t));
256 if (sidbuf == NULL) {
257 err_msg("Can't malloc memory");
261 if (dm_getall_sessions(nsids, sidbuf, &nret) == -1) {
263 errno_msg("Can't get sessions with new buf");
270 if (error && (sidbuf != NULL) )
282 * Get all tokens in the session
296 tbuf = (dm_token_t *)malloc(ntokens * sizeof(dm_token_t));
300 if (dm_getall_tokens(sid, ntokens, tbuf, &nret) == -1) {
301 if (errno != E2BIG) {
302 errno_msg("Can't get all tokens");
307 tbuf = (dm_token_t *)malloc(ntokens * sizeof(dm_token_t));
311 if (dm_getall_tokens(sid, ntokens, tbuf, &nret) == -1) {
312 errno_msg("Can't get all tokens");
317 if (error && (tbuf != NULL))