1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2013-2020 Couchbase, Inc.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#ifndef LCB_PROCUTIL_H
19#define LCB_PROCUTIL_H
20
21#ifdef _WIN32
22#include <windows.h>
23#include <process.h>
24#else
25#include <sys/types.h>
26#endif
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32typedef struct {
33    /** Full command line to launch */
34    const char *name;
35
36    /** Name of file to which output should be redirected. Optional */
37    const char *redirect;
38
39    /** Whether this should be a foreground process (uses 'system') */
40    int interactive;
41
42    /** Exit status */
43    int status;
44
45    /** Whether the application exited*/
46    int exited;
47
48    /** PLATFORM-SPECIFIC */
49#ifdef _WIN32
50    STARTUPINFO si;
51    PROCESS_INFORMATION pi;
52#else
53    pid_t pid;
54#endif
55} child_process_t;
56
57/**
58 * Create a new process.
59 * Returns 0 on success and nonzero on failure.
60 */
61int create_process(child_process_t *process);
62
63/**
64 * Try to kill the process. If 'force' is specified, the process is killed
65 * using more "forceful" action
66 */
67void kill_process(child_process_t *process, int force);
68
69/**
70 * Wait until a process has terminated.
71 * If tmosec is negative, it polls without blocking,
72 * if it is 0, it waits forever. If it is
73 * positive, it will wait for that many seconds, polling intermittently.
74 *
75 * Returns 0 if the process exited. nonzero on timeout
76 */
77int wait_process(child_process_t *process, int tmosec);
78
79/**
80 * Cleans up any resources opened while creating the process.
81 */
82void cleanup_process(child_process_t *process);
83
84#ifdef __cplusplus
85}
86#endif
87
88#endif
89