e6d0086983dd231c0a7bec170c669dbf65a5a86f
[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          * EXAMINE /usr/include/sys/dmi.h:
128          *--------------------------------
129          */
130 #ifdef __sgi
131 #define DMAPI_HDR "/usr/include/sys/dmi.h"
132
133         if (stat(DMAPI_HDR, &stat_buf)==-1){
134           if (errno==ENOENT) { 
135             printf( "You are missing a vital DMAPI file: %s\n", DMAPI_HDR);
136           }
137           else {
138             printf( "ERROR: could not stat %s (%s)\n", DMAPI_HDR, strerror(errno));
139           }
140         }
141         else {
142           if (stat_buf.st_size <= 15000) {
143             printf("You appear to have an old version of a vital DMAPI file: %s\n", DMAPI_HDR);
144           }
145           else if (Vflag) {
146             printf("(You appear to have the correct version of %s\n", DMAPI_HDR);
147           }
148         }
149 #endif
150         
151         /*--------------------------
152          * RESOLVE KERNEL PRESENCE:
153          *--------------------------
154          */
155         if (dmi(CREATE_DESTROY_OPCODE, old_sid, junk, &sid) >= 0) {
156           printf("ERROR: invalid kernel create/destroy_session call "
157                  "succeeded!\n");
158           exit(1);
159         }
160         else if (errno==ENOPKG) {
161           kernel_status=0;
162         }
163         else if (errno==EINVAL){
164           if (Vflag) printf("(create/destroy_session call verifies "
165                             "that you have DMAPI in kernel)\n");
166         }
167         else {
168           printf("ERROR: kernel create/destroy_session call produced "
169                  "unexpected errno, (%d) %s\n", errno, strerror(errno));
170         }
171         
172         /*----------------------------------
173          * RESOLVE KERNEL STATUS IF PRESENT:
174          *----------------------------------
175          */
176         if (kernel_status==-1 &&
177             dmi(SET_DISP_OPCODE, 
178                 (dm_sessid_t) 0, 
179                 (void*) 0,
180                 (size_t) 0,
181                 (dm_token_t) 0,
182                 (dm_eventset_t) 0,
183                 (u_int) 0) >= 0) { 
184           printf("ERROR: invalid kernel set_disp call suceeded!\n");
185         }
186         else {
187           if (errno==ENOSYS) {
188             if (Vflag) 
189               printf("(kernel set_disp call indicates old kernel)\n");
190             kernel_status=1;
191           }
192           else if (errno==ENOPKG) {
193             if (Vflag) 
194               printf("(kernel set_disp call indicates no kernel)\n");
195             kernel_status=0;
196           }
197           else if (errno==EINVAL) {
198             if (Vflag)
199               printf("(kernel set_disp call indicates new kernel)\n");
200             kernel_status=2;
201           }
202           else {
203             printf("ERROR: kernel set_disp call failed: (%d) %s\n", 
204                    errno, strerror(errno));
205             exit(1);
206           }
207         }
208
209         /*-------------------------
210          * RESOLVE LIBRARY STATUS:
211          *-------------------------
212          */
213         if (dm_init_service(&name) == -1)  {
214           fprintf(stderr, "ERROR: can't initialize the DMAPI (%s).\n",
215                   strerror(errno));
216           library_status=0;
217         }
218         else if (strcmp(name, DM_VER_STR_CONTENTS)) {
219           if (Vflag) 
220             printf("(dm_init_service suggests that "
221                    "you have an old library)\n");
222           library_status=1;
223         }
224         else {
225           if (Vflag) 
226             printf("(dm_init_service suggests that "
227                    "you have a new library)\n");
228           library_status=2;
229         }
230         
231         if (Vflag) printf("(dm_init_service returned %s)\n", name);
232         
233         /*-------------------------
234          * MAKE A DIAGNOSIS:
235          *-------------------------
236          */
237
238         if (library_status==2 && kernel_status==2){
239           printf("DIAGNOSIS: Tests show a current version of "
240                  "DMAPI is installed.\n");
241         } 
242         else if (library_status==1 && kernel_status==1) {
243           printf("DIAGNOSIS: Tests show that you have an outdated "
244                  "installation of DMAPI.\nUpgrades to both kernel and "
245                  "library routines will be necessary.\n");
246         }
247         else if (library_status==0 && kernel_status==0) {
248           printf("DIAGNOSIS: Tests show that NO components of the DMAPI "
249                  "are installed!\nUpgrades to both kernel and "
250                  "library routines will be necessary.\n");
251         }
252         else {
253           printf("DIAGNOSIS: Tests show that:\n"
254                  "Your DMAPI kernel routines are ");
255           switch (kernel_status) {
256           case 0: printf ("missing (not installed).\n");
257              break;
258           case 1: printf ("outdated.\n");
259              break;
260           case 2: printf ("current.\n ");
261              break;
262           default: printf("[ERROR!].\n");
263           }
264           printf("Your DMAPI library is ");
265           switch (library_status) {
266           case 0: printf ("missing (not installed).\n");
267              break;
268           case 1: printf ("outdated.\n");
269              break;
270           case 2: printf ("current.\n");
271              break;
272           default: printf("[ERROR!].\n");
273           }
274         }
275 #ifndef linux
276         if (library_status!=2 || kernel_status!=2){
277           printf("Please install XFS patch 1907 (for IRIX 6.2) or "
278                  "patch 2287 (for IRIX 6.4).\n");
279         }
280 #endif
281 }
282
283  
284