1 /*
2 * Copyright (c) 2015-2018 Red Hat, Inc.
3 *
4 * All rights reserved.
5 *
6 * Author: Jan Friesse (jfriesse@redhat.com)
7 *
8 * This software licensed under BSD license, the text of which follows:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the Red Hat, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <arpa/inet.h>
38 #include <sys/stat.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <inttypes.h>
44 #include <libgen.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <syslog.h>
50
51 #include "utils.h"
52
53 /*
54 * Check string to value on, off, yes, no, 0, 1. Return 1 if value is on, yes or 1, 0 if
55 * value is off, no or 0 and -1 otherwise.
56 */
57 int
utils_parse_bool_str(const char * str)58 utils_parse_bool_str(const char *str)
59 {
60
61 if (strcasecmp(str, "yes") == 0 ||
62 strcasecmp(str, "on") == 0 ||
63 strcasecmp(str, "1") == 0) {
64 return (1);
65 } else if (strcasecmp(str, "no") == 0 ||
66 strcasecmp(str, "off") == 0 ||
67 strcasecmp(str, "0") == 0) {
68 return (0);
69 }
70
71 return (-1);
72 }
73
74 int
utils_flock(const char * lockfile,pid_t pid,int * another_instance_running)75 utils_flock(const char *lockfile, pid_t pid, int *another_instance_running)
76 {
77 struct flock lock;
78 char pid_s[17];
79 int fd_flag;
80 int lf;
81 char *dname;
82
83 *another_instance_running = 0;
84
85 /*
86 * lockfile directory may not exists. Creation of directory should
87 * be handled by initscript/tmpfiles.d. But as a last chance it
88 * make sense to try to create it here.
89 */
90 dname = strdup(lockfile);
91 if (dname != NULL) {
92 (void)mkdir(dirname(dname), 0770);
93 free(dname);
94 }
95
96 lf = open(lockfile, O_WRONLY | O_CREAT, 0640);
97 if (lf == -1) {
98 return (-1);
99 }
100
101 retry_fcntl:
102 lock.l_type = F_WRLCK;
103 lock.l_start = 0;
104 lock.l_whence = SEEK_SET;
105 lock.l_len = 0;
106 if (fcntl(lf, F_SETLK, &lock) == -1) {
107 switch (errno) {
108 case EINTR:
109 goto retry_fcntl;
110 break;
111 case EAGAIN:
112 case EACCES:
113 *another_instance_running = 1;
114 goto error_close;
115 break;
116 default:
117 goto error_close;
118 break;
119 }
120 }
121
122 if (ftruncate(lf, 0) == -1) {
123 goto error_close_unlink;
124 }
125
126 memset(pid_s, 0, sizeof(pid_s));
127 snprintf(pid_s, sizeof(pid_s) - 1, "%u\n", pid);
128
129 retry_write:
130 if (write(lf, pid_s, strlen(pid_s)) != (ssize_t)strlen(pid_s)) {
131 if (errno == EINTR) {
132 goto retry_write;
133 } else {
134 goto error_close_unlink;
135 }
136 }
137
138 if ((fd_flag = fcntl(lf, F_GETFD, 0)) == -1) {
139 goto error_close_unlink;
140 }
141 fd_flag |= FD_CLOEXEC;
142 if (fcntl(lf, F_SETFD, fd_flag) == -1) {
143 goto error_close_unlink;
144 }
145
146 return (lf);
147
148 error_close_unlink:
149 unlink(lockfile);
150 error_close:
151 close(lf);
152
153 return (-1);
154 }
155
156 void
utils_tty_detach(void)157 utils_tty_detach(void)
158 {
159 int devnull;
160
161 switch (fork()) {
162 case -1:
163 err(1, "Can't create child process");
164 break;
165 case 0:
166 break;
167 default:
168 exit(0);
169 break;
170 }
171
172 /* Create new session */
173 (void)setsid();
174
175 /*
176 * Map stdin/out/err to /dev/null.
177 */
178 devnull = open("/dev/null", O_RDWR);
179 if (devnull == -1) {
180 err(1, "Can't open /dev/null");
181 }
182
183 if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
184 || dup2(devnull, 2) < 0) {
185 close(devnull);
186 err(1, "Can't dup2 stdin/out/err to /dev/null");
187 }
188 close(devnull);
189 }
190
191 int
utils_fd_set_non_blocking(int fd)192 utils_fd_set_non_blocking(int fd)
193 {
194 int flags;
195
196 flags = fcntl(fd, F_GETFL, NULL);
197
198 if (flags < 0) {
199 return (-1);
200 }
201
202 flags |= O_NONBLOCK;
203 if (fcntl(fd, F_SETFL, flags) < 0) {
204 return (-1);
205 }
206
207 return (0);
208 }
209
210 /*
211 * Safer wrapper of strtoll. Return 0 on success, otherwise -1.
212 */
213 int
utils_strtonum(const char * str,long long int min_val,long long int max_val,long long int * res)214 utils_strtonum(const char *str, long long int min_val, long long int max_val,
215 long long int *res)
216 {
217 long long int tmp_ll;
218 char *ep;
219
220 if (min_val > max_val) {
221 return (-1);
222 }
223
224 errno = 0;
225
226 tmp_ll = strtoll(str, &ep, 10);
227 if (ep == str || *ep != '\0' || errno != 0) {
228 return (-1);
229 }
230
231 if (tmp_ll < min_val || tmp_ll > max_val) {
232 return (-1);
233 }
234
235 *res = tmp_ll;
236
237 return (0);
238 }
239