Imported Upstream version 1.8.6p8
[debian/sudo] / plugins / sudoers / regress / check_symbols / check_symbols.c
1 /*
2  * Copyright (c) 2012 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <stdio.h>
21 #ifdef STDC_HEADERS
22 # include <stdlib.h>
23 # include <stddef.h>
24 #else
25 # ifdef HAVE_STDLIB_H
26 #  include <stdlib.h>
27 # endif
28 #endif /* STDC_HEADERS */
29 #ifdef HAVE_STRING_H
30 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
31 #  include <memory.h>
32 # endif
33 # include <string.h>
34 #endif /* HAVE_STRING_H */
35 #ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 #endif /* HAVE_STRINGS_H */
38 #ifdef HAVE_DLOPEN
39 # include <dlfcn.h>
40 #else
41 # include "compat/dlfcn.h"
42 #endif
43 #include <errno.h>
44 #include <limits.h>
45
46 #include "missing.h"
47 #include "error.h"
48
49 #ifndef LINE_MAX
50 # define LINE_MAX 2048
51 #endif
52
53 static void
54 usage(void)
55 {
56     fprintf(stderr, "usage: load_symbols plugin.so symbols_file\n");
57     exit(1);
58 }
59
60 int
61 main(int argc, char *argv[])
62 {
63     void *handle, *sym;
64     const char *plugin_path;
65     const char *symbols_file;
66     char *cp, line[LINE_MAX];
67     FILE *fp;
68     int ntests = 0, errors = 0;
69
70 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
71     setprogname(argc > 0 ? argv[0] : "check_symbols");
72 #endif
73
74     if (argc != 3)
75         usage();
76     plugin_path = argv[1];
77     symbols_file = argv[2];
78
79     handle = dlopen(plugin_path, RTLD_LAZY|RTLD_GLOBAL);
80     if (handle == NULL)
81         errorx2(1, "unable to dlopen %s: %s", plugin_path, dlerror());
82
83     fp = fopen(symbols_file, "r");
84     if (fp == NULL)
85         error2(1, "unable to open %s", symbols_file);
86
87     while (fgets(line, sizeof(line), fp) != NULL) {
88         ntests++;
89         if ((cp = strchr(line, '\n')) != NULL)
90             *cp = '\0';
91         sym = dlsym(handle, line);
92         if (sym == NULL) {
93             warningx2("unable to resolve symbol %s: %s", line, dlerror());
94             errors++;
95         }
96     }
97
98     /*
99      * Make sure unexported symbols are not available.
100      */
101     sym = dlsym(handle, "user_in_group");
102     if (sym != NULL) {
103         warningx2("able to resolve local symbol user_in_group");
104         errors++;
105     }
106     ntests++;
107
108     dlclose(handle);
109
110     printf("check_symbols: %d tests run, %d errors, %d%% success rate\n",
111         ntests, errors, (ntests - errors) * 100 / ntests);
112
113     exit(errors);
114 }
115
116 void
117 cleanup(int gotsig)
118 {
119     return;
120 }