handle the server just killing us sometimes
[cycon.git] / launch.c
blob00477e25a978b3ea615025ca0b2fab987c2f5c82
1 /*
2 * Copyright (c) 2018, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <spawn.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
29 #include "cycon.h"
30 #include "macros.h"
32 extern char **environ;
34 void launch_only_once(const char *uri)
36 pid_t dummy = 0;
37 posix_spawn_file_actions_t fa;
38 int kill_fa = 0;
39 posix_spawnattr_t sa;
40 int kill_sa = 0;
41 char *uridup = strdup(uri);
42 char *prog = strdup("mpv");
43 char *quiet = strdup("--quiet");
44 char * const argv[4] = { prog, quiet, uridup, 0 };
46 if (!uridup ||
47 !prog ||
48 !quiet) {
49 goto done;
52 if (posix_spawn_file_actions_init(&fa)) {
53 PERROR_MESSAGE("posix_spawn_file_actions_init");
54 goto done;
57 kill_fa = 1;
59 if (posix_spawnattr_init(&sa)) {
60 PERROR_MESSAGE("posix_spawnattr_init");
61 goto done;
64 kill_sa = 1;
66 if (posix_spawn_file_actions_addclose(&fa, 0)) {
67 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
68 goto done;
71 if (posix_spawn_file_actions_addclose(&fa, 1)) {
72 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
73 goto done;
76 if (posix_spawn_file_actions_addclose(&fa, 2)) {
77 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
78 goto done;
81 if (posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF)) {
82 PERROR_MESSAGE("posix_spawnattr_setflags");
83 goto done;
86 if (posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY,
87 0)) {
88 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
89 goto done;
92 if (posix_spawn_file_actions_addopen(&fa, 1, "/dev/null", O_WRONLY,
93 0)) {
94 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
95 goto done;
98 if (posix_spawn_file_actions_adddup2(&fa, 1, 2)) {
99 PERROR_MESSAGE("posix_spawn_file_actions_adddup2");
100 goto done;
103 posix_spawnp(&dummy, "mpv-with-load-screen", &fa, &sa, argv, environ);
104 done:
106 if (kill_fa) {
107 posix_spawn_file_actions_destroy(&fa);
110 if (kill_sa) {
111 posix_spawnattr_destroy(&sa);
114 free(uridup);