utils.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <inttypes.h>
  42. #include <libgen.h>
  43. #include <math.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <syslog.h>
  49. #include "utils.h"
  50. /*
  51. * Check string to value on, off, yes, no, 0, 1. Return 1 if value is on, yes or 1, 0 if
  52. * value is off, no or 0 and -1 otherwise.
  53. */
  54. int
  55. utils_parse_bool_str(const char *str)
  56. {
  57. if (strcasecmp(str, "yes") == 0 ||
  58. strcasecmp(str, "on") == 0 ||
  59. strcasecmp(str, "1") == 0) {
  60. return (1);
  61. } else if (strcasecmp(str, "no") == 0 ||
  62. strcasecmp(str, "off") == 0 ||
  63. strcasecmp(str, "0") == 0) {
  64. return (0);
  65. }
  66. return (-1);
  67. }
  68. int
  69. utils_flock(const char *lockfile, pid_t pid, int *another_instance_running)
  70. {
  71. struct flock lock;
  72. char pid_s[17];
  73. int fd_flag;
  74. int lf;
  75. char *dname;
  76. *another_instance_running = 0;
  77. /*
  78. * lockfile directory may not exists. Creation of directory should
  79. * be handled by initscript/tmpfiles.d. But as a last chance it
  80. * make sense to try to create it here.
  81. */
  82. dname = strdup(lockfile);
  83. if (dname != NULL) {
  84. (void)mkdir(dirname(dname), 0770);
  85. free(dname);
  86. }
  87. lf = open(lockfile, O_WRONLY | O_CREAT, 0640);
  88. if (lf == -1) {
  89. return (-1);
  90. }
  91. retry_fcntl:
  92. lock.l_type = F_WRLCK;
  93. lock.l_start = 0;
  94. lock.l_whence = SEEK_SET;
  95. lock.l_len = 0;
  96. if (fcntl(lf, F_SETLK, &lock) == -1) {
  97. switch (errno) {
  98. case EINTR:
  99. goto retry_fcntl;
  100. break;
  101. case EAGAIN:
  102. case EACCES:
  103. *another_instance_running = 1;
  104. goto error_close;
  105. break;
  106. default:
  107. goto error_close;
  108. break;
  109. }
  110. }
  111. if (ftruncate(lf, 0) == -1) {
  112. goto error_close_unlink;
  113. }
  114. memset(pid_s, 0, sizeof(pid_s));
  115. snprintf(pid_s, sizeof(pid_s) - 1, "%u\n", pid);
  116. retry_write:
  117. if (write(lf, pid_s, strlen(pid_s)) != (ssize_t)strlen(pid_s)) {
  118. if (errno == EINTR) {
  119. goto retry_write;
  120. } else {
  121. goto error_close_unlink;
  122. }
  123. }
  124. if ((fd_flag = fcntl(lf, F_GETFD, 0)) == -1) {
  125. goto error_close_unlink;
  126. }
  127. fd_flag |= FD_CLOEXEC;
  128. if (fcntl(lf, F_SETFD, fd_flag) == -1) {
  129. goto error_close_unlink;
  130. }
  131. return (lf);
  132. error_close_unlink:
  133. unlink(lockfile);
  134. error_close:
  135. close(lf);
  136. return (-1);
  137. }
  138. void
  139. utils_tty_detach(void)
  140. {
  141. int devnull;
  142. switch (fork()) {
  143. case -1:
  144. err(EXIT_FAILURE, "Can't create child process");
  145. break;
  146. case 0:
  147. break;
  148. default:
  149. exit(EXIT_SUCCESS);
  150. break;
  151. }
  152. /* Create new session */
  153. (void)setsid();
  154. /*
  155. * Map stdin/out/err to /dev/null.
  156. */
  157. devnull = open("/dev/null", O_RDWR);
  158. if (devnull == -1) {
  159. err(EXIT_FAILURE, "Can't open /dev/null");
  160. }
  161. if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
  162. || dup2(devnull, 2) < 0) {
  163. close(devnull);
  164. err(EXIT_FAILURE, "Can't dup2 stdin/out/err to /dev/null");
  165. }
  166. close(devnull);
  167. }
  168. int
  169. utils_fd_set_non_blocking(int fd)
  170. {
  171. int flags;
  172. flags = fcntl(fd, F_GETFL, NULL);
  173. if (flags < 0) {
  174. return (-1);
  175. }
  176. flags |= O_NONBLOCK;
  177. if (fcntl(fd, F_SETFL, flags) < 0) {
  178. return (-1);
  179. }
  180. return (0);
  181. }
  182. /*
  183. * Safer wrapper of strtoll. Return 0 on success, otherwise -1.
  184. */
  185. int
  186. utils_strtonum(const char *str, long long int min_val, long long int max_val,
  187. long long int *res)
  188. {
  189. long long int tmp_ll;
  190. char *ep;
  191. if (min_val > max_val) {
  192. return (-1);
  193. }
  194. errno = 0;
  195. tmp_ll = strtoll(str, &ep, 10);
  196. if (ep == str || *ep != '\0' || errno != 0) {
  197. return (-1);
  198. }
  199. if (tmp_ll < min_val || tmp_ll > max_val) {
  200. return (-1);
  201. }
  202. *res = tmp_ll;
  203. return (0);
  204. }
  205. /*
  206. * Safer wrapper of strtod. Return 0 on success, otherwise -1.
  207. */
  208. int
  209. utils_strtod(const char *str, double min_val, double max_val,
  210. double *res)
  211. {
  212. double tmp_d;
  213. char *ep;
  214. if (min_val > max_val) {
  215. return (-1);
  216. }
  217. errno = 0;
  218. tmp_d = strtod(str, &ep);
  219. if (ep == str || *ep != '\0' || errno != 0) {
  220. return (-1);
  221. }
  222. if (tmp_d < min_val || tmp_d > max_val || isnan(tmp_d)) {
  223. return (-1);
  224. }
  225. *res = tmp_d;
  226. return (0);
  227. }