chiark / gitweb /
Prep v238: Fix elogind_daemonize(), it failed due to some misunderstandings on my...
[elogind.git] / src / shared / musl_missing.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of elogind.
4
5   Copyright 2017 Sven Eden
6
7   elogind is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   elogind is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with elogind; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <string.h>
23
24 #include "alloc-util.h"
25
26 #ifndef __GLIBC__
27 char *program_invocation_name       = NULL;
28 char *program_invocation_short_name = NULL;
29 #endif // __GLIBC__
30
31 const char *program_arg_name = NULL;
32
33 #include "musl_missing.h"
34
35 static void elogind_free_program_name(void) {
36
37         if (program_invocation_name && (program_invocation_name != program_arg_name) && strlen(program_invocation_name))
38                 program_invocation_name       = mfree(program_invocation_name);
39         if (program_invocation_short_name && (program_invocation_short_name != program_arg_name) && strlen(program_invocation_short_name))
40                 program_invocation_short_name = mfree(program_invocation_short_name);
41 }
42
43 void elogind_set_program_name(const char* pcall) {
44         assert(pcall && pcall[0]);
45
46         program_arg_name = pcall;
47
48         if ( ( program_invocation_name
49             && strcmp(program_invocation_name, program_arg_name))
50           || ( program_invocation_short_name
51             && strcmp(program_invocation_short_name, basename(program_arg_name)) ) )
52                 elogind_free_program_name();
53
54         if (NULL == program_invocation_name)
55                 program_invocation_name       = strdup(program_arg_name);
56         if (NULL == program_invocation_short_name)
57                 program_invocation_short_name = strdup(basename(program_arg_name));
58 #ifndef __GLIBC__
59         atexit(elogind_free_program_name);
60 #endif // __GLIBC__
61 }
62