utils.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2015-2020 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. #include <sys/types.h>
  35. #include <sys/file.h>
  36. #include <arpa/inet.h>
  37. #include <sys/stat.h>
  38. #include <err.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <grp.h>
  42. #include <inttypes.h>
  43. #include <libgen.h>
  44. #include <limits.h>
  45. #include <math.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50. #include <syslog.h>
  51. #include "utils.h"
  52. #define UTILS_GID_DETERMINE_MAX_SIZE 65536
  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
  58. utils_parse_bool_str(const char *str)
  59. {
  60. if (strcasecmp(str, "yes") == 0 ||
  61. strcasecmp(str, "on") == 0 ||
  62. strcasecmp(str, "1") == 0) {
  63. return (1);
  64. } else if (strcasecmp(str, "no") == 0 ||
  65. strcasecmp(str, "off") == 0 ||
  66. strcasecmp(str, "0") == 0) {
  67. return (0);
  68. }
  69. return (-1);
  70. }
  71. int
  72. utils_flock(const char *lockfile, pid_t pid, int *another_instance_running)
  73. {
  74. struct flock lock;
  75. char pid_s[17];
  76. int fd_flag;
  77. int lf;
  78. char *dname;
  79. *another_instance_running = 0;
  80. /*
  81. * lockfile directory may not exists. Creation of directory should
  82. * be handled by initscript/tmpfiles.d. But as a last chance it
  83. * make sense to try to create it here.
  84. */
  85. dname = strdup(lockfile);
  86. if (dname != NULL) {
  87. (void)mkdir(dirname(dname), 0770);
  88. free(dname);
  89. }
  90. lf = open(lockfile, O_WRONLY | O_CREAT, 0640);
  91. if (lf == -1) {
  92. return (-1);
  93. }
  94. retry_fcntl:
  95. lock.l_type = F_WRLCK;
  96. lock.l_start = 0;
  97. lock.l_whence = SEEK_SET;
  98. lock.l_len = 0;
  99. if (fcntl(lf, F_SETLK, &lock) == -1) {
  100. switch (errno) {
  101. case EINTR:
  102. goto retry_fcntl;
  103. break;
  104. case EAGAIN:
  105. case EACCES:
  106. *another_instance_running = 1;
  107. goto error_close;
  108. break;
  109. default:
  110. goto error_close;
  111. break;
  112. }
  113. }
  114. if (ftruncate(lf, 0) == -1) {
  115. goto error_close_unlink;
  116. }
  117. memset(pid_s, 0, sizeof(pid_s));
  118. snprintf(pid_s, sizeof(pid_s) - 1, "%u\n", pid);
  119. retry_write:
  120. if (write(lf, pid_s, strlen(pid_s)) != (ssize_t)strlen(pid_s)) {
  121. if (errno == EINTR) {
  122. goto retry_write;
  123. } else {
  124. goto error_close_unlink;
  125. }
  126. }
  127. if ((fd_flag = fcntl(lf, F_GETFD, 0)) == -1) {
  128. goto error_close_unlink;
  129. }
  130. fd_flag |= FD_CLOEXEC;
  131. if (fcntl(lf, F_SETFD, fd_flag) == -1) {
  132. goto error_close_unlink;
  133. }
  134. return (lf);
  135. error_close_unlink:
  136. unlink(lockfile);
  137. error_close:
  138. close(lf);
  139. return (-1);
  140. }
  141. void
  142. utils_tty_detach(void)
  143. {
  144. int devnull;
  145. switch (fork()) {
  146. case -1:
  147. err(EXIT_FAILURE, "Can't create child process");
  148. break;
  149. case 0:
  150. break;
  151. default:
  152. exit(EXIT_SUCCESS);
  153. break;
  154. }
  155. /* Create new session */
  156. (void)setsid();
  157. /*
  158. * Map stdin/out/err to /dev/null.
  159. */
  160. devnull = open("/dev/null", O_RDWR);
  161. if (devnull == -1) {
  162. err(EXIT_FAILURE, "Can't open /dev/null");
  163. }
  164. if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
  165. || dup2(devnull, 2) < 0) {
  166. close(devnull);
  167. err(EXIT_FAILURE, "Can't dup2 stdin/out/err to /dev/null");
  168. }
  169. close(devnull);
  170. }
  171. int
  172. utils_fd_set_non_blocking(int fd)
  173. {
  174. int flags;
  175. flags = fcntl(fd, F_GETFL, NULL);
  176. if (flags < 0) {
  177. return (-1);
  178. }
  179. flags |= O_NONBLOCK;
  180. if (fcntl(fd, F_SETFL, flags) < 0) {
  181. return (-1);
  182. }
  183. return (0);
  184. }
  185. /*
  186. * Safer wrapper of strtoll. Return 0 on success, otherwise -1.
  187. */
  188. int
  189. utils_strtonum_base(const char *str, long long int min_val, long long int max_val,
  190. int base, long long int *res)
  191. {
  192. long long int tmp_ll;
  193. char *ep;
  194. if (min_val > max_val) {
  195. return (-1);
  196. }
  197. errno = 0;
  198. tmp_ll = strtoll(str, &ep, base);
  199. if (ep == str || *ep != '\0' || errno != 0) {
  200. return (-1);
  201. }
  202. if (tmp_ll < min_val || tmp_ll > max_val) {
  203. return (-1);
  204. }
  205. *res = tmp_ll;
  206. return (0);
  207. }
  208. /*
  209. * Shortcut for decimal utils_strtonum_base
  210. */
  211. int
  212. utils_strtonum(const char *str, long long int min_val, long long int max_val,
  213. long long int *res)
  214. {
  215. return (utils_strtonum_base(str, min_val, max_val, 10, res));
  216. }
  217. /*
  218. * Safer wrapper of strtod. Return 0 on success, otherwise -1.
  219. */
  220. int
  221. utils_strtod(const char *str, double min_val, double max_val,
  222. double *res)
  223. {
  224. double tmp_d;
  225. char *ep;
  226. if (min_val > max_val) {
  227. return (-1);
  228. }
  229. errno = 0;
  230. tmp_d = strtod(str, &ep);
  231. if (ep == str || *ep != '\0' || errno != 0) {
  232. return (-1);
  233. }
  234. if (tmp_d < min_val || tmp_d > max_val || isnan(tmp_d)) {
  235. return (-1);
  236. }
  237. *res = tmp_d;
  238. return (0);
  239. }
  240. /*
  241. * Check if string is empty and set set_umask to 0, or parse string
  242. * as umask octal number and set set_umask to 1 and umask value.
  243. * Return 0 on success, otherwise -1.
  244. */
  245. int
  246. utils_parse_umask(const char *str, int *set_umask, mode_t *umask)
  247. {
  248. long long int tmpll;
  249. if (strcmp(str, "") == 0) {
  250. *set_umask = 0;
  251. } else {
  252. if (utils_strtonum_base(str, 0, S_IRWXU|S_IRWXG|S_IRWXO, 8, &tmpll) == -1) {
  253. return (-1);
  254. }
  255. *set_umask = 1;
  256. *umask = (mode_t)tmpll;
  257. }
  258. return (0);
  259. }
  260. /*
  261. * Get gid of group with grp_name name. On success, 0 is returned and
  262. * gid contains valid group id or -1 if grp_name is empty string or -1.
  263. * In case of error including group doesn't exist -1 is returned and
  264. * gid is not changed.
  265. */
  266. int
  267. utils_get_group_gid(const char *grp_name, gid_t *gid)
  268. {
  269. long long int tmpll;
  270. char *grp_buf, *tmp_buf;
  271. long int grp_buf_size;
  272. int res;
  273. struct group grp, *grp_res;
  274. int ret;
  275. ret = 0;
  276. if (strcmp(grp_name, "") == 0) {
  277. *gid = (gid_t)-1;
  278. return (0);
  279. }
  280. if (utils_strtonum(grp_name, -1, UINT_MAX, &tmpll) == 0) {
  281. *gid = (gid_t)tmpll;
  282. return (0);
  283. }
  284. grp_buf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
  285. if (grp_buf_size == -1) {
  286. grp_buf_size = 4096;
  287. }
  288. grp_buf = malloc(grp_buf_size);
  289. if (grp_buf == NULL) {
  290. return (-1);
  291. }
  292. while ((res = getgrnam_r(grp_name, &grp, grp_buf, grp_buf_size, &grp_res)) == ERANGE) {
  293. grp_buf_size *= 2;
  294. if (grp_buf_size > UTILS_GID_DETERMINE_MAX_SIZE) {
  295. ret = -1;
  296. goto exit_free_grp_buf;
  297. }
  298. tmp_buf = realloc(grp_buf, grp_buf_size);
  299. if (tmp_buf == NULL) {
  300. ret = -1;
  301. goto exit_free_grp_buf;
  302. }
  303. grp_buf = tmp_buf;
  304. }
  305. if (res != 0) {
  306. ret = -1;
  307. goto exit_free_grp_buf;
  308. }
  309. if (grp_res == NULL) {
  310. ret = -1;
  311. goto exit_free_grp_buf;
  312. } else {
  313. *gid = grp.gr_gid;
  314. }
  315. exit_free_grp_buf:
  316. free(grp_buf);
  317. return (ret);
  318. }