utils.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2015-2016 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 <err.h>
  38. #include <errno.h>
  39. #include <inttypes.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <syslog.h>
  45. #include "utils.h"
  46. /*
  47. * Check string to value on, off, yes, no, 0, 1. Return 1 if value is on, yes or 1, 0 if
  48. * value is off, no or 0 and -1 otherwise.
  49. */
  50. int
  51. utils_parse_bool_str(const char *str)
  52. {
  53. if (strcasecmp(str, "yes") == 0 ||
  54. strcasecmp(str, "on") == 0 ||
  55. strcasecmp(str, "1") == 0) {
  56. return (1);
  57. } else if (strcasecmp(str, "no") == 0 ||
  58. strcasecmp(str, "off") == 0 ||
  59. strcasecmp(str, "0") == 0) {
  60. return (0);
  61. }
  62. return (-1);
  63. }
  64. int
  65. utils_flock(const char *lockfile, pid_t pid, int *another_instance_running)
  66. {
  67. struct flock lock;
  68. char pid_s[17];
  69. int fd_flag;
  70. int lf;
  71. *another_instance_running = 0;
  72. lf = open(lockfile, O_WRONLY | O_CREAT, 0640);
  73. if (lf == -1) {
  74. return (-1);
  75. }
  76. retry_fcntl:
  77. lock.l_type = F_WRLCK;
  78. lock.l_start = 0;
  79. lock.l_whence = SEEK_SET;
  80. lock.l_len = 0;
  81. if (fcntl(lf, F_SETLK, &lock) == -1) {
  82. switch (errno) {
  83. case EINTR:
  84. goto retry_fcntl;
  85. break;
  86. case EAGAIN:
  87. case EACCES:
  88. *another_instance_running = 1;
  89. goto error_close;
  90. break;
  91. default:
  92. goto error_close;
  93. break;
  94. }
  95. }
  96. if (ftruncate(lf, 0) == -1) {
  97. goto error_close_unlink;
  98. }
  99. memset(pid_s, 0, sizeof(pid_s));
  100. snprintf(pid_s, sizeof(pid_s) - 1, "%u\n", pid);
  101. retry_write:
  102. if (write(lf, pid_s, strlen(pid_s)) != strlen(pid_s)) {
  103. if (errno == EINTR) {
  104. goto retry_write;
  105. } else {
  106. goto error_close_unlink;
  107. }
  108. }
  109. if ((fd_flag = fcntl(lf, F_GETFD, 0)) == -1) {
  110. goto error_close_unlink;
  111. }
  112. fd_flag |= FD_CLOEXEC;
  113. if (fcntl(lf, F_SETFD, fd_flag) == -1) {
  114. goto error_close_unlink;
  115. }
  116. return (lf);
  117. error_close_unlink:
  118. unlink(lockfile);
  119. error_close:
  120. close(lf);
  121. return (-1);
  122. }
  123. void
  124. utils_tty_detach(void)
  125. {
  126. int devnull;
  127. switch (fork()) {
  128. case -1:
  129. err(1, "Can't create child process");
  130. break;
  131. case 0:
  132. break;
  133. default:
  134. exit(0);
  135. break;
  136. }
  137. /* Create new session */
  138. (void)setsid();
  139. /*
  140. * Map stdin/out/err to /dev/null.
  141. */
  142. devnull = open("/dev/null", O_RDWR);
  143. if (devnull == -1) {
  144. err(1, "Can't open /dev/null");
  145. }
  146. if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
  147. || dup2(devnull, 2) < 0) {
  148. close(devnull);
  149. err(1, "Can't dup2 stdin/out/err to /dev/null");
  150. }
  151. close(devnull);
  152. }