Fix: close socket on protocol error, sendmsg MSG_NOSIGNAL
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
CommitLineData
b13d93c2
PW
1/*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
7 * the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
5aed31fc 19#define _LGPL_SOURCE
b13d93c2 20#define _GNU_SOURCE
f02baefb 21#include <lttng/ust-dlfcn.h>
b13d93c2 22#include <inttypes.h>
b13d93c2
PW
23#include <link.h>
24#include <unistd.h>
25#include <stdio.h>
13436238
PW
26#include <limits.h>
27#include <sys/types.h>
28#include <sys/stat.h>
b13d93c2
PW
29#include <signal.h>
30#include <sched.h>
31#include <stdarg.h>
eb2b066f 32#include "usterr-signal-safe.h"
b13d93c2
PW
33
34#include <lttng/ust-compiler.h>
35#include <lttng/ust.h>
36
13436238 37#define TRACEPOINT_DEFINE
6d4658aa 38#include "ust_dl.h"
13436238 39
b13d93c2
PW
40static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag);
41static int (*__lttng_ust_plibc_dlclose)(void *handle);
b13d93c2
PW
42
43static
44void *_lttng_ust_dl_libc_dlopen(const char *filename, int flag)
45{
46 if (!__lttng_ust_plibc_dlopen) {
47 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
48 if (__lttng_ust_plibc_dlopen == NULL) {
49 fprintf(stderr, "%s\n", dlerror());
50 return NULL;
51 }
52 }
53 return __lttng_ust_plibc_dlopen(filename, flag);
54}
55
56static
57int _lttng_ust_dl_libc_dlclose(void *handle)
58{
59 if (!__lttng_ust_plibc_dlclose) {
60 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
61 if (__lttng_ust_plibc_dlclose == NULL) {
62 fprintf(stderr, "%s\n", dlerror());
63 return -1;
64 }
65 }
66 return __lttng_ust_plibc_dlclose(handle);
67}
68
69static
03db42de 70void lttng_ust_dl_dlopen(void *so_base, const char *so_name, void *ip)
b13d93c2 71{
13436238
PW
72 char resolved_path[PATH_MAX];
73 struct stat sostat;
b13d93c2 74
13436238
PW
75 if (!realpath(so_name, resolved_path)) {
76 ERR("could not resolve path '%s'", so_name);
77 return;
b13d93c2 78 }
b13d93c2 79
13436238
PW
80 if (stat(resolved_path, &sostat)) {
81 ERR("could not access file status for %s", resolved_path);
82 return;
b13d93c2 83 }
13436238 84
6d4658aa 85 tracepoint(lttng_ust_dl, dlopen,
03db42de 86 so_base, resolved_path, sostat.st_size, sostat.st_mtime, ip);
13436238 87 return;
b13d93c2
PW
88}
89
90void *dlopen(const char *filename, int flag)
91{
92 void *handle = _lttng_ust_dl_libc_dlopen(filename, flag);
bd703713 93 if (__tracepoint_ptrs_registered && handle) {
b13d93c2
PW
94 struct link_map *p = NULL;
95 if (dlinfo(handle, RTLD_DI_LINKMAP, &p) != -1 && p != NULL
96 && p->l_addr != 0)
03db42de
MD
97 lttng_ust_dl_dlopen((void *) p->l_addr, p->l_name,
98 __builtin_return_address(0));
b13d93c2
PW
99 }
100 return handle;
101}
102
103int dlclose(void *handle)
104{
bd703713 105 if (__tracepoint_ptrs_registered && handle) {
b13d93c2
PW
106 struct link_map *p = NULL;
107 if (dlinfo(handle, RTLD_DI_LINKMAP, &p) != -1 && p != NULL
108 && p->l_addr != 0)
03db42de
MD
109 tracepoint(lttng_ust_dl, dlclose, (void *) p->l_addr,
110 __builtin_return_address(0));
b13d93c2
PW
111 }
112 return _lttng_ust_dl_libc_dlclose(handle);
113}
This page took 0.027692 seconds and 4 git commands to generate.