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