PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/external/alsa-utils/alsactl/init_utils_run.c

https://gitlab.com/brian0218/rk3288_r-box_android4.4.2_sdk
C | 247 lines | 194 code | 22 blank | 31 comment | 70 complexity | dc958057f139e1a35c0643802165b0db MD5 | raw file
  1. /*
  2. * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation version 2 of the License.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. *
  17. */
  18. #define MY_MAX(a,b) ((a) > (b) ? (a) : (b))
  19. #define READ_END 0
  20. #define WRITE_END 1
  21. static
  22. int run_program1(struct space *space,
  23. const char *command0, char *result,
  24. size_t ressize, size_t *reslen, int log);
  25. static
  26. int run_program0(struct space *space,
  27. const char *command0, char *result,
  28. size_t ressize, size_t *reslen, int log)
  29. {
  30. int retval = 0;
  31. int status;
  32. int outpipe[2] = {-1, -1};
  33. int errpipe[2] = {-1, -1};
  34. pid_t pid;
  35. char arg[PATH_SIZE];
  36. char program[PATH_SIZE];
  37. char *argv[(sizeof(arg) / 2) + 1];
  38. int devnull;
  39. int i;
  40. /* build argv from comand */
  41. strlcpy(arg, command0, sizeof(arg));
  42. i = 0;
  43. if (strchr(arg, ' ') != NULL) {
  44. char *pos = arg;
  45. while (pos != NULL) {
  46. if (pos[0] == '\'') {
  47. /* don't separate if in apostrophes */
  48. pos++;
  49. argv[i] = strsep(&pos, "\'");
  50. while (pos != NULL && pos[0] == ' ')
  51. pos++;
  52. } else {
  53. argv[i] = strsep(&pos, " ");
  54. }
  55. dbg("arg[%i] '%s'", i, argv[i]);
  56. i++;
  57. }
  58. argv[i] = NULL;
  59. } else {
  60. argv[0] = arg;
  61. argv[1] = NULL;
  62. }
  63. info("'%s'", command0);
  64. /* prepare pipes from child to parent */
  65. if (result || log) {
  66. if (pipe(outpipe) != 0) {
  67. Perror(space, "pipe failed: %s", strerror(errno));
  68. return -1;
  69. }
  70. }
  71. if (log) {
  72. if (pipe(errpipe) != 0) {
  73. Perror(space, "pipe failed: %s", strerror(errno));
  74. return -1;
  75. }
  76. }
  77. /* allow programs in /lib/alsa called without the path */
  78. if (strchr(argv[0], '/') == NULL) {
  79. strlcpy(program, "/lib/alsa/", sizeof(program));
  80. strlcat(program, argv[0], sizeof(program));
  81. argv[0] = program;
  82. }
  83. pid = fork();
  84. switch(pid) {
  85. case 0:
  86. /* child closes parent ends of pipes */
  87. if (outpipe[READ_END] > 0)
  88. close(outpipe[READ_END]);
  89. if (errpipe[READ_END] > 0)
  90. close(errpipe[READ_END]);
  91. /* discard child output or connect to pipe */
  92. devnull = open("/dev/null", O_RDWR);
  93. if (devnull > 0) {
  94. dup2(devnull, STDIN_FILENO);
  95. if (outpipe[WRITE_END] < 0)
  96. dup2(devnull, STDOUT_FILENO);
  97. if (errpipe[WRITE_END] < 0)
  98. dup2(devnull, STDERR_FILENO);
  99. close(devnull);
  100. } else
  101. Perror(space, "open /dev/null failed: %s", strerror(errno));
  102. if (outpipe[WRITE_END] > 0) {
  103. dup2(outpipe[WRITE_END], STDOUT_FILENO);
  104. close(outpipe[WRITE_END]);
  105. }
  106. if (errpipe[WRITE_END] > 0) {
  107. dup2(errpipe[WRITE_END], STDERR_FILENO);
  108. close(errpipe[WRITE_END]);
  109. }
  110. execv(argv[0], argv);
  111. /* we should never reach this */
  112. Perror(space, "exec of program '%s' failed", argv[0]);
  113. _exit(1);
  114. case -1:
  115. Perror(space, "fork of '%s' failed: %s", argv[0], strerror(errno));
  116. return -1;
  117. default:
  118. /* read from child if requested */
  119. if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
  120. ssize_t count;
  121. size_t respos = 0;
  122. /* parent closes child ends of pipes */
  123. if (outpipe[WRITE_END] > 0)
  124. close(outpipe[WRITE_END]);
  125. if (errpipe[WRITE_END] > 0)
  126. close(errpipe[WRITE_END]);
  127. /* read child output */
  128. while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
  129. int fdcount;
  130. fd_set readfds;
  131. FD_ZERO(&readfds);
  132. if (outpipe[READ_END] > 0)
  133. FD_SET(outpipe[READ_END], &readfds);
  134. if (errpipe[READ_END] > 0)
  135. FD_SET(errpipe[READ_END], &readfds);
  136. fdcount = select(MY_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
  137. if (fdcount < 0) {
  138. if (errno == EINTR)
  139. continue;
  140. retval = -1;
  141. break;
  142. }
  143. /* get stdout */
  144. if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
  145. char inbuf[1024];
  146. char *pos;
  147. char *line;
  148. count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
  149. if (count <= 0) {
  150. close(outpipe[READ_END]);
  151. outpipe[READ_END] = -1;
  152. if (count < 0) {
  153. Perror(space, "stdin read failed: %s", strerror(errno));
  154. retval = -1;
  155. }
  156. continue;
  157. }
  158. inbuf[count] = '\0';
  159. /* store result for rule processing */
  160. if (result) {
  161. if (respos + count < ressize) {
  162. memcpy(&result[respos], inbuf, count);
  163. respos += count;
  164. } else {
  165. Perror(space, "ressize %ld too short", (long)ressize);
  166. retval = -1;
  167. }
  168. }
  169. pos = inbuf;
  170. while ((line = strsep(&pos, "\n")))
  171. if (pos || line[0] != '\0')
  172. info("'%s' (stdout) '%s'", argv[0], line);
  173. }
  174. /* get stderr */
  175. if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
  176. char errbuf[1024];
  177. char *pos;
  178. char *line;
  179. count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
  180. if (count <= 0) {
  181. close(errpipe[READ_END]);
  182. errpipe[READ_END] = -1;
  183. if (count < 0)
  184. Perror(space, "stderr read failed: %s", strerror(errno));
  185. continue;
  186. }
  187. errbuf[count] = '\0';
  188. pos = errbuf;
  189. while ((line = strsep(&pos, "\n")))
  190. if (pos || line[0] != '\0')
  191. info("'%s' (stderr) '%s'", argv[0], line);
  192. }
  193. }
  194. if (outpipe[READ_END] > 0)
  195. close(outpipe[READ_END]);
  196. if (errpipe[READ_END] > 0)
  197. close(errpipe[READ_END]);
  198. /* return the childs stdout string */
  199. if (result) {
  200. result[respos] = '\0';
  201. dbg("result='%s'", result);
  202. if (reslen)
  203. *reslen = respos;
  204. }
  205. }
  206. waitpid(pid, &status, 0);
  207. if (WIFEXITED(status)) {
  208. info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
  209. if (WEXITSTATUS(status) != 0)
  210. retval = -1;
  211. } else {
  212. Perror(space, "'%s' abnormal exit", argv[0]);
  213. retval = -1;
  214. }
  215. }
  216. return retval;
  217. }
  218. static
  219. int run_program(struct space *space, const char *command0, char *result,
  220. size_t ressize, size_t *reslen, int log)
  221. {
  222. if (command0[0] == '_' && command0[1] == '_')
  223. return run_program1(space, command0, result, ressize, reslen, log);
  224. return run_program0(space, command0, result, ressize, reslen, log);
  225. }