make pkg-config overridable
[cycon.git] / launch.c
blob5747980f8eb0ab1b4daf800704d9c4b4b12ce8e7
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
35 launch_only_once(const char *uri)
37 pid_t dummy = 0;
38 posix_spawn_file_actions_t fa;
39 int kill_fa = 0;
40 posix_spawnattr_t sa;
41 int kill_sa = 0;
42 char *uridup = strdup(uri);
43 char *prog = strdup("mpv");
44 char *quiet = strdup("--quiet");
45 char * const argv[4] = { prog, quiet, uridup, 0 };
47 if (!uridup ||
48 !prog ||
49 !quiet) {
50 goto done;
53 if (posix_spawn_file_actions_init(&fa)) {
54 PERROR_MESSAGE("posix_spawn_file_actions_init");
55 goto done;
58 kill_fa = 1;
60 if (posix_spawnattr_init(&sa)) {
61 PERROR_MESSAGE("posix_spawnattr_init");
62 goto done;
65 kill_sa = 1;
67 if (posix_spawn_file_actions_addclose(&fa, 0)) {
68 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
69 goto done;
72 if (posix_spawn_file_actions_addclose(&fa, 1)) {
73 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
74 goto done;
77 if (posix_spawn_file_actions_addclose(&fa, 2)) {
78 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
79 goto done;
82 if (posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF)) {
83 PERROR_MESSAGE("posix_spawnattr_setflags");
84 goto done;
87 if (posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY,
88 0)) {
89 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
90 goto done;
93 if (posix_spawn_file_actions_addopen(&fa, 1, "/dev/null", O_WRONLY,
94 0)) {
95 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
96 goto done;
99 if (posix_spawn_file_actions_adddup2(&fa, 1, 2)) {
100 PERROR_MESSAGE("posix_spawn_file_actions_adddup2");
101 goto done;
104 posix_spawnp(&dummy, "mpv-with-load-screen", &fa, &sa, argv, environ);
105 done:
107 if (kill_fa) {
108 posix_spawn_file_actions_destroy(&fa);
111 if (kill_sa) {
112 posix_spawnattr_destroy(&sa);
115 free(uridup);
116 free(prog);
117 free(quiet);