No Message Supplied
[xfstests-dev.git] / dmapi / src / suite1 / cmd / set_eventlist.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 #include <getopt.h>
36 #include <string.h>
37
38
39 /*---------------------------------------------------------------------------
40
41 Test program used to test the DMAPI function dm_set_eventlist().  The
42 command line is:
43
44         set_eventlist [-F] [-m max] [-s sid] [-t token] {pathname|handle} event [...]
45
46 where:
47 {pathname|handle}
48         is either the pathname of a file or a handle.
49 max
50         is the value to use for the maxevent parameter of dm_set_eventlist().
51 sid
52         is the dm_sessid_t value to use.
53 token
54         is the dm_token_t value to use (DM_NO_TOKEN is the default).
55 event
56         is one or more events to set.
57
58 ----------------------------------------------------------------------------*/
59
60 #ifndef linux
61 extern  char    *sys_errlist[];
62 #endif
63 extern  int     optind;
64 extern  char    *optarg;
65
66
67 char    *Progname;
68
69
70 static void
71 usage(void)
72 {
73         int     i;
74
75         fprintf(stderr, "usage:\t%s [-F] [-m max] [-s sid] "
76                 "{pathname|handle} event [...]\n", Progname);
77         fprintf(stderr, "possible events are:\n");
78         for (i = 0; i < ev_namecnt; i++) {
79                 fprintf(stderr, "%s (%d)\n", ev_names[i].name,
80                         ev_names[i].value);
81         }
82         exit(1);
83 }
84
85
86 int
87 main(
88         int     argc, 
89         char    **argv)
90 {
91         dm_sessid_t     sid = DM_NO_SESSION;
92         dm_token_t      token = DM_NO_TOKEN;
93         char            *object;
94         dm_eventset_t   eventset;
95         void            *hanp;
96         size_t          hlen;
97         int             Fflag = 0;
98         u_int           maxevent = DM_EVENT_MAX;
99         char            *name;
100         int             opt;
101
102         if (Progname = strrchr(argv[0], '/')) {
103                 Progname++;
104         } else {
105                 Progname = argv[0];
106         }
107
108         /* Crack and validate the command line options. */
109
110         while ((opt = getopt(argc, argv, "Fm:s:t:")) != EOF) {
111                 switch (opt) {
112                 case 'F':
113                         Fflag++;
114                         break;
115                 case 'm':
116                         maxevent = atol(optarg);
117                         break;
118                 case 's':
119                         sid = atol(optarg);
120                         break;
121                 case 't':
122                         token = atol(optarg);
123                         break;
124                 case '?':
125                         usage();
126                 }
127         }
128         if (optind + 1 > argc)
129                 usage();
130         object = argv[optind++];
131
132         if (dm_init_service(&name) == -1)  {
133                 fprintf(stderr, "Can't initialize the DMAPI\n");
134                 exit(1);
135         }
136         if (sid == DM_NO_SESSION)
137                 find_test_session(&sid);
138
139         DMEV_ZERO(eventset);
140
141         /* Get the file's handle or convert the external handle. */
142
143         if (opaque_to_handle(object, &hanp, &hlen)) {
144                 fprintf(stderr, "can't get handle for %s\n", object);
145                 exit(1);
146         }
147
148         if (Fflag) {
149                 void    *fshanp;
150                 size_t  fshlen;
151
152                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
153                         fprintf(stderr, "can't get filesystem handle from %s\n",
154                                 object);
155                         exit(1);
156                 }
157                 dm_handle_free(hanp, hlen);
158                 hanp = fshanp;
159                 hlen = fshlen;
160         }
161
162         for (; optind < argc; optind++) {
163                 dm_eventtype_t  event;
164
165                 event = ev_name_to_value(argv[optind]);
166                 if (event == DM_EVENT_INVALID) {
167                         fprintf(stderr, "invalid event %s\n", argv[optind]);
168                         usage();
169                 }
170                 if ((event == DM_EVENT_READ) || (event == DM_EVENT_WRITE) ||
171                     (event == DM_EVENT_TRUNCATE)) {
172                         fprintf(stderr, "Use set_region to twiddle read/write/trunc events\n");
173                         exit(1);
174                 }
175
176                 DMEV_SET(event, eventset);
177         }
178
179         if (dm_set_eventlist(sid, hanp, hlen, token, &eventset, maxevent)) {
180                 fprintf(stderr, "dm_set_eventlist failed, %s\n",
181                         strerror(errno));
182                 exit(1);
183         }
184
185         dm_handle_free(hanp, hlen);
186         exit(0);
187 }