/
libpid.h
143 lines (113 loc) · 3.1 KB
/
libpid.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef LIBPID_PID_H_
#define LIBPID_PID_H_
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <TlHelp32.h>
#ifdef _MSC_VER
#ifndef _PID_T_
#define _PID_T_
#ifndef _WIN64
typedef int pid_t;
#else
typedef __int64 pid_t;
#endif
#endif
#endif
#endif
#include <sys/types.h>
#ifndef PID_SNAPSHOT_NAMELEN
#define PID_SNAPSHOT_NAMELEN 1024
#endif
#if defined __APPLE__ && defined __MACH__
#include <sys/param.h>
#if defined BSD
#define LIPPID_ON_UNIX 1
#define LIBPID_ON_DARWIN 1
#endif
#elif defined(__unix__)
#define LIBPID_ON_UNIX 1
#endif
/**
* \returns process id
*
* Retrieves process id of caller
*/
pid_t pid_self(void);
#if defined LIBPID_ON_UNIX && !defined LIBPID_NO_MACRO
#define pid_self getpid
pid_t getpid(void);
#endif
#if 0
/**
* \param pid process id
* \param level [in] pointer to 0 = parent, 1 = grandparent and so on
* \param level [out] Maximum level traversed
* \returns oldest parent or parent of specified level, whichever's reached first
* If pointer to level doesn't change
*
* Retrieves the specified process' parent of level X
*/
pid_t pid_parent(pid_t pid, int *level);
#define pid_parent pid_parent
#endif
typedef struct pid_snapshot pid_snapshot;
enum { PID_SNAPSHOT_PID = 0, PID_SNAPSHOT_NAME = 1, PID_SNAPSHOT_CUSTOM_NAMELEN = 2 };
pid_snapshot *pid_snapshot_new(int flags);
void pid_snapshot_del(pid_snapshot *);
struct pid_snapshot_entry {
size_t size;
pid_t pid;
char *name;
#ifdef _WIN32
PROCESSENTRY32 sysentry;
#elif defined __APPLE__ && defined __MACH__
struct {
char name[PID_SNAPSHOT_NAMELEN];
} sysentry;
#endif
};
int pid_snapshot_first(struct pid_snapshot *snapshot, struct pid_snapshot_entry *entry);
int pid_snapshot_next(struct pid_snapshot *snapshot, struct pid_snapshot_entry *entry);
#if (defined(LIBPID_ON_DARWIN) || defined(_WIN32)) && !defined LIBPID_NO_MACRO
#define pid_snapshot pid_snapshot
#define pid_snapshot_entry pid_snapshot_entry
#define pid_snapshot_new pid_snapshot_new
#define pid_snapshot_del pid_snapshot_del
#define pid_snapshot_first pid_snapshot_first
#define pid_snapshot_next pid_snapshot_next
#endif
/* TODO: provide more complete functions */
#if (LIBPID_ON_DARWIN || _WIN32) && !defined LIBPID_NO_MACRO
pid_t pid_byname(const char *name);
#define pid_byname pid_byname
#endif
#if LIBPID_ON_DARWIN && !defined LIBPID_NO_MACRO
pid_t pid_byfileref(const char *name);
#define pid_byfileref pid_byfileref
#endif
#if _WIN32 && !defined LIBPID_NO_MACRO
pid_t pid_bywindow(const char *title, const char *);
#define pid_bywindow pid_bywindow
#endif
#ifndef LIBPID_NO_FMT
#if defined _WIN64
#include <inttypes.h>
#define FMTpid PRId64
#else
#define FMTpid "d"
#endif
#define PRIpid FMTpid
#define SCNpid FMTpid
#endif
#if 0
pid_snapshot *pid_search(void);
int pid_kill(pid_t, int);
int pid_wait(pid_t, ...);
int pid_file(pid_t);
struct pid_query_t {
const char *cmdline;
};
int pid_query(pid_t, struct pid_query_t*);
#endif
#endif