Coverage Report

Created: 2026-01-16 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/lib/util/util_process.c
Line
Count
Source
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
void process_set_title(const char *short_format, const char *long_format, ...)
39
0
{
40
0
#if defined(HAVE_PRCTL) && defined(PR_SET_NAME)
41
0
  if (short_format != NULL) {
42
0
    va_list ap;
43
44
0
    va_start(ap, long_format);
45
0
    vsnprintf(short_comment, sizeof(short_comment), short_format, ap);
46
0
    va_end(ap);
47
48
0
    prctl(PR_SET_NAME, (unsigned long) short_comment, 0, 0, 0);
49
0
  }
50
0
#endif
51
52
0
  if (long_format != NULL) {
53
0
    va_list ap;
54
55
0
    va_start(ap, long_format);
56
0
    vsnprintf(long_comment, sizeof(long_comment), long_format, ap);
57
0
    va_end(ap);
58
59
0
    setproctitle("%s", long_comment);
60
0
  }
61
0
}
62
63
const char *process_get_short_title(void)
64
0
{
65
0
  return short_comment;
66
0
}
67
68
const char *process_get_long_title(void)
69
0
{
70
0
  return long_comment;
71
0
}
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
void process_save_binary_name(const char *progname)
79
0
{
80
0
  strlcpy(binary_name, progname, sizeof(binary_name));
81
0
}
82
83
/* Samba binaries will set this during popt handling */
84
const char *process_get_saved_binary_name(void)
85
0
{
86
0
  return binary_name;
87
0
}
88
89
90
int prctl_set_comment(const char *comment_format, ...)
91
0
{
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
0
}