Skip to content

Commit e8e6a8a

Browse files
committed
unix,win: add uv_os_getppid()
Refs: nodejs/node#14957 PR-URL: libuv#1610 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 719dfec commit e8e6a8a

File tree

9 files changed

+27
-3
lines changed

9 files changed

+27
-3
lines changed

docs/src/misc.rst

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ Data types
5959
Abstract representation of a file descriptor. On Unix systems this is a
6060
`typedef` of `int` and on Windows a `HANDLE`.
6161

62+
.. c:type:: uv_pid_t
63+
64+
Cross platform representation of a `pid_t`.
65+
66+
.. versionadded:: 1.16.0
67+
6268
.. c:type:: uv_rusage_t
6369
6470
Data type for resource usage results.
@@ -221,6 +227,12 @@ API
221227
On Windows not all fields are set, the unsupported fields are filled with zeroes.
222228
See :c:type:`uv_rusage_t` for more details.
223229
230+
.. c:function:: uv_pid_t uv_os_getppid(void)
231+
232+
Returns the parent process ID.
233+
234+
.. versionadded:: 1.16.0
235+
224236
.. c:function:: int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count)
225237
226238
Gets information about the CPUs on the system. The `cpu_infos` array will

include/uv-unix.h

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ typedef struct uv_buf_t {
123123
typedef int uv_file;
124124
typedef int uv_os_sock_t;
125125
typedef int uv_os_fd_t;
126+
typedef pid_t uv_pid_t;
126127

127128
#define UV_ONCE_INIT PTHREAD_ONCE_INIT
128129

include/uv-win.h

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ typedef struct uv_buf_t {
222222
typedef int uv_file;
223223
typedef SOCKET uv_os_sock_t;
224224
typedef HANDLE uv_os_fd_t;
225+
typedef int uv_pid_t;
225226

226227
typedef HANDLE uv_thread_t;
227228

include/uv.h

+1
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ UV_EXTERN int uv_os_homedir(char* buffer, size_t* size);
10691069
UV_EXTERN int uv_os_tmpdir(char* buffer, size_t* size);
10701070
UV_EXTERN int uv_os_get_passwd(uv_passwd_t* pwd);
10711071
UV_EXTERN void uv_os_free_passwd(uv_passwd_t* pwd);
1072+
UV_EXTERN uv_pid_t uv_os_getppid(void);
10721073

10731074
UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
10741075
UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);

src/unix/core.c

+5
Original file line numberDiff line numberDiff line change
@@ -1343,3 +1343,8 @@ int uv_os_gethostname(char* buffer, size_t* size) {
13431343
uv_os_fd_t uv_get_osfhandle(int fd) {
13441344
return fd;
13451345
}
1346+
1347+
1348+
uv_pid_t uv_os_getppid(void) {
1349+
return getppid();
1350+
}

src/win/internal.h

-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle);
326326
void uv__util_init(void);
327327

328328
uint64_t uv__hrtime(double scale);
329-
int uv_parent_pid(void);
330329
int uv_current_pid(void);
331330
__declspec(noreturn) void uv_fatal_error(const int errorno, const char* syscall);
332331
int uv__getpwuid_r(uv_passwd_t* pwd);

src/win/pipe.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
19691969

19701970
if (pipe->ipc) {
19711971
assert(!(pipe->flags & UV_HANDLE_NON_OVERLAPPED_PIPE));
1972-
pipe->pipe.conn.ipc_pid = uv_parent_pid();
1972+
pipe->pipe.conn.ipc_pid = uv_os_getppid();
19731973
assert(pipe->pipe.conn.ipc_pid != -1);
19741974
}
19751975
return 0;

src/win/util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ uint64_t uv_get_total_memory(void) {
331331
}
332332

333333

334-
int uv_parent_pid(void) {
334+
uv_pid_t uv_os_getppid(void) {
335335
int parent_pid = -1;
336336
HANDLE handle;
337337
PROCESSENTRY32 pe;

test/test-platform-output.c

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ TEST_IMPL(platform_output) {
2929
size_t rss;
3030
size_t size;
3131
double uptime;
32+
uv_pid_t ppid;
3233
uv_rusage_t rusage;
3334
uv_cpu_info_t* cpus;
3435
uv_interface_address_t* interfaces;
@@ -144,5 +145,9 @@ TEST_IMPL(platform_output) {
144145
printf(" shell: %s\n", pwd.shell);
145146
printf(" home directory: %s\n", pwd.homedir);
146147

148+
ppid = uv_os_getppid();
149+
ASSERT(ppid > 0);
150+
printf("uv_os_getppid: %d\n", (int) ppid);
151+
147152
return 0;
148153
}

0 commit comments

Comments
 (0)