Branch data Line data Source code
1 : : /* 2 : : * libdpkg - Debian packaging suite library routines 3 : : * progname.c - program name handling functions 4 : : * 5 : : * Copyright © 2011 Guillem Jover <guillem@debian.org> 6 : : * 7 : : * This is free software; you can redistribute it and/or modify 8 : : * it under the terms of the GNU General Public License as published by 9 : : * the Free Software Foundation; either version 2 of the License, or 10 : : * (at your option) any later version. 11 : : * 12 : : * This is distributed in the hope that it will be useful, 13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : : * GNU General Public License for more details. 16 : : * 17 : : * You should have received a copy of the GNU General Public License 18 : : * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 : : */ 20 : : 21 : : #include <config.h> 22 : : #include <compat.h> 23 : : 24 : : #ifdef HAVE_GETPROCS64 25 : : #include <sys/types.h> 26 : : #include <string.h> 27 : : #include <unistd.h> 28 : : #include <procinfo.h> 29 : : #endif 30 : : #include <errno.h> 31 : : #include <stdlib.h> 32 : : 33 : : #include <dpkg/path.h> 34 : : #include <dpkg/progname.h> 35 : : 36 : : static const char *progname; 37 : : 38 : : /** 39 : : * Set the program name. 40 : : * 41 : : * This function will set the program name which will be used by error 42 : : * reporting functions, among others. 43 : : * 44 : : * @param name The new program name. 45 : : */ 46 : : void 47 : 373 : dpkg_set_progname(const char *name) 48 : : { 49 : 373 : progname = path_basename(name); 50 : 373 : } 51 : : 52 : : #if defined(HAVE___PROGNAME) 53 : : extern const char *__progname; 54 : : #endif 55 : : 56 : : /** 57 : : * Get the program name. 58 : : * 59 : : * The program name might have been set previously by dpkg_set_progname(), 60 : : * but if not this function will try to initialize it by system dependent 61 : : * means, so it's half safe to not call dpkg_set_progname() at all. At worst 62 : : * the function might return NULL in that case. 63 : : * 64 : : * @return A pointer to a static buffer with the program name. 65 : : */ 66 : : const char * 67 : 234 : dpkg_get_progname(void) 68 : : { 69 [ + + ]: 234 : if (progname == NULL) { 70 : : #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME) 71 : 9 : progname = program_invocation_short_name; 72 : : #elif defined(HAVE___PROGNAME) 73 : : progname = __progname; 74 : : #elif defined(HAVE_GETPROGNAME) 75 : : progname = getprogname(); 76 : : #elif defined(HAVE_GETEXECNAME) 77 : : /* getexecname(3) returns an absolute path, normalize it. */ 78 : : dpkg_set_progname(getexecname()); 79 : : #elif defined(HAVE_GETPROCS64) 80 : : struct procentry64 pe; 81 : : pid_t pid = getpid(); 82 : : 83 : : if (getprocs64(&pe, sizeof(pe), NULL, 0, &pid, 1) >= 0) 84 : : progname = strdup(pe.pi_comm); 85 : : #endif 86 : : } 87 : : 88 : 234 : return progname; 89 : : }