| 27 | static pid_t daemonGID = -1; |
| 28 | |
| 29 | static Int32 daemonLoop() |
| 30 | { |
| 31 | close(pipeA[1]); |
| 32 | close(pipeB[0]); |
| 33 | |
| 34 | fcntl(pipeA[0], F_SETFD, fcntl(pipeA[0], F_GETFD) | FD_CLOEXEC); |
| 35 | fcntl(pipeB[1], F_SETFD, fcntl(pipeB[1], F_GETFD) | FD_CLOEXEC); |
| 36 | |
| 37 | if (setpgid(0, 0) == 0) |
| 38 | daemonGID = getpgrp(); |
| 39 | |
| 40 | pollfd fds; |
| 41 | fds.events = POLLIN; |
| 42 | fds.revents = 0; |
| 43 | fds.fd = pipeA[0]; |
| 44 | |
| 45 | // Read messages from the parent |
| 46 | std::string cmd = ""; |
| 47 | UChar8 c; |
| 48 | |
| 49 | while (true) |
| 50 | { |
| 51 | if (poll(&fds, 1, -1) != 1) |
| 52 | return 1; |
| 53 | |
| 54 | Int32 n = read(pipeA[0], &c, sizeof(c)); |
| 55 | |
| 56 | // Closed pipe |
| 57 | if (n == 0) |
| 58 | return 0; |
| 59 | |
| 60 | while (n > 0) |
| 61 | { |
| 62 | // Reached end of command |
| 63 | if (c == '\0') |
| 64 | { |
| 65 | pid_t pid = fork(); |
| 66 | |
| 67 | if (pid == 0) |
| 68 | exit(system(cmd.c_str())); |
| 69 | else if (pid > 0) |
| 70 | { |
| 71 | // Send the launched app PID to the compositor |
| 72 | Char8 buffer[32]; |
| 73 | sprintf(buffer, "%d", pid); |
| 74 | Char8 *ptr = buffer; |
| 75 | |
| 76 | while (true) |
| 77 | { |
| 78 | ssize_t w = write(pipeB[1], ptr, sizeof(c)); |
| 79 | |
| 80 | if (*ptr == '\0') |
| 81 | break; |
| 82 | |
| 83 | if (w > 0) |
| 84 | ptr += w; |
| 85 | } |
| 86 | } |