Line data Source code
1 : /* 2 : * Unix SMB/CIFS implementation. 3 : * 4 : * Process utils. 5 : * 6 : * Copyright (c) 2013 Andreas Schneider <asn@samba.org> 7 : * 8 : * This program is free software; you can redistribute it and/or modify 9 : * it under the terms of the GNU General Public License as published by 10 : * the Free Software Foundation; either version 3 of the License, or 11 : * (at your option) any later version. 12 : * 13 : * This program is distributed in the hope that it will be useful, 14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : * GNU General Public License for more details. 17 : * 18 : * You should have received a copy of the GNU General Public License 19 : * along with this program; if not, see <http://www.gnu.org/licenses/>. 20 : */ 21 : 22 : #include "util_process.h" 23 : #include "replace.h" 24 : 25 : #ifdef HAVE_SYS_PRCTL_H 26 : #include <sys/prctl.h> 27 : #endif 28 : 29 : /* 30 : * These variables are static so that we can access them 31 : * with process_get_short_title() and process_get_long_title(). The 32 : * purpose of this is to allow smb_panic_log() to print them. 33 : */ 34 : static char short_comment[16] = {0,}; 35 : static char long_comment[256] = {0,}; 36 : static char binary_name[256]; 37 : 38 48216 : void process_set_title(const char *short_format, const char *long_format, ...) 39 : { 40 : #if defined(HAVE_PRCTL) && defined(PR_SET_NAME) 41 48216 : if (short_format != NULL) { 42 902 : va_list ap; 43 : 44 48216 : va_start(ap, long_format); 45 48216 : vsnprintf(short_comment, sizeof(short_comment), short_format, ap); 46 48216 : va_end(ap); 47 : 48 48216 : prctl(PR_SET_NAME, (unsigned long) short_comment, 0, 0, 0); 49 : } 50 : #endif 51 : 52 48216 : if (long_format != NULL) { 53 902 : va_list ap; 54 : 55 48216 : va_start(ap, long_format); 56 48216 : vsnprintf(long_comment, sizeof(long_comment), long_format, ap); 57 48216 : va_end(ap); 58 : 59 48216 : setproctitle("%s", long_comment); 60 : } 61 48216 : } 62 : 63 0 : const char *process_get_short_title(void) 64 : { 65 0 : return short_comment; 66 : } 67 : 68 0 : const char *process_get_long_title(void) 69 : { 70 0 : return long_comment; 71 : } 72 : 73 : /* 74 : * This is just for debugging in a panic, so we don't want to do 75 : * anything more than return a fixed pointer, so we save a copy to a 76 : * static variable. 77 : */ 78 27082 : void process_save_binary_name(const char *progname) 79 : { 80 27082 : strlcpy(binary_name, progname, sizeof(binary_name)); 81 27082 : } 82 : 83 : /* Samba binaries will set this during popt handling */ 84 0 : const char *process_get_saved_binary_name(void) 85 : { 86 0 : return binary_name; 87 : } 88 : 89 : 90 0 : int prctl_set_comment(const char *comment_format, ...) 91 : { 92 0 : char comment[16]; 93 0 : va_list ap; 94 : 95 0 : va_start(ap, comment_format); 96 0 : vsnprintf(comment, sizeof(comment), comment_format, ap); 97 0 : va_end(ap); 98 : 99 0 : process_set_title("%s", "%s", comment); 100 0 : return 0; 101 : }