Coverage Report

Created: 2024-09-30 06:24

/src/proftpd/src/pidfile.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 2007-2020 The ProFTPD Project team
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18
 *
19
 * As a special exemption, The ProFTPD Project team and other respective
20
 * copyright holders give permission to link this program with OpenSSL, and
21
 * distribute the resulting executable, without including the source code for
22
 * OpenSSL in the source distribution.
23
 */
24
25
/* Pidfile management */
26
27
#include "conf.h"
28
#include "privs.h"
29
30
static const char *pidfile_path = PR_PID_FILE_PATH;
31
32
0
const char *pr_pidfile_get(void) {
33
0
  return pidfile_path;
34
0
}
35
36
0
int pr_pidfile_set(const char *path) {
37
0
  if (path == NULL) {
38
0
    errno = EINVAL;
39
0
    return -1;
40
0
  }
41
42
  /* Do not allow relative paths. */
43
0
  if (*path != '/') {
44
0
    errno = EINVAL;
45
0
    return -1;
46
0
  }
47
48
0
  pidfile_path = pstrdup(permanent_pool, path);
49
0
  return 0;
50
0
}
51
52
0
int pr_pidfile_write(void) {
53
0
  int fd, res, xerrno;
54
0
  mode_t mode = 0644;
55
56
0
  PRIVS_ROOT
57
0
  fd = open(pidfile_path, O_WRONLY|O_CREAT|O_TRUNC, mode);
58
0
  xerrno = errno;
59
0
  PRIVS_RELINQUISH
60
61
0
  if (fd < 0) {
62
0
    errno = xerrno;
63
0
    return -1;
64
0
  }
65
66
  /* Ensure that our permissions are as desired, regardless of umask. */
67
0
  PRIVS_ROOT
68
0
  res = fchmod(fd, mode);
69
0
  xerrno = errno;
70
0
  PRIVS_RELINQUISH
71
72
0
  if (res < 0) {
73
0
    fprintf(stderr, "error setting permissions for PidFile '%s': %s\n",
74
0
      pidfile_path, strerror(xerrno));
75
0
  }
76
77
0
  dprintf(fd, "%lu\n", (unsigned long) getpid());
78
0
  if (close(fd) < 0) {
79
0
    fprintf(stderr, "error writing PidFile '%s': %s\n", pidfile_path,
80
0
      strerror(errno));
81
0
  }
82
83
0
  return 0;
84
0
}
85
86
0
int pr_pidfile_remove(void) {
87
0
  return unlink(pidfile_path);
88
0
}