1*7ce20373SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*7ce20373SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*7ce20373SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*7ce20373SAndrew Rist * distributed with this work for additional information
6*7ce20373SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*7ce20373SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*7ce20373SAndrew Rist * "License"); you may not use this file except in compliance
9*7ce20373SAndrew Rist * with the License. You may obtain a copy of the License at
10*7ce20373SAndrew Rist *
11*7ce20373SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*7ce20373SAndrew Rist *
13*7ce20373SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*7ce20373SAndrew Rist * software distributed under the License is distributed on an
15*7ce20373SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7ce20373SAndrew Rist * KIND, either express or implied. See the License for the
17*7ce20373SAndrew Rist * specific language governing permissions and limitations
18*7ce20373SAndrew Rist * under the License.
19*7ce20373SAndrew Rist *
20*7ce20373SAndrew Rist *************************************************************/
21*7ce20373SAndrew Rist
22*7ce20373SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir #include <stdio.h>
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir #include <errno.h>
28cdf0e10cSrcweir #include <unistd.h>
29cdf0e10cSrcweir #include <dlfcn.h>
30cdf0e10cSrcweir
31cdf0e10cSrcweir /*
32cdf0e10cSrcweir * NOTE: Since no one is really interested in correct unload behavior I've
33cdf0e10cSrcweir * disabled the shared library unload check. If you want to reenable it comment
34cdf0e10cSrcweir * the following line out
35cdf0e10cSrcweir */
36cdf0e10cSrcweir #define NO_UNLOAD_CHECK
37cdf0e10cSrcweir
38cdf0e10cSrcweir static const char *pprog_name = "checkdll";
39cdf0e10cSrcweir static const char *psymbol = "GetVersionInfo";
40cdf0e10cSrcweir
usage()41cdf0e10cSrcweir void usage()
42cdf0e10cSrcweir {
43cdf0e10cSrcweir fprintf(stderr, "usage: %s [-s] <dllname>\n", pprog_name);
44cdf0e10cSrcweir return;
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
main(int argc,char * argv[])47cdf0e10cSrcweir int main(int argc, char *argv[])
48cdf0e10cSrcweir {
49cdf0e10cSrcweir int rc;
50cdf0e10cSrcweir int silent=0;
51cdf0e10cSrcweir void *phandle;
52cdf0e10cSrcweir char *(*pfun)(void);
53cdf0e10cSrcweir
54cdf0e10cSrcweir if ( argc < 2 || argc > 4) {
55cdf0e10cSrcweir usage();
56cdf0e10cSrcweir return 1;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
59cdf0e10cSrcweir if ( !strcmp(argv[1],"-s") ) {
60cdf0e10cSrcweir silent = 1;
61cdf0e10cSrcweir ++argv, --argc;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir if ( (rc = access( argv[1], R_OK )) == -1 ) {
65cdf0e10cSrcweir fprintf(stderr, "%s: ERROR: %s: %s\n",
66cdf0e10cSrcweir pprog_name, argv[1], strerror(errno));
67cdf0e10cSrcweir return 2;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir if (!silent) printf("Checking DLL %s ...", argv[1]);
71cdf0e10cSrcweir fflush(stdout);
72cdf0e10cSrcweir
73cdf0e10cSrcweir if ( (phandle = dlopen(argv[1], RTLD_NOW)) != NULL ) {
74cdf0e10cSrcweir if ( (pfun = (char *(*)(void))dlsym(phandle, psymbol)) != NULL ) {
75cdf0e10cSrcweir if (!silent) printf(": ok\n");
76cdf0e10cSrcweir }
77cdf0e10cSrcweir else
78cdf0e10cSrcweir {
79cdf0e10cSrcweir printf(": WARNING: %s\n", dlerror());
80cdf0e10cSrcweir }
81cdf0e10cSrcweir #ifdef NO_UNLOAD_CHECK
82cdf0e10cSrcweir _exit(0);
83cdf0e10cSrcweir #else
84cdf0e10cSrcweir dlclose(phandle);
85cdf0e10cSrcweir #endif
86cdf0e10cSrcweir return 0;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir
89cdf0e10cSrcweir printf(": ERROR: %s\n", dlerror());
90cdf0e10cSrcweir return 3;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir
93cdf0e10cSrcweir
94