2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
38 #include <linux/limits.h>
44 #include <sys/types.h>
48 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
53 const char* SelLdrLauncher::GetPluginDirname() {
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");
63 strncpy(bin_dir
, info
.dli_fname
, sizeof(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.
73 // Open the file, returning NULL for failure.
74 if (NULL
== (fp
= fopen("/proc/self/maps", "r"))) {
79 static int dummy_for_address
;
80 const unsigned kAddressInSo
= (unsigned) &dummy_for_address
;
89 if (7 != fscanf(fp
, "%x-%x %s %x %x:%x %x",
90 &low_addr
, &high_addr
, perm
, &offset
, &dev_maj
,
94 if (NULL
== fgets(bin_dir
, sizeof(bin_dir
), fp
)) {
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
, '/');
103 // Close /proc/self/maps.
106 // If we didn't find a matching pathname, try the path of the current
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
) {
113 bin_dir
[bin_dir_size
] = '\0';
116 // Return just the directory path.
117 return dirname(pathname
);