Imported Upstream version 1.8.7
[debian/sudo] / plugins / sudoers / regress / check_symbols / check_symbols.c
1 /*
2  * Copyright (c) 2012-2013 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 __dso_public int main(int argc, char *argv[]);
54
55 static void
56 usage(void)
57 {
58     fprintf(stderr, "usage: load_symbols plugin.so symbols_file\n");
59     exit(1);
60 }
61
62 int
63 main(int argc, char *argv[])
64 {
65     void *handle, *sym;
66     const char *plugin_path;
67     const char *symbols_file;
68     char *cp, line[LINE_MAX];
69     FILE *fp;
70     int ntests = 0, errors = 0;
71
72 #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME)
73     setprogname(argc > 0 ? argv[0] : "check_symbols");
74 #endif
75
76     if (argc != 3)
77         usage();
78     plugin_path = argv[1];
79     symbols_file = argv[2];
80
81     handle = dlopen(plugin_path, RTLD_LAZY|RTLD_GLOBAL);
82     if (handle == NULL)
83         fatalx_nodebug("unable to dlopen %s: %s", plugin_path, dlerror());
84
85     fp = fopen(symbols_file, "r");
86     if (fp == NULL)
87         fatal_nodebug("unable to open %s", symbols_file);
88
89     while (fgets(line, sizeof(line), fp) != NULL) {
90         ntests++;
91         if ((cp = strchr(line, '\n')) != NULL)
92             *cp = '\0';
93         sym = dlsym(handle, line);
94         if (sym == NULL) {
95             printf("%s: test %d: unable to resolve symbol %s: %s\n",
96                 getprogname(), ntests, line, dlerror());
97             errors++;
98         }
99     }
100
101     /*
102      * Make sure unexported symbols are not available.
103      */
104     ntests++;
105     sym = dlsym(handle, "user_in_group");
106     if (sym != NULL) {
107         printf("%s: test %d: able to resolve local symbol user_in_group\n",
108             getprogname(), ntests);
109         errors++;
110     }
111
112     dlclose(handle);
113
114     printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
115         ntests, errors, (ntests - errors) * 100 / ntests);
116
117     exit(errors);
118 }