Coverage Report

Created: 2024-05-20 01:00

/src/openiked-portable/compat/setproctitle.c
Line
Count
Source (jump to first uncovered line)
1
/* Based on conf.c from UCB sendmail 8.8.8 */
2
3
/*
4
 * Copyright 2003 Damien Miller
5
 * Copyright (c) 1983, 1995-1997 Eric P. Allman
6
 * Copyright (c) 1988, 1993
7
 *  The Regents of the University of California.  All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 */
33
34
#include "openbsd-compat.h"
35
36
#ifndef HAVE_SETPROCTITLE
37
38
#include <stdarg.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
#ifdef HAVE_SYS_PSTAT_H
43
#include <sys/pstat.h>
44
#endif
45
#include <string.h>
46
47
#include <vis.h>
48
49
#define SPT_NONE  0 /* don't use it at all */
50
#define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
51
#define SPT_REUSEARGV 2 /* cover argv with title information */
52
53
#ifndef SPT_TYPE
54
# define SPT_TYPE SPT_NONE
55
#endif
56
57
#ifndef SPT_PADCHAR
58
0
# define SPT_PADCHAR  '\0'
59
#endif
60
61
#if SPT_TYPE == SPT_REUSEARGV
62
static char *argv_start = NULL;
63
static size_t argv_env_len = 0;
64
#endif
65
66
#endif /* HAVE_SETPROCTITLE */
67
68
void
69
compat_init_setproctitle(int argc, char *argv[])
70
0
{
71
0
#if !defined(HAVE_SETPROCTITLE) && \
72
0
    defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
73
0
  extern char **environ;
74
0
  char *lastargv = NULL;
75
0
  char **envp = environ;
76
0
  int i;
77
78
  /*
79
   * NB: This assumes that argv has already been copied out of the
80
   * way. This is true for sshd, but may not be true for other
81
   * programs. Beware.
82
   */
83
84
0
  if (argc == 0 || argv[0] == NULL)
85
0
    return;
86
87
  /* Fail if we can't allocate room for the new environment */
88
0
  for (i = 0; envp[i] != NULL; i++)
89
0
    ;
90
0
  if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
91
0
    environ = envp; /* put it back */
92
0
    return;
93
0
  }
94
95
  /*
96
   * Find the last argv string or environment variable within
97
   * our process memory area.
98
   */
99
0
  for (i = 0; i < argc; i++) {
100
0
    if (lastargv == NULL || lastargv + 1 == argv[i])
101
0
      lastargv = argv[i] + strlen(argv[i]);
102
0
  }
103
0
  for (i = 0; envp[i] != NULL; i++) {
104
0
    if (lastargv + 1 == envp[i])
105
0
      lastargv = envp[i] + strlen(envp[i]);
106
0
  }
107
108
0
  argv[1] = NULL;
109
0
  argv_start = argv[0];
110
0
  argv_env_len = lastargv - argv[0] - 1;
111
112
  /*
113
   * Copy environment
114
   * XXX - will truncate env on strdup fail
115
   */
116
0
  for (i = 0; envp[i] != NULL; i++)
117
0
    environ[i] = strdup(envp[i]);
118
0
  environ[i] = NULL;
119
0
#endif /* SPT_REUSEARGV */
120
0
}
121
122
#ifndef HAVE_SETPROCTITLE
123
void
124
setproctitle(const char *fmt, ...)
125
0
{
126
0
#if SPT_TYPE != SPT_NONE
127
0
  va_list ap;
128
0
  char buf[1024], ptitle[1024];
129
0
  size_t len = 0;
130
0
  int r;
131
0
  extern char *__progname;
132
#if SPT_TYPE == SPT_PSTAT
133
  union pstun pst;
134
#endif
135
136
0
#if SPT_TYPE == SPT_REUSEARGV
137
0
  if (argv_env_len <= 0)
138
0
    return;
139
0
#endif
140
141
0
  strlcpy(buf, __progname, sizeof(buf));
142
143
0
  r = -1;
144
0
  va_start(ap, fmt);
145
0
  if (fmt != NULL) {
146
0
    len = strlcat(buf, ": ", sizeof(buf));
147
0
    if (len < sizeof(buf))
148
0
      r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
149
0
  }
150
0
  va_end(ap);
151
0
  if (r == -1 || (size_t)r >= sizeof(buf) - len)
152
0
    return;
153
0
  strnvis(ptitle, buf, sizeof(ptitle),
154
0
      VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL);
155
156
#if SPT_TYPE == SPT_PSTAT
157
  pst.pst_command = ptitle;
158
  pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
159
#elif SPT_TYPE == SPT_REUSEARGV
160
/*  debug("setproctitle: copy \"%s\" into len %d",
161
      buf, argv_env_len); */
162
0
  len = strlcpy(argv_start, ptitle, argv_env_len);
163
0
  for(; len < argv_env_len; len++)
164
0
    argv_start[len] = SPT_PADCHAR;
165
0
#endif
166
167
0
#endif /* SPT_NONE */
168
0
}
169
170
#endif /* HAVE_SETPROCTITLE */