summaryrefslogtreecommitdiffstats
path: root/os_win32/popen.h
blob: 021158758a3eaf1d51b9d2825d3df4c656d918d2 (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
/*
 * os_win32/popen.h
 *
 * Home page of code is: https://www.smartmontools.org
 *
 * Copyright (C) 2018-21 Christian Franke
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef POPEN_H
#define POPEN_H

#define POPEN_H_CVSID "$Id: popen.h 5272 2021-12-18 15:55:35Z chrfranke $"

#include <stdio.h>

// MinGW <stdio.h> defines these to _popen/_pclose
#undef popen
#undef pclose

#ifdef __cplusplus
extern "C" {
#endif

// popen(3) reimplementation for Windows
//
// The _popen() from MSVCRT is not useful as it always opens a new
// console window if parent process has none.
//
// Differences to popen(3):
// - Only modes "r[bt]" are supported
// - stdin and stderr from parent are not inherited to child process
//   but redirected to null device
// - Only one child process can be run at a time

FILE * popen(const char * command, const char * mode);

int pclose(FILE * f);

#ifdef __cplusplus
}
#endif

// Enhanced version of popen() with ability to modify the access token.
// If 'restricted' is set, the child process is run with a restricted access
// token.  The local Administrator group and most privileges (all except
// SeChangeNotifyPrivilege) are removed.
FILE * popen_as_restr_user(const char * cmd, const char * mode, bool restricted);

// Check whether the access token of the current user could be effectively
// restricted.
// Returns false if the current user is the local SYSTEM or Administrator account.
bool popen_as_restr_check();

// wait(3) macros from <sys/wait.h>
#ifndef WIFEXITED
#define WIFEXITED(status)   (((status) & 0xff) == 0x00)
#define WIFSIGNALED(status) (((status) & 0xff) != 0x00)
#define WIFSTOPPED(status)  (0)
#define WEXITSTATUS(status) ((status) >> 8)
#define WTERMSIG(status)    ((status) & 0xff)
#define WSTOPSIG(status)    (0)
#endif // WIFEXITED

#endif // POPEN_H