common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / dmapi / src / suite2 / src / check_dmapi.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 <sys/types.h>
20 #include <sys/stat.h>
21 #ifdef linux
22 #include <dmapi.h>
23 #else
24 #include <sys/dmi.h>
25 #endif
26
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <strings.h>
33
34 #include <lib/hsm.h>
35 #include <lib/dmport.h>
36
37 #ifdef linux
38 #include <string.h>
39 #endif
40
41 /*---------------------------------------------------------------------------
42 Automated test of version of DMAPI libraries & kernels 
43
44 The command line is: 
45
46      check_dmapi [-v] 
47
48 where v is a verbose-output flag
49 ----------------------------------------------------------------------------*/
50 #ifdef linux
51 #define CREATE_DESTROY_OPCODE DM_DESTROY_SESSION
52 #define SET_DISP_OPCODE DM_SET_DISP
53 #else
54 #define CREATE_DESTROY_OPCODE 5
55 #define SET_DISP_OPCODE 46
56 #endif
57
58 #ifndef linux
59 extern  char    *sys_errlist[];
60 #endif
61 extern  int     optind;
62 extern  char    *optarg;
63
64 char    *Progname;
65
66 int     dmi(int, ...);
67
68 static void
69 usage(void)
70 {
71         int     i;
72
73         fprintf(stderr, "usage:\t%s [-v]\n"
74                 "\t(use the v switch for verbose output)\n", Progname);
75         exit(1);
76 }
77
78
79 int
80 main(
81         int     argc, 
82         char    **argv)
83 {
84         dm_sessid_t     old_sid = -1;
85         dm_sessid_t     sid;
86         void            *hanp;
87         size_t           hlen;
88         dm_token_t      token = DM_NO_TOKEN;
89         int             Vflag = 0;
90         char            *name = "old";
91         char            *junk = "test junk";
92         int             opt;
93         int             i;
94         int             kernel_status=-1;
95         int             library_status=-1L;
96         dm_size_t       retval;
97         struct stat     stat_buf;
98
99         if (Progname = strrchr(argv[0], '/')) {
100                 Progname++;
101         } else {
102                 Progname = argv[0];
103         }
104
105         /* Crack and validate the command line options. */
106
107         while ((opt = getopt(argc, argv, "v")) != EOF) {
108           switch (opt) {
109           case 'v':
110             Vflag++;
111             break;
112           case '?':
113             usage();
114           }
115         }
116         if (optind != argc)
117           usage();
118         
119
120         if (geteuid()!=0) {
121           printf("You are running as user #%d.  "
122                  "You must be root to run this diagnostic.\n", geteuid());
123           exit(1);
124         }
125
126         /*--------------------------
127          * RESOLVE KERNEL PRESENCE:
128          *--------------------------
129          */
130         if (dmi(CREATE_DESTROY_OPCODE, old_sid, junk, &sid) >= 0) {
131           printf("ERROR: invalid kernel create/destroy_session call "
132                  "succeeded!\n");
133           exit(1);
134         }
135         else if (errno==ENOPKG) {
136           kernel_status=0;
137         }
138         else if (errno==EINVAL){
139           if (Vflag) printf("(create/destroy_session call verifies "
140                             "that you have DMAPI in kernel)\n");
141         }
142         else {
143           printf("ERROR: kernel create/destroy_session call produced "
144                  "unexpected errno, (%d) %s\n", errno, strerror(errno));
145         }
146         
147         /*----------------------------------
148          * RESOLVE KERNEL STATUS IF PRESENT:
149          *----------------------------------
150          */
151         if (kernel_status==-1 &&
152             dmi(SET_DISP_OPCODE, 
153                 (dm_sessid_t) 0, 
154                 (void*) 0,
155                 (size_t) 0,
156                 (dm_token_t) 0,
157                 (dm_eventset_t) 0,
158                 (u_int) 0) >= 0) { 
159           printf("ERROR: invalid kernel set_disp call suceeded!\n");
160         }
161         else {
162           if (errno==ENOSYS) {
163             if (Vflag) 
164               printf("(kernel set_disp call indicates old kernel)\n");
165             kernel_status=1;
166           }
167           else if (errno==ENOPKG) {
168             if (Vflag) 
169               printf("(kernel set_disp call indicates no kernel)\n");
170             kernel_status=0;
171           }
172           else if (errno==EINVAL) {
173             if (Vflag)
174               printf("(kernel set_disp call indicates new kernel)\n");
175             kernel_status=2;
176           }
177           else {
178             printf("ERROR: kernel set_disp call failed: (%d) %s\n", 
179                    errno, strerror(errno));
180             exit(1);
181           }
182         }
183
184         /*-------------------------
185          * RESOLVE LIBRARY STATUS:
186          *-------------------------
187          */
188         if (dm_init_service(&name) == -1)  {
189           fprintf(stderr, "ERROR: can't initialize the DMAPI (%s).\n",
190                   strerror(errno));
191           library_status=0;
192         }
193         else if (strcmp(name, DM_VER_STR_CONTENTS)) {
194           if (Vflag) 
195             printf("(dm_init_service suggests that "
196                    "you have an old library)\n");
197           library_status=1;
198         }
199         else {
200           if (Vflag) 
201             printf("(dm_init_service suggests that "
202                    "you have a new library)\n");
203           library_status=2;
204         }
205         
206         if (Vflag) printf("(dm_init_service returned %s)\n", name);
207         
208         /*-------------------------
209          * MAKE A DIAGNOSIS:
210          *-------------------------
211          */
212
213         if (library_status==2 && kernel_status==2){
214           printf("DIAGNOSIS: Tests show a current version of "
215                  "DMAPI is installed.\n");
216         } 
217         else if (library_status==1 && kernel_status==1) {
218           printf("DIAGNOSIS: Tests show that you have an outdated "
219                  "installation of DMAPI.\nUpgrades to both kernel and "
220                  "library routines will be necessary.\n");
221         }
222         else if (library_status==0 && kernel_status==0) {
223           printf("DIAGNOSIS: Tests show that NO components of the DMAPI "
224                  "are installed!\nUpgrades to both kernel and "
225                  "library routines will be necessary.\n");
226         }
227         else {
228           printf("DIAGNOSIS: Tests show that:\n"
229                  "Your DMAPI kernel routines are ");
230           switch (kernel_status) {
231           case 0: printf ("missing (not installed).\n");
232              break;
233           case 1: printf ("outdated.\n");
234              break;
235           case 2: printf ("current.\n ");
236              break;
237           default: printf("[ERROR!].\n");
238           }
239           printf("Your DMAPI library is ");
240           switch (library_status) {
241           case 0: printf ("missing (not installed).\n");
242              break;
243           case 1: printf ("outdated.\n");
244              break;
245           case 2: printf ("current.\n");
246              break;
247           default: printf("[ERROR!].\n");
248           }
249         }
250 #ifndef linux
251         if (library_status!=2 || kernel_status!=2){
252           printf("Please install XFS patch 1907 (for IRIX 6.2) or "
253                  "patch 2287 (for IRIX 6.4).\n");
254         }
255 #endif
256 }
257
258  
259