]>
git.ipfire.org Git - thirdparty/rng-tools.git/blob - util.c
3 * Copyright 2009 Red Hat, Inc.
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 #error Invalid or missing autoconf build environment
26 #include "rng-tools-config.h"
28 #include <sys/types.h>
36 int write_pid_file(const char *pid_fn
)
45 sprintf(str
, "%u\n", (unsigned int) getpid());
47 /* open non-exclusively (works on NFS v2) */
48 fd
= open(pid_fn
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
52 message(LOG_DAEMON
|LOG_ERR
, "Cannot open PID file %s: %s",
53 pid_fn
, strerror(err
));
58 memset(&lock
, 0, sizeof(lock
));
59 lock
.l_type
= F_WRLCK
;
60 lock
.l_whence
= SEEK_SET
;
61 if (fcntl(fd
, F_SETLK
, &lock
) != 0) {
64 message(LOG_DAEMON
|LOG_ERR
, "PID file %s is already locked",
67 message(LOG_DAEMON
|LOG_ERR
, "Cannot lock PID file %s: %s",
68 pid_fn
, strerror(err
));
78 ssize_t rc
= write(fd
, s
, bytes
);
81 message(LOG_DAEMON
|LOG_ERR
, "PID number write failed: %s",
90 /* make sure file data is written to disk */
93 message(LOG_DAEMON
|LOG_ERR
, "PID file fsync failed: %s", strerror(err
));