1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 // This program determines which OS that this Valgrind installation
7 // supports, which depends on what was chosen at configure-time.
8 //
9 // We return:
10 // - 0 if the machine matches the asked-for OS and satisfies a
11 //     version requirement, if any
12 // - 1 if it doesn't match but does match the name of another OS
13 // - 2 if it doesn't match the name of any OS
14 // - 3 if there was a usage error (it also prints an error message)
15 
16 // Nb: When updating this file for a new OS, add the name to
17 // 'all_OSes' as well as adding go().
18 
19 #define False  0
20 #define True   1
21 typedef int    Bool;
22 
23 char* all_OSes[] = {
24    "linux",
25    "darwin",
26    "solaris",
27    "dragonfly",
28    NULL
29 };
30 
31 #if defined(VGO_linux)
matches_version(char * min_version)32 static Bool matches_version(char *min_version)
33 {
34    int a1, a2, a3, g1, g2, g3;  // 'a' = actual;  'g' = given
35 
36    if (min_version == NULL)  return True;  // no version specified
37 
38    // get actual version number
39    FILE *fp = fopen("/proc/sys/kernel/osrelease", "r");
40    if (fp == NULL || fscanf(fp, "%d.%d.%d", &a1, &a2, &a3) != 3) return False;
41    fclose(fp);
42 
43    // parse given version number
44    if (sscanf(min_version, "%d.%d.%d", &g1, &g2, &g3) != 3) return False;
45 
46 //   printf("actual %d %d %d\n", a1, a2,a3);
47 //   printf("given  %d %d %d\n", g1, g2,g3);
48 
49    if (a1 > g1) return True;
50    if (a1 < g1) return False;
51    if (a2 > g2) return True;
52    if (a2 < g2) return False;
53    if (a3 >= g3) return True;
54 
55    return False;
56 }
57 #endif
58 
go(char * OS,char * min_version)59 static Bool go(char* OS, char *min_version)
60 {
61 #if defined(VGO_linux)
62    if ( 0 == strcmp( OS, "linux" ) && matches_version( min_version )) return True;
63 
64 #elif defined(VGO_darwin)
65    if ( 0 == strcmp( OS, "darwin" ) ) return True;
66 
67 #elif defined(VGO_solaris)
68    if ( 0 == strcmp( OS, "solaris" ) ) return True;
69 
70 #elif defined(VGO_dragonfly)
71    if ( 0 == strcmp( OS, "dragonfly" ) ) return True;
72 
73 #else
74 #  error Unknown OS
75 #endif   // VGO_*
76 
77    return False;
78 }
79 
80 //---------------------------------------------------------------------------
81 // main
82 //---------------------------------------------------------------------------
main(int argc,char ** argv)83 int main(int argc, char **argv)
84 {
85    int i;
86    if ( argc < 2 ) {
87       fprintf( stderr, "usage: os_test <OS-type> [<min-version>]\n" );
88       exit(3);             // Usage error.
89    }
90    if (go( argv[1], argv[2] )) {
91       return 0;            // Matched.
92    }
93    for (i = 0; NULL != all_OSes[i]; i++) {
94       if ( 0 == strcmp( argv[1], all_OSes[i] ) )
95          return 1;         // Didn't match, but named another OS.
96    }
97    return 2;               // Didn't match any OSes.
98 }
99 
100