Fix: race between lttng-ust getenv() and application setenv()
[lttng-ust.git] / liblttng-ust / lttng-clock.c
CommitLineData
e11fcffc
MD
1/*
2 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.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
19#define _GNU_SOURCE
3fbec7dc 20#define _LGPL_SOURCE
e11fcffc
MD
21#include <error.h>
22#include <dlfcn.h>
23#include <stdlib.h>
24#include <usterr-signal-safe.h>
25#include <lttng/ust-clock.h>
26#include <urcu/system.h>
27#include <urcu/arch.h>
28
29#include "clock.h"
730bf2af 30#include "getenv.h"
e11fcffc
MD
31
32struct lttng_trace_clock *lttng_trace_clock;
33
34static
35struct lttng_trace_clock user_tc;
36
4bc1ccd7
MD
37static
38void *clock_handle;
39
e11fcffc
MD
40int lttng_ust_trace_clock_set_read64_cb(uint64_t (*read64)(void))
41{
42 if (CMM_LOAD_SHARED(lttng_trace_clock))
43 return -EBUSY;
44 user_tc.read64 = read64;
45 return 0;
46}
47
48int lttng_ust_trace_clock_set_freq_cb(uint64_t (*freq)(void))
49{
50 if (CMM_LOAD_SHARED(lttng_trace_clock))
51 return -EBUSY;
52 user_tc.freq = freq;
53 return 0;
54}
55
56int lttng_ust_trace_clock_set_uuid_cb(int (*uuid)(char *uuid))
57{
58 if (CMM_LOAD_SHARED(lttng_trace_clock))
59 return -EBUSY;
60 user_tc.uuid = uuid;
61 return 0;
62}
63
64int lttng_ust_trace_clock_set_name_cb(const char *(*name)(void))
65{
66 if (CMM_LOAD_SHARED(lttng_trace_clock))
67 return -EBUSY;
68 user_tc.name = name;
69 return 0;
70}
71
72int lttng_ust_trace_clock_set_description_cb(const char *(*description)(void))
73{
74 if (CMM_LOAD_SHARED(lttng_trace_clock))
75 return -EBUSY;
76 user_tc.description = description;
77 return 0;
78}
79
80int lttng_ust_enable_trace_clock_override(void)
81{
82 if (CMM_LOAD_SHARED(lttng_trace_clock))
83 return -EBUSY;
84 if (!user_tc.read64)
85 return -EINVAL;
86 if (!user_tc.freq)
87 return -EINVAL;
88 if (!user_tc.name)
89 return -EINVAL;
90 if (!user_tc.description)
91 return -EINVAL;
92 /* Use default uuid cb when NULL */
93 cmm_smp_mb(); /* Store callbacks before trace clock */
94 CMM_STORE_SHARED(lttng_trace_clock, &user_tc);
95 return 0;
96}
97
98void lttng_ust_clock_init(void)
99{
100 const char *libname;
e11fcffc
MD
101 void (*libinit)(void);
102
4bc1ccd7
MD
103 if (clock_handle)
104 return;
6f626d28 105 libname = lttng_getenv("LTTNG_UST_CLOCK_PLUGIN");
e11fcffc
MD
106 if (!libname)
107 return;
4bc1ccd7
MD
108 clock_handle = dlopen(libname, RTLD_NOW);
109 if (!clock_handle) {
e11fcffc
MD
110 PERROR("Cannot load LTTng UST clock override library %s",
111 libname);
112 return;
113 }
114 dlerror();
4bc1ccd7 115 libinit = (void (*)(void)) dlsym(clock_handle,
e11fcffc
MD
116 "lttng_ust_clock_plugin_init");
117 if (!libinit) {
118 PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()",
119 libname);
120 return;
121 }
122 libinit();
123}
This page took 0.027996 seconds and 4 git commands to generate.