util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "util.h"
  15. #include "log.h"
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. void msleep(unsigned int msecs)
  24. {
  25. int err;
  26. struct timespec time;
  27. time.tv_sec = 0;
  28. while (msecs >= 1000) {
  29. time.tv_sec++;
  30. msecs -= 1000;
  31. }
  32. time.tv_nsec = msecs;
  33. time.tv_nsec *= 1000000;
  34. do {
  35. err = nanosleep(&time, &time);
  36. } while (err && errno == EINTR);
  37. if (err) {
  38. fprintf(stderr, "nanosleep() failed with: %s\n",
  39. strerror(errno));
  40. }
  41. }
  42. char * string_strip(char *str)
  43. {
  44. char *start = str;
  45. size_t len;
  46. if (!str)
  47. return NULL;
  48. while (*start != '\0' && isspace(*start))
  49. start++;
  50. len = strlen(start);
  51. while (len && isspace(start[len - 1])) {
  52. start[len - 1] = '\0';
  53. len--;
  54. }
  55. return start;
  56. }
  57. char * string_split(char *str, int (*sep_match)(int c))
  58. {
  59. char c;
  60. if (!str)
  61. return NULL;
  62. for (c = *str; c != '\0'; c = *str) {
  63. if (sep_match(c)) {
  64. *str = '\0';
  65. return str + 1;
  66. }
  67. str++;
  68. }
  69. return NULL;
  70. }
  71. uint_fast8_t tiny_hash(const char *str)
  72. {
  73. uint8_t c, hash = 21;
  74. while ((c = *str++) != '\0')
  75. hash = ((hash << 3) + hash) + c;
  76. return hash;
  77. }
  78. pid_t subprocess_exec(const char *_command)
  79. {
  80. char command_buf[4096] = { };
  81. char *command;
  82. char *argv[32] = { };
  83. unsigned int i;
  84. long err = 0;
  85. pid_t pid;
  86. if (strlen(_command) >= sizeof(command_buf) - 1) {
  87. logerr("subprocess_exec: Command too long");
  88. err = -E2BIG;
  89. goto out;
  90. }
  91. strcpy(command_buf, _command);
  92. command = command_buf;
  93. for (i = 0; i < ARRAY_SIZE(argv) - 1; i++) {
  94. argv[i] = command;
  95. command = string_split(command, isspace);
  96. if (strempty(argv[i])) {
  97. /* Skip empty elements */
  98. argv[i] = NULL;
  99. i--;
  100. }
  101. if (!command)
  102. break;
  103. }
  104. if (command) {
  105. logerr("subprocess_exec: Too many arguments: '%s'\n", _command);
  106. err = -EINVAL;
  107. goto out;
  108. }
  109. if (!argv[0]) {
  110. logerr("subprocess_exec: Too few arguments\n");
  111. err = -EINVAL;
  112. goto out;
  113. }
  114. pid = vfork();
  115. if (pid < 0) {
  116. logerr("subprocess_exec: Failed to fork (%s)\n", strerror(errno));
  117. err = -errno;
  118. goto out;
  119. }
  120. if (pid == 0) { /* Child */
  121. execv(argv[0], argv);
  122. logerr("subprocess_exec: Failed to exec '%s': %s\n",
  123. _command, strerror(errno));
  124. exit(0);
  125. while (1);
  126. }
  127. err = pid;
  128. out:
  129. return err;
  130. }