ctdb-protocol: Fix typo in type of return variable
[samba.git] / ctdb / server / ctdb_mutex_fcntl_helper.c
1 /*
2    CTDB mutex fcntl lock file helper
3
4    Copyright (C) Martin Schwenke 2015
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23
24 #include "lib/util/sys_rw.h"
25
26 /* protocol.h is just needed for ctdb_sock_addr, which is used in system.h */
27 #include "protocol/protocol.h"
28 #include "common/system.h"
29
30 static char *progname = NULL;
31
32 static char fcntl_lock(const char *file, int *outfd)
33 {
34         int fd;
35         struct flock lock;
36
37         fd = open(file, O_RDWR|O_CREAT, 0600);
38         if (fd == -1) {
39                 fprintf(stderr, "%s: Unable to open %s - (%s)\n",
40                         progname, file, strerror(errno));
41                 return '3';
42         }
43
44         lock.l_type = F_WRLCK;
45         lock.l_whence = SEEK_SET;
46         lock.l_start = 0;
47         lock.l_len = 1;
48         lock.l_pid = 0;
49
50         if (fcntl(fd, F_SETLK, &lock) != 0) {
51                 int saved_errno = errno;
52                 close(fd);
53                 fd = -1;
54                 if (saved_errno == EACCES ||
55                     saved_errno == EAGAIN) {
56                         /* Lock contention, fail silently */
57                         return '1';
58                 }
59
60                 /* Log an error for any other failure */
61                 fprintf(stderr,
62                         "%s: Failed to get lock on '%s' - (%s)\n",
63                         progname, file, strerror(saved_errno));
64                 return '3';
65         }
66
67         *outfd = fd;
68
69         return '0';
70 }
71
72 int main(int argc, char *argv[])
73 {
74         char result;
75         int ppid;
76         const char *file = NULL;
77         int fd = -1;
78
79         progname = argv[0];
80
81         if (argc != 2) {
82                 fprintf(stderr, "Usage: %s <file>\n", progname);
83                 exit(1);
84         }
85
86         ppid = getppid();
87
88         if (ppid == 1) {
89                 /* The original parent is gone and the process has
90                  * been reparented to init.  This can happen if the
91                  * helper is started just as the parent is killed
92                  * during shutdown.  The error message doesn't need to
93                  * be stellar, since there won't be anything around to
94                  * capture and log it...
95                  */
96                 fprintf(stderr, "%s: PPID == 1\n", progname);
97                 exit(1);
98         }
99
100         file = argv[1];
101
102         result = fcntl_lock(file, &fd);
103         sys_write(STDOUT_FILENO, &result, 1);
104
105         ctdb_wait_for_process_to_exit(ppid);
106
107         if (fd != -1) {
108                 close(fd);
109         }
110
111         return 0;
112 }