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