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