]> git.sesse.net Git - vlc/blob - src/misc/linux_specific.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / linux_specific.c
1 /*****************************************************************************
2  * linux_specific.c: Linux-specific initialization
3  *****************************************************************************
4  * Copyright © 2008 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <vlc_common.h>
29 #include "../libvlc.h"
30
31 static const char default_path[] = PKGLIBDIR;
32
33 static void set_libvlc_path (void)
34 {
35     /* Find the path to libvlc (i.e. ourselves) */
36     FILE *maps = fopen ("/proc/self/maps", "rt");
37     if (maps == NULL)
38         goto error;
39
40     char *line = NULL;
41     size_t linelen = 0;
42     uintptr_t needle = (uintptr_t)set_libvlc_path;
43
44     for (;;)
45     {
46         ssize_t len = getline (&line, &linelen, maps);
47         if (len == -1)
48             break;
49
50         void *start, *end;
51         if (sscanf (line, "%p-%p", &start, &end) < 2)
52             continue;
53         if (needle < (uintptr_t)start || (uintptr_t)end <= needle)
54             continue;
55         char *dir = strchr (line, '/');
56         if (dir == NULL)
57             continue;
58         char *file = strrchr (line, '/');
59         if (end == NULL)
60             continue;
61         *file = '\0';
62         if (asprintf (&psz_vlcpath, "%s/"PACKAGE, dir) == -1)
63             goto error;
64         break;
65     }
66     free (line);
67     fclose (maps);
68     return;
69
70 error:
71     psz_vlcpath = (char *)default_path; /* default, cannot fail */
72 }
73
74 static void unset_libvlc_path (void)
75 {
76     if (psz_vlcpath != default_path)
77         free (psz_vlcpath);
78 }
79
80 static struct
81 {
82     vlc_mutex_t lock;
83     unsigned refs;
84 } once = { VLC_STATIC_MUTEX, 0 };
85
86 #ifdef __GLIBC__
87 # include <gnu/libc-version.h>
88 # include <stdlib.h>
89 #endif
90
91 void system_Init (libvlc_int_t *libvlc, int *argc, const char *argv[])
92 {
93 #ifdef __GLIBC__
94     const char *glcv = gnu_get_libc_version ();
95
96     /* gettext in glibc 2.5-2.7 is not thread-safe. LibVLC keeps crashing,
97      * especially in sterror_r(). Even if we have NLS disabled, the calling
98      * process might have called setlocale(). */
99     if (strverscmp (glcv, "2.5") >= 0 && strverscmp (glcv, "2.8") < 0)
100     {
101         fputs ("LibVLC has detected an unusable buggy GNU/libc version.\n"
102                "Please update to version 2.8 or newer.\n", stderr);
103         fflush (stderr);
104 #ifndef DISABLE_BUGGY_GLIBC_CHECK
105         abort ();
106 #endif
107     }
108 #endif
109
110     vlc_mutex_lock (&once.lock);
111     if (once.refs++ == 0)
112         set_libvlc_path ();
113     vlc_mutex_unlock (&once.lock);
114
115     (void)libvlc; (void)argc; (void)argv;
116 }
117
118 void system_Configure (libvlc_int_t *libvlc,
119                        int argc, const char *const argv[])
120 {
121     (void)libvlc; (void)argc; (void)argv;
122 }
123
124 void system_End (libvlc_int_t *libvlc)
125 {
126     vlc_mutex_lock (&once.lock);
127     if (--once.refs == 0)
128         unset_libvlc_path ();
129     vlc_mutex_unlock (&once.lock);
130
131     (void)libvlc;
132 }