736414cea8d4dc120af939c4820a0ff1ee26424e
[xfstests-dev.git] / dmapi / src / suite1 / cmd / set_disp.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <lib/hsm.h>
34
35 #ifdef linux
36 #include <string.h>
37 #endif
38
39 /*---------------------------------------------------------------------------
40
41 Test program used to test the DMAPI function dm_set_eventlist().  The
42 command line is:
43
44         set_disp [-m max] [-s sid] [-t token] {pathname|handle} [event...]
45         set_disp [-m max] [-s sid] -G [DM_EVENT_MOUNT]
46
47 where
48 {pathname|handle}
49         is the name of a file or handle whose filesystem is of interest.
50 -G
51         if the global handle should be used
52 max
53         is the value to use for the maxevent parameter of dm_set_disp(),
54 sid
55         is the dm_sessid_t to use rather than the default test session.
56 token
57         is the dm_token_t to use in place of DM_NO_TOKEN.
58 event
59         is zero or more events to set.
60
61 ----------------------------------------------------------------------------*/
62
63 #ifndef linux
64 extern  char    *sys_errlist[];
65 #endif
66 extern  int     optind;
67 extern  char    *optarg;
68
69
70 char    *Progname;
71
72
73 static void
74 usage(void)
75 {
76         int     i;
77
78         fprintf(stderr, "usage:\t%s [-m max] [-s sid] [-t token] "
79                 "{pathname|handle} [event...]\n", Progname);
80         fprintf(stderr, "usage:\t%s [-m max] [-s sid] -G [DM_EVENT_MOUNT]\n",
81                 Progname);
82         fprintf(stderr, "possible events are:\n");
83         for (i = 0; i < ev_namecnt; i++) {
84                 fprintf(stderr, "%s (%d)\n", ev_names[i].name,
85                         ev_names[i].value);
86         }
87         exit(1);
88 }
89
90
91 int
92 main(
93         int     argc, 
94         char    **argv)
95 {
96         dm_sessid_t     sid = DM_NO_SESSION;
97         dm_token_t      token = DM_NO_TOKEN;
98         u_int           maxevent = DM_EVENT_MAX;
99         dm_eventset_t   eventset;
100         void            *fshanp;
101         size_t          fshlen;
102         int             Gflag = 0;
103         char            *name;
104         int             opt;
105
106         if (Progname = strrchr(argv[0], '/')) {
107                 Progname++;
108         } else {
109                 Progname = argv[0];
110         }
111
112         /* Crack and validate the command line options. */
113
114         while ((opt = getopt(argc, argv, "Gm:s:t:")) != EOF) {
115                 switch (opt) {
116                 case 'G':
117                         Gflag++;
118                         break;
119                 case 'm':
120                         maxevent = atol(optarg);
121                         break;
122                 case 's':
123                         sid = atol(optarg);
124                         break;
125                 case 't':
126                         token = atol(optarg);
127                         break;
128                 case '?':
129                         usage();
130                 }
131         }
132         if (Gflag && token != DM_NO_TOKEN)
133                 usage();
134         if (optind + (Gflag ? 0 : 1) > argc)
135                 usage();
136
137         if (dm_init_service(&name) == -1)  {
138                 fprintf(stderr, "Can't inititalize the DMAPI\n");
139                 exit(1);
140         }
141         if (sid == DM_NO_SESSION)
142                 find_test_session(&sid);
143
144         DMEV_ZERO(eventset);
145
146         /* Get the file or filesystem's handle. */
147
148         if (Gflag) {
149                 fshanp = DM_GLOBAL_HANP;
150                 fshlen = DM_GLOBAL_HLEN;
151         } else {
152                 char    *object;
153                 void    *hanp;
154                 size_t  hlen;
155
156                 object = argv[optind++];
157
158                 if (opaque_to_handle(object, &hanp, &hlen)) {
159                         fprintf(stderr, "can't get handle from %s\n", object);
160                         exit(1);
161                 }
162                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
163                         fprintf(stderr, "can't get filesystem handle from %s\n",
164                                 object);
165                         exit(1);
166                 }
167                 dm_handle_free(hanp, hlen);
168         }
169
170         for (; optind < argc; optind++) {
171                 dm_eventtype_t  event;
172
173                 event = ev_name_to_value(argv[optind]);
174                 if (event == DM_EVENT_INVALID) {
175                         fprintf(stderr, "invalid event %s\n", argv[optind]);
176                         usage();
177                 }
178                 DMEV_SET(event, eventset);
179         }
180
181         if (dm_set_disp(sid, fshanp, fshlen, token, &eventset, maxevent)) {
182                 fprintf(stderr, "dm_set_disp failed, %s\n",
183                         strerror(errno));
184                 exit(1);
185         }
186
187         if (!Gflag)
188                 dm_handle_free(fshanp, fshlen);
189         exit(0);
190 }