summaryrefslogtreecommitdiffstats
path: root/osdep/subprocess.c
blob: 9da5c10c1cc764c7fa61b3332699fca6f6cc768e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <pthread.h>

#include "config.h"

#include "common/common.h"
#include "common/msg.h"
#include "common/msg_control.h"

#include "subprocess.h"

void mp_devnull(void *ctx, char *data, size_t size)
{
}

#if HAVE_POSIX_SPAWN

int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
                  subprocess_read_cb on_stdout, subprocess_read_cb on_stderr,
                  char **error)
{
    struct mp_subprocess_opts opts = {
        .exe = args[0],
        .args = args,
        .cancel = cancel,
    };
    opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){
        .fd = 0, // stdin
        .src_fd = 0,
    };
    opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){
        .fd = 1, // stdout
        .on_read = on_stdout,
        .on_read_ctx = ctx,
        .src_fd = on_stdout ? -1 : 1,
    };
    opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){
        .fd = 2, // stderr
        .on_read = on_stderr,
        .on_read_ctx = ctx,
        .src_fd = on_stderr ? -1 : 2,
    };
    struct mp_subprocess_result res;
    mp_subprocess2(&opts, &res);
    if (res.error < 0) {
        *error = (char *)mp_subprocess_err_str(res.error);
        return res.error;
    }
    return res.exit_status;
}

#endif

struct subprocess_args {
    struct mp_log *log;
    char **args;
};

static void *run_subprocess(void *ptr)
{
    struct subprocess_args *p = ptr;
    pthread_detach(pthread_self());

    mp_msg_flush_status_line(p->log);

    char *err = NULL;
    if (mp_subprocess(p->args, NULL, NULL, NULL, NULL, &err) < 0)
        mp_err(p->log, "Running subprocess failed: %s\n", err);

    talloc_free(p);
    return NULL;
}

void mp_subprocess_detached(struct mp_log *log, char **args)
{
    struct subprocess_args *p = talloc_zero(NULL, struct subprocess_args);
    p->log = mp_log_new(p, log, NULL);
    int num_args = 0;
    for (int n = 0; args[n]; n++)
        MP_TARRAY_APPEND(p, p->args, num_args, talloc_strdup(p, args[n]));
    MP_TARRAY_APPEND(p, p->args, num_args, NULL);
    pthread_t thread;
    if (pthread_create(&thread, NULL, run_subprocess, p))
        talloc_free(p);
}

const char *mp_subprocess_err_str(int num)
{
    // Note: these are visible to the public client API
    switch (num) {
    case MP_SUBPROCESS_OK:              return "success";
    case MP_SUBPROCESS_EKILLED_BY_US:   return "killed";
    case MP_SUBPROCESS_EINIT:           return "init";
    case MP_SUBPROCESS_EUNSUPPORTED:    return "unsupported";
    case MP_SUBPROCESS_EGENERIC:        // fall through
    default:                            return "unknown";
    }
}