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