4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright 2020, DataDirect Networks Storage.
31 int create_pid_file(const char *path)
33 char buf[3 * sizeof(long long) + 2];
38 fd = open(path, O_RDWR|O_CREAT|O_CLOEXEC, 0600);
40 fprintf(stderr, "%s: cannot open '%s': %s\n",
41 program_invocation_short_name, path, strerror(errno));
50 rc2 = fcntl(fd, F_SETLK, &fl);
52 fprintf(stderr, "%s: cannot lock '%s': %s\n",
53 program_invocation_short_name, path, strerror(errno));
57 rc2 = ftruncate(fd, 0);
59 fprintf(stderr, "%s: cannot truncate '%s': %s\n",
60 program_invocation_short_name, path, strerror(errno));
64 buf_len = snprintf(buf, sizeof(buf), "%lld\n", (long long)getpid());
65 rc2 = write(fd, buf, buf_len);
67 fprintf(stderr, "%s: cannot write '%s': %s\n",
68 program_invocation_short_name, path, strerror(errno));
72 if (rc2 < 0 && !(fd < 0)) {