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