suricata
util-pidfile.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Utility code for dealing with a pidfile.
25  * Adaptation of Steve Grubbs patch to our coding guidelines
26  * (thanks for the patch Steve ;)
27  */
28 
29 #include "suricata-common.h"
30 #include "util-pidfile.h"
31 #include "util-debug.h"
32 #ifdef OS_WIN32
33 #include "win32-misc.h"
34 #endif /* OS_WIN32 */
35 
36 /**
37  * \brief Write a pid file (used at the startup)
38  * This commonly needed by the init scripts
39  *
40  * \param pointer to the name of the pid file to write (optarg)
41  *
42  * \retval 0 if succes
43  * \retval -1 on failure
44  */
45 int SCPidfileCreate(const char *pidfile)
46 {
47  SCEnter();
48 
49  int pidfd = 0;
50  char val[32];
51 
52  size_t len = snprintf(val, sizeof(val), "%"PRIuMAX"\n", (uintmax_t)getpid());
53  if (len <= 0) {
54  SCLogError("Pid error (%s)", strerror(errno));
55  SCReturnInt(-1);
56  }
57 
58  pidfd = open(pidfile, O_CREAT | O_TRUNC | O_NOFOLLOW | O_WRONLY, 0644);
59  if (pidfd < 0) {
60  SCLogError("unable to set pidfile '%s': %s", pidfile, strerror(errno));
61  SCReturnInt(-1);
62  }
63 
64  ssize_t r = write(pidfd, val, (unsigned int)len);
65  if (r == -1) {
66  SCLogError("unable to write pidfile: %s", strerror(errno));
67  close(pidfd);
68  SCReturnInt(-1);
69  } else if ((size_t)r != len) {
70  SCLogError("unable to write pidfile: wrote"
71  " %" PRIdMAX " of %" PRIuMAX " bytes.",
72  (intmax_t)r, (uintmax_t)len);
73  close(pidfd);
74  SCReturnInt(-1);
75  }
76 
77  close(pidfd);
78  SCReturnInt(0);
79 }
80 
81 /**
82  * \brief Remove the pid file (used at the startup)
83  *
84  * \param pointer to the name of the pid file to write (optarg)
85  */
86 void SCPidfileRemove(const char *pid_filename)
87 {
88  if (pid_filename != NULL) {
89  /* we ignore the result, the user may have removed the file already. */
90  (void)unlink(pid_filename);
91  }
92 }
93 
94 /**
95  * \brief Check the Suricata pid file (used at the startup)
96  *
97  * This commonly needed by the init scripts.
98  *
99  * This function will fail if the PID file exists, but tries to log a
100  * meaningful message if appears Suricata is running, or if the PID
101  * file appears to be stale.
102  *
103  * \param pointer to the name of the pid file to write (optarg)
104  *
105  * \retval 0 if succes
106  * \retval -1 on failure
107  */
108 int SCPidfileTestRunning(const char *pid_filename)
109 {
110  /* Check if the existing process is still alive. */
111  FILE *pf;
112 
113  pf = fopen(pid_filename, "r");
114  if (pf == NULL) {
115  if (access(pid_filename, F_OK) == 0) {
116  SCLogError("pid file '%s' exists and can not be read. Aborting!", pid_filename);
117  return -1;
118  } else {
119  return 0;
120  }
121  }
122 
123 #ifndef OS_WIN32
124  pid_t pidv;
125  if (fscanf(pf, "%d", &pidv) == 1 && kill(pidv, 0) == 0) {
126  SCLogError("pid file '%s' exists and Suricata appears to be running. "
127  "Aborting!",
128  pid_filename);
129  } else
130 #endif
131  {
132  SCLogError("pid file '%s' exists but appears stale. "
133  "Make sure Suricata is not running and then remove %s. "
134  "Aborting!",
135  pid_filename, pid_filename);
136  }
137 
138  fclose(pf);
139  return -1;
140 }
win32-misc.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
O_NOFOLLOW
#define O_NOFOLLOW
Definition: win32-misc.h:30
util-pidfile.h
util-debug.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
suricata-common.h
SCPidfileTestRunning
int SCPidfileTestRunning(const char *pid_filename)
Check the Suricata pid file (used at the startup)
Definition: util-pidfile.c:108
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCPidfileCreate
int SCPidfileCreate(const char *pidfile)
Write a pid file (used at the startup) This commonly needed by the init scripts.
Definition: util-pidfile.c:45
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCPidfileRemove
void SCPidfileRemove(const char *pid_filename)
Remove the pid file (used at the startup)
Definition: util-pidfile.c:86