]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libbsd/libbsd/lib/contrib/src/setproctitle_ctor.c
Update
[l4.git] / l4 / pkg / libbsd / libbsd / lib / contrib / src / setproctitle_ctor.c
1 /*
2  * Copyright © 2013 Guillem Jover <guillem@hadrons.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
18  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <unistd.h>
28
29 /*
30  * The automatic initialization cannot be part of the main shared library,
31  * because there is no thread-safe way to change the environ global
32  * variable. This is not a problem if the initializaion happens just at
33  * program load time, but becomes one if the shared library is directly or
34  * indirectly dlopen()ed during the execution of the program, which could
35  * have either kept references to the old environ or could change it in
36  * some other thread. This has been observed for example on systems using
37  * Samba NSS modules.
38  *
39  * To avoid any other possible fallout, the constructor is split into a
40  * new static library that needs to be linked explicitly into programs
41  * using setproctitle(). As an additional safety measure the pkg-config
42  * linker flags will mark the program as not allowing to be dlopen()ed
43  * so that we make sure to avoid the problem described above.
44  */
45
46 /*
47  * Force setproctitle_init() function into the .init_array section instead of
48  * expecting either the compiler to place constructors there or the linker to
49  * move them from .ctors to .init_array.
50  */
51 void (*libbsd_init_func)(int argc, char *argv[], char *envp[])
52         __attribute__((section(".init_array"))) = setproctitle_init;