dmapi: fix build failure if libdm is provided via -ldm
[xfstests-dev.git] / dmapi / src / suite1 / cmd / request_right.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
28 Test program used to test the DMAPI function dm_request_right().  The
29 command line is:
30
31         request_right [-F] [-w] [-s sid] token {pathname|handle} right
32
33 where
34 -F
35         when a pathname is specified, -F indicates that its filesystem handle
36         should be used rather than its file object handle.
37 -w
38         if specified, the DM_RR_WAIT flag will be used.
39 sid
40         is the dm_sessid_t to use rather than the default test session.
41 token
42         is the dm_token_t to use.
43 {pathname|handle}
44         is either a handle, or is the pathname of a file whose handle is
45         to be used.
46 right
47         is the dm_right_t to use.
48
49 ----------------------------------------------------------------------------*/
50
51 #ifndef linux
52 extern  char    *sys_errlist[];
53 #endif
54 extern  int     optind;
55 extern  char    *optarg;
56
57
58 char    *Progname;
59
60
61 static void
62 usage(void)
63 {
64         int     i;
65
66         fprintf(stderr, "usage:\t%s [-F] [-w] [-s sid] token "
67                 "{pathname|handle} right\n", Progname);
68         fprintf(stderr, "possible rights are:\n");
69         for (i = 0; i < rt_namecnt; i++) {
70                 fprintf(stderr, "%s (%d)\n", rt_names[i].name,
71                         rt_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;
84         dm_right_t      right;
85         char            *object;
86         char            *rightstr;
87         void            *hanp;
88         size_t          hlen;
89         int             Fflag = 0;
90         int             wflag = 0;
91         char            *name;
92         int             opt;
93
94         Progname = strrchr(argv[0], '/');
95         if (Progname) {
96                 Progname++;
97         } else {
98                 Progname = argv[0];
99         }
100
101         /* Crack and validate the command line options. */
102
103         while ((opt = getopt(argc, argv, "Fws:")) != EOF) {
104                 switch (opt) {
105                 case 'F':
106                         Fflag++;
107                         break;
108                 case 'w':
109                         wflag++;
110                         break;
111                 case 's':
112                         sid = atol(optarg);
113                         break;
114                 case '?':
115                         usage();
116                 }
117         }
118         if (optind + 3 != argc)
119                 usage();
120         token = atol(argv[optind++]);
121         object = argv[optind++];
122         rightstr = argv[optind];
123
124         if (dm_init_service(&name) == -1)  {
125                 fprintf(stderr, "Can't initialize the DMAPI\n");
126                 exit(1);
127         }
128         if (sid == DM_NO_SESSION)
129                 find_test_session(&sid);
130
131         /* Get the file or filesystem's handle. */
132
133         if (opaque_to_handle(object, &hanp, &hlen)) {
134                 fprintf(stderr, "can't get handle from %s\n", object);
135                 exit(1);
136         }
137         if (Fflag) {
138                 void    *fshanp;
139                 size_t  fshlen;
140
141                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
142                         fprintf(stderr, "can't get filesystem handle from %s\n",
143                                 object);
144                         exit(1);
145                 }
146                 dm_handle_free(hanp, hlen);
147                 hanp = fshanp;
148                 hlen = fshlen;
149         }
150
151         if (rt_name_to_value(rightstr, &right)) {
152                 fprintf(stderr, "invalid right %s\n", rightstr);
153                 usage();
154         }
155
156         if (dm_request_right(sid, hanp, hlen, token,
157             (wflag ? DM_RR_WAIT : 0), right)) {
158                 fprintf(stderr, "dm_request_right failed, %s\n",
159                         strerror(errno));
160                 return(1);
161         }
162
163         dm_handle_free(hanp, hlen);
164         exit(0);
165 }