Remove check for ABIVERSION, so that I don't have to rebuild binutils when this is...
[nativeclient.git] / nonnacl_util / linux / get_plugin_dirname.cc
blob8726b689badb00bffd2a94f5fc7687b518d988df
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36 #include <dlfcn.h>
37 #include <libgen.h>
38 #include <linux/limits.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <unistd.h>
48 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
50 namespace nacl {
53 const char* SelLdrLauncher::GetPluginDirname() {
54 Dl_info info;
55 void* sym_addr = reinterpret_cast<void*>(&GetPluginDirname);
56 char* pathname = NULL;
57 static char bin_dir[2 * PATH_MAX + 1];
59 // First, try looking a symbol up in the dynamic loader's records.
60 if (0 == dladdr(sym_addr, &info)) {
61 printf("dladdr failed\n");
62 } else {
63 strncpy(bin_dir, info.dli_fname, sizeof(bin_dir));
64 pathname = bin_dir;
66 // What follows is probably major overkill if the dladdr scheme works.
67 if (NULL == pathname) {
68 // Identify the path the plugin was loaded from. For Linux we look through
69 // the address ranges in /proc/self/maps for the address of a static
70 // variable. If that fails, look in /proc/self/exe.
71 FILE* fp;
73 // Open the file, returning NULL for failure.
74 if (NULL == (fp = fopen("/proc/self/maps", "r"))) {
75 return NULL;
77 // Read line by line.
78 while (!feof(fp)) {
79 static int dummy_for_address;
80 const unsigned kAddressInSo = (unsigned) &dummy_for_address;
81 uintptr_t low_addr;
82 uintptr_t high_addr;
83 char perm[5];
84 uintptr_t offset;
85 unsigned dev_maj;
86 unsigned dev_min;
87 unsigned inode;
89 if (7 != fscanf(fp, "%x-%x %s %x %x:%x %x",
90 &low_addr, &high_addr, perm, &offset, &dev_maj,
91 &dev_min, &inode)) {
92 return NULL;
94 if (NULL == fgets(bin_dir, sizeof(bin_dir), fp)) {
95 return NULL;
97 // If the address is in the range, break out of the loop.
98 if (kAddressInSo >= low_addr && kAddressInSo < high_addr) {
99 pathname = strchr(bin_dir, '/');
100 break;
103 // Close /proc/self/maps.
104 fclose(fp);
106 // If we didn't find a matching pathname, try the path of the current
107 // executable.
108 if (NULL == pathname) {
109 int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
110 if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
111 return NULL;
113 bin_dir[bin_dir_size] = '\0';
114 pathname = bin_dir;
116 // Return just the directory path.
117 return dirname(pathname);
121 } // namespace nacl