jdk/src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c
author rriggs
Thu, 16 Mar 2017 15:40:38 -0400
changeset 44269 762b2df849b3
parent 40206 8870e08b50bc
child 45926 43879a69df50
permissions -rw-r--r--
8176272: (process) ProcessHandle::onExit fails to wait for non-child process Reviewed-by: chegar, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     1
/*
44269
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     4
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    10
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    15
 * accompanied this code).
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    16
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    20
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    23
 * questions.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    24
 */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    25
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    26
#include "jni.h"
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    27
#include "jni_util.h"
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    28
#include "java_lang_ProcessHandleImpl.h"
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    29
#include "java_lang_ProcessHandleImpl_Info.h"
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    30
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    31
#include "ProcessHandleImpl_unix.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    32
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    33
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    34
#include <stdio.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    35
#include <errno.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    36
#include <fcntl.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    37
#include <signal.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    38
#include <stdlib.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    39
#include <unistd.h>
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    40
#include <string.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    41
#include <dirent.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    42
#include <ctype.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    43
#include <limits.h>
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    44
#include <sys/types.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    45
#include <sys/stat.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    46
#include <sys/wait.h>
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    47
40182
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    48
/* For POSIX-compliant getpwuid_r on Solaris */
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    49
#if defined(__solaris__)
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    50
#define _POSIX_PTHREAD_SEMANTICS
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    51
#endif
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    52
#include <pwd.h>
1e16a8fd76ba 8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents: 32209
diff changeset
    53
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    54
#ifdef _AIX
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    55
#include <sys/procfs.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    56
#endif
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    57
#ifdef __solaris__
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    58
#include <procfs.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    59
#endif
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    60
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    61
/**
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    62
 * This file contains the implementation of the native ProcessHandleImpl
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    63
 * functions which are common to all Unix variants.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    64
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    65
 * The currently supported Unix variants are Solaris, Linux, MaxOS X and AIX.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    66
 * The various similarities and differences between these systems make it hard
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    67
 * to find a clear boundary between platform specific and shared code.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    68
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    69
 * In order to ease code sharing between the platforms while still keeping the
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    70
 * code as clean as possible (i.e. free of preprocessor macros) we use the
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    71
 * following source code layout (remember that ProcessHandleImpl_unix.c will
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    72
 * be compiled on EVERY Unix platform while ProcessHandleImpl_<os>.c will be
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    73
 * only compiled on the specific OS):
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    74
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    75
 * - all the JNI wrappers for the ProcessHandleImpl functions go into this file
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    76
 * - if their implementation is common on ALL the supported Unix platforms it
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    77
 *   goes right into the JNI wrappers
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    78
 * - if the whole function or substantial parts of it are platform dependent,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    79
 *   the implementation goes into os_<function_name> functions in
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    80
 *   ProcessHandleImpl_<os>.c
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    81
 * - if at least two platforms implement an os_<function_name> function in the
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    82
 *   same way, this implementation is factored out into unix_<function_name>,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    83
 *   placed into this file and called from the corresponding os_<function_name>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    84
 *   function.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    85
 * - For convenience, all the os_ and unix_ functions are declared in
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    86
 *   ProcessHandleImpl_unix.h which is included into every
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    87
 *   ProcessHandleImpl_<os>.c file.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    88
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    89
 * Example 1:
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    90
 * ----------
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    91
 * The implementation of Java_java_lang_ProcessHandleImpl_initNative()
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    92
 * is the same on all platforms except on Linux where it initilizes one
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    93
 * additional field. So we place the implementation right into
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    94
 * Java_java_lang_ProcessHandleImpl_initNative() but add call to
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    95
 * os_init() at the end of the function which is empty on all platforms
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    96
 * except Linux where it performs the additionally initializations.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    97
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    98
 * Example 2:
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
    99
 * ----------
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   100
 * The implementation of Java_java_lang_ProcessHandleImpl_00024Info_info0 is the
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   101
 * same on Solaris and AIX but different on Linux and MacOSX. We therefore simply
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   102
 * call the helpers os_getParentPidAndTimings() and os_getCmdlineAndUserInfo().
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   103
 * The Linux and MaxOS X versions of these functions (in the corresponding files
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   104
 * ProcessHandleImpl_linux.c and ProcessHandleImpl_macosx.c) directly contain
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   105
 * the platform specific implementations while the Solaris and AIX
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   106
 * implementations simply call back to unix_getParentPidAndTimings() and
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   107
 * unix_getCmdlineAndUserInfo() which are implemented right in this file.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   108
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   109
 * The term "same implementation" is still a question of interpretation. It my
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   110
 * be acceptable to have a few ifdef'ed lines if that allows the sharing of a
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   111
 * huge function. On the other hand, if the platform specific code in a shared
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   112
 * function grows over a certain limit, it may be better to refactor that
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   113
 * functionality into corresponding, platform-specific os_ functions.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   114
 */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   115
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   116
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   117
#ifndef WIFEXITED
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   118
#define WIFEXITED(status) (((status)&0xFF) == 0)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   119
#endif
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   120
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   121
#ifndef WEXITSTATUS
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   122
#define WEXITSTATUS(status) (((status)>>8)&0xFF)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   123
#endif
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   124
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   125
#ifndef WIFSIGNALED
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   126
#define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   127
#endif
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   128
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   129
#ifndef WTERMSIG
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   130
#define WTERMSIG(status) ((status)&0x7F)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   131
#endif
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   132
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   133
#ifdef __solaris__
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   134
/* The child exited because of a signal.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   135
 * The best value to return is 0x80 + signal number,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   136
 * because that is what all Unix shells do, and because
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   137
 * it allows callers to distinguish between process exit and
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   138
 * process death by signal.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   139
 * Unfortunately, the historical behavior on Solaris is to return
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   140
 * the signal number, and we preserve this for compatibility. */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   141
#define WTERMSIG_RETURN(status) WTERMSIG(status)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   142
#else
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   143
#define WTERMSIG_RETURN(status) (WTERMSIG(status) + 0x80)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   144
#endif
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   145
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   146
#define RESTARTABLE(_cmd, _result) do { \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   147
  do { \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   148
    _result = _cmd; \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   149
  } while((_result == -1) && (errno == EINTR)); \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   150
} while(0)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   151
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   152
#define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   153
  do { \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   154
    _result = _cmd; \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   155
  } while((_result == NULL) && (errno == EINTR)); \
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   156
} while(0)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   157
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   158
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   159
/* Field id for jString 'command' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   160
jfieldID ProcessHandleImpl_Info_commandID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   161
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   162
/* Field id for jString 'commandLine' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   163
jfieldID ProcessHandleImpl_Info_commandLineID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   164
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   165
/* Field id for jString[] 'arguments' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   166
jfieldID ProcessHandleImpl_Info_argumentsID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   167
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   168
/* Field id for jlong 'totalTime' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   169
jfieldID ProcessHandleImpl_Info_totalTimeID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   170
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   171
/* Field id for jlong 'startTime' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   172
jfieldID ProcessHandleImpl_Info_startTimeID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   173
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   174
/* Field id for jString 'user' in java.lang.ProcessHandleImpl.Info */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   175
jfieldID ProcessHandleImpl_Info_userID;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   176
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   177
/* Size of password or group entry when not available via sysconf */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   178
#define ENT_BUF_SIZE   1024
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   179
/* The value for the size of the buffer used by getpwuid_r(). The result of */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   180
/* sysconf(_SC_GETPW_R_SIZE_MAX) if available or ENT_BUF_SIZE otherwise. */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   181
static long getpw_buf_size;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   182
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   183
/**************************************************************
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   184
 * Static method to initialize field IDs and the ticks per second rate.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   185
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   186
 * Class:     java_lang_ProcessHandleImpl_Info
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   187
 * Method:    initIDs
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   188
 * Signature: ()V
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   189
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   190
JNIEXPORT void JNICALL
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   191
Java_java_lang_ProcessHandleImpl_00024Info_initIDs(JNIEnv *env, jclass clazz) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   192
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   193
    CHECK_NULL(ProcessHandleImpl_Info_commandID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   194
            (*env)->GetFieldID(env, clazz, "command", "Ljava/lang/String;"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   195
    CHECK_NULL(ProcessHandleImpl_Info_commandLineID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   196
            (*env)->GetFieldID(env, clazz, "commandLine", "Ljava/lang/String;"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   197
    CHECK_NULL(ProcessHandleImpl_Info_argumentsID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   198
            (*env)->GetFieldID(env, clazz, "arguments", "[Ljava/lang/String;"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   199
    CHECK_NULL(ProcessHandleImpl_Info_totalTimeID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   200
            (*env)->GetFieldID(env, clazz, "totalTime", "J"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   201
    CHECK_NULL(ProcessHandleImpl_Info_startTimeID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   202
            (*env)->GetFieldID(env, clazz, "startTime", "J"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   203
    CHECK_NULL(ProcessHandleImpl_Info_userID =
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   204
            (*env)->GetFieldID(env, clazz, "user", "Ljava/lang/String;"));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   205
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   206
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   207
/***********************************************************
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   208
 * Static method to initialize platform dependent constants.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   209
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   210
 * Class:     java_lang_ProcessHandleImpl
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   211
 * Method:    initNative
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   212
 * Signature: ()V
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   213
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   214
JNIEXPORT void JNICALL
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   215
Java_java_lang_ProcessHandleImpl_initNative(JNIEnv *env, jclass clazz) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   216
    getpw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   217
    if (getpw_buf_size == -1) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   218
        getpw_buf_size = ENT_BUF_SIZE;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   219
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   220
    os_initNative(env, clazz);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   221
}
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   222
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   223
/* Block until a child process exits and return its exit code.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   224
 * Note, can only be called once for any given pid if reapStatus = true.
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   225
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   226
 * Class:     java_lang_ProcessHandleImpl
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   227
 * Method:    waitForProcessExit0
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   228
 * Signature: (JZ)I
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   229
 */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   230
JNIEXPORT jint JNICALL
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   231
Java_java_lang_ProcessHandleImpl_waitForProcessExit0(JNIEnv* env,
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   232
                                                     jclass junk,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   233
                                                     jlong jpid,
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   234
                                                     jboolean reapStatus) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   235
    pid_t pid = (pid_t)jpid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   236
    errno = 0;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   237
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   238
    if (reapStatus != JNI_FALSE) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   239
        /* Wait for the child process to exit.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   240
         * waitpid() is standard, so use it on all POSIX platforms.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   241
         * It is known to work when blocking to wait for the pid
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   242
         * This returns immediately if the child has already exited.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   243
         */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   244
        int status;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   245
        while (waitpid(pid, &status, 0) < 0) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   246
            switch (errno) {
44269
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   247
                case ECHILD:
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   248
                    return java_lang_ProcessHandleImpl_NOT_A_CHILD; // No child
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   249
                case EINTR: break;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   250
                default: return -1;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   251
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   252
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   253
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   254
        if (WIFEXITED(status)) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   255
            return WEXITSTATUS(status);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   256
        } else if (WIFSIGNALED(status)) {
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   257
            return WTERMSIG_RETURN(status);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   258
        } else {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   259
            return status;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   260
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   261
     } else {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   262
        /*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   263
         * Wait for the child process to exit without reaping the exitValue.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   264
         * waitid() is standard on all POSIX platforms.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   265
         * Note: waitid on Mac OS X 10.7 seems to be broken;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   266
         * it does not return the exit status consistently.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   267
         */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   268
        siginfo_t siginfo;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   269
        int options = WEXITED |  WNOWAIT;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   270
        memset(&siginfo, 0, sizeof siginfo);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   271
        while (waitid(P_PID, pid, &siginfo, options) < 0) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   272
            switch (errno) {
44269
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   273
                case ECHILD:
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   274
                    return java_lang_ProcessHandleImpl_NOT_A_CHILD; // No child
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   275
                case EINTR: break;
762b2df849b3 8176272: (process) ProcessHandle::onExit fails to wait for non-child process
rriggs
parents: 40206
diff changeset
   276
                default: return -1;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   277
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   278
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   279
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   280
        if (siginfo.si_code == CLD_EXITED) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   281
             /*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   282
              * The child exited normally; get its exit code.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   283
              */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   284
             return siginfo.si_status;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   285
        } else if (siginfo.si_code == CLD_KILLED || siginfo.si_code == CLD_DUMPED) {
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   286
             return WTERMSIG_RETURN(siginfo.si_status);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   287
        } else {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   288
             /*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   289
              * Unknown exit code; pass it through.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   290
              */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   291
             return siginfo.si_status;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   292
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   293
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   294
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   295
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   296
/*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   297
 * Class:     java_lang_ProcessHandleImpl
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   298
 * Method:    getCurrentPid0
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   299
 * Signature: ()J
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   300
 */
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   301
JNIEXPORT jlong JNICALL
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   302
Java_java_lang_ProcessHandleImpl_getCurrentPid0(JNIEnv *env, jclass clazz) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   303
    pid_t pid = getpid();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   304
    return (jlong) pid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   305
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   306
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   307
/*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   308
 * Class:     java_lang_ProcessHandleImpl
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   309
 * Method:    destroy0
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   310
 * Signature: (JJZ)Z
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   311
 */
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   312
JNIEXPORT jboolean JNICALL
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   313
Java_java_lang_ProcessHandleImpl_destroy0(JNIEnv *env,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   314
                                          jobject obj,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   315
                                          jlong jpid,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   316
                                          jlong startTime,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   317
                                          jboolean force) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   318
    pid_t pid = (pid_t) jpid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   319
    int sig = (force == JNI_TRUE) ? SIGKILL : SIGTERM;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   320
    jlong start = Java_java_lang_ProcessHandleImpl_isAlive0(env, obj, jpid);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   321
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   322
    if (start == startTime || start == 0 || startTime == 0) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   323
        return (kill(pid, sig) < 0) ? JNI_FALSE : JNI_TRUE;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   324
    } else {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   325
        return JNI_FALSE;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   326
    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   327
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   328
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   329
/*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   330
 * Returns the children of the requested pid and optionally each parent and
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   331
 * start time.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   332
 * Accumulates any process who parent pid matches.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   333
 * The resulting pids are stored into the array of longs.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   334
 * The number of pids is returned if they all fit.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   335
 * If the array is too short, the negative of the desired length is returned.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   336
 * Class:     java_lang_ProcessHandleImpl
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   337
 * Method:    getProcessPids0
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   338
 * Signature: (J[J[J[J)I
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   339
 */
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   340
JNIEXPORT jint JNICALL
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   341
Java_java_lang_ProcessHandleImpl_getProcessPids0(JNIEnv *env,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   342
                                                 jclass clazz,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   343
                                                 jlong jpid,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   344
                                                 jlongArray jarray,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   345
                                                 jlongArray jparentArray,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   346
                                                 jlongArray jstimesArray) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   347
    return os_getChildren(env, jpid, jarray, jparentArray, jstimesArray);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   348
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   349
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   350
/*
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   351
 * Fill in the Info object from the OS information about the process.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   352
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   353
 * Class:     java_lang_ProcessHandleImpl_Info
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   354
 * Method:    info0
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   355
 * Signature: (Ljava/lang/ProcessHandle/Info;J)I
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   356
 */
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   357
JNIEXPORT void JNICALL
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   358
Java_java_lang_ProcessHandleImpl_00024Info_info0(JNIEnv *env,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   359
                                                 jobject jinfo,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   360
                                                 jlong jpid) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   361
    pid_t pid = (pid_t) jpid;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   362
    pid_t ppid;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   363
    jlong totalTime = -1L;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   364
    jlong startTime = -1L;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   365
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   366
    ppid = os_getParentPidAndTimings(env, pid,  &totalTime, &startTime);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   367
    if (ppid >= 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   368
        (*env)->SetLongField(env, jinfo, ProcessHandleImpl_Info_totalTimeID, totalTime);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   369
        JNU_CHECK_EXCEPTION(env);
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   370
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   371
        (*env)->SetLongField(env, jinfo, ProcessHandleImpl_Info_startTimeID, startTime);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   372
        JNU_CHECK_EXCEPTION(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   373
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   374
    os_getCmdlineAndUserInfo(env, jinfo, pid);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   375
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   376
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   377
/*
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   378
 * Check if a process is alive.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   379
 * Return the start time (ms since 1970) if it is available.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   380
 * If the start time is not available return 0.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   381
 * If the pid is invalid, return -1.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   382
 *
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   383
 * Class:     java_lang_ProcessHandleImpl
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   384
 * Method:    isAlive0
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   385
 * Signature: (J)J
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   386
 */
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   387
JNIEXPORT jlong JNICALL
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   388
Java_java_lang_ProcessHandleImpl_isAlive0(JNIEnv *env, jobject obj, jlong jpid) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   389
    pid_t pid = (pid_t) jpid;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   390
    jlong startTime = 0L;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   391
    jlong totalTime = 0L;
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   392
    pid_t ppid = os_getParentPidAndTimings(env, pid, &totalTime, &startTime);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   393
    return (ppid < 0) ? -1 : startTime;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   394
}
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   395
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   396
/*
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   397
 * Returns the parent pid of the requested pid.
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   398
 * The start time of the process must match (or be ANY).
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   399
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   400
 * Class:     java_lang_ProcessHandleImpl
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   401
 * Method:    parent0
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   402
 * Signature: (JJ)J
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   403
 */
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   404
JNIEXPORT jlong JNICALL
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   405
Java_java_lang_ProcessHandleImpl_parent0(JNIEnv *env,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   406
                                        jobject obj,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   407
                                        jlong jpid,
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   408
                                        jlong startTime) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   409
    pid_t pid = (pid_t) jpid;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   410
    pid_t ppid;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   411
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   412
    if (pid == getpid()) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   413
        ppid = getppid();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   414
    } else {
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   415
        jlong start = 0L;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   416
        jlong total = 0L;        // unused
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   417
        ppid = os_getParentPidAndTimings(env, pid, &total, &start);
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   418
        if (start != startTime && start != 0 && startTime != 0) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   419
            ppid = -1;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   420
        }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   421
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   422
    return (jlong) ppid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   423
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   424
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   425
/**
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   426
 * Construct the argument array by parsing the arguments from the sequence
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   427
 * of arguments.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   428
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   429
void unix_fillArgArray(JNIEnv *env, jobject jinfo, int nargs, char *cp,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   430
                       char *argsEnd, jstring cmdexe, char *cmdline) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   431
    jobject argsArray;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   432
    int i;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   433
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   434
    (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_commandID, cmdexe);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   435
    JNU_CHECK_EXCEPTION(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   436
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   437
    if (nargs >= 1) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   438
        // Create a String array for nargs-1 elements
40179
b5d59a6f093a 8161431: JNI pending exceptions in ProcessHandleImpl_linux.c and ProcessHandleImpl_unix.c
rriggs
parents: 32209
diff changeset
   439
        jclass clazzString = JNU_ClassString(env);
b5d59a6f093a 8161431: JNI pending exceptions in ProcessHandleImpl_linux.c and ProcessHandleImpl_unix.c
rriggs
parents: 32209
diff changeset
   440
        CHECK_NULL(clazzString);
b5d59a6f093a 8161431: JNI pending exceptions in ProcessHandleImpl_linux.c and ProcessHandleImpl_unix.c
rriggs
parents: 32209
diff changeset
   441
        argsArray = (*env)->NewObjectArray(env, nargs - 1, clazzString, NULL);
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   442
        CHECK_NULL(argsArray);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   443
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   444
        for (i = 0; i < nargs - 1; i++) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   445
            jstring str = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   446
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   447
            cp += strlen(cp) + 1;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   448
            if (cp > argsEnd || *cp == '\0') {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   449
                return;  // Off the end pointer or an empty argument is an error
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   450
            }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   451
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   452
            CHECK_NULL((str = JNU_NewStringPlatform(env, cp)));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   453
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   454
            (*env)->SetObjectArrayElement(env, argsArray, i, str);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   455
            JNU_CHECK_EXCEPTION(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   456
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   457
        (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_argumentsID, argsArray);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   458
        JNU_CHECK_EXCEPTION(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   459
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   460
    if (cmdline != NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   461
        jstring commandLine = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   462
        CHECK_NULL((commandLine = JNU_NewStringPlatform(env, cmdline)));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   463
        (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_commandLineID, commandLine);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   464
        JNU_CHECK_EXCEPTION(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   465
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   466
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   467
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   468
void unix_getUserInfo(JNIEnv* env, jobject jinfo, uid_t uid) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   469
    int result = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   470
    char* pwbuf;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   471
    jstring name = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   472
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   473
    /* allocate buffer for password record */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   474
    pwbuf = (char*)malloc(getpw_buf_size);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   475
    if (pwbuf == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   476
        JNU_ThrowOutOfMemoryError(env, "Unable to open getpwent");
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   477
    } else {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   478
        struct passwd pwent;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   479
        struct passwd* p = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   480
        RESTARTABLE(getpwuid_r(uid, &pwent, pwbuf, (size_t)getpw_buf_size, &p), result);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   481
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   482
        // Create the Java String if a name was found
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   483
        if (result == 0 && p != NULL &&
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   484
            p->pw_name != NULL && *(p->pw_name) != '\0') {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   485
            name = JNU_NewStringPlatform(env, p->pw_name);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   486
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   487
        free(pwbuf);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   488
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   489
    if (name != NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   490
        (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_userID, name);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   491
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   492
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   493
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   494
/*
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   495
 * The following functions are common on Solaris, Linux and AIX.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   496
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   497
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   498
#if defined(__solaris__) || defined (__linux__) || defined(_AIX)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   499
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   500
/*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   501
 * Returns the children of the requested pid and optionally each parent and
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   502
 * start time.
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   503
 * Reads /proc and accumulates any process who parent pid matches.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   504
 * The resulting pids are stored into the array of longs.
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   505
 * The number of pids is returned if they all fit.
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   506
 * If the array is too short, the negative of the desired length is returned.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   507
 */
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   508
jint unix_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   509
                      jlongArray jparentArray, jlongArray jstimesArray) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   510
    DIR* dir;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   511
    struct dirent* ptr;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   512
    pid_t pid = (pid_t) jpid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   513
    jlong* pids = NULL;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   514
    jlong* ppids = NULL;
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   515
    jlong* stimes = NULL;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   516
    jsize parentArraySize = 0;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   517
    jsize arraySize = 0;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   518
    jsize stimesSize = 0;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   519
    jsize count = 0;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   520
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   521
    arraySize = (*env)->GetArrayLength(env, jarray);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   522
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   523
    if (jparentArray != NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   524
        parentArraySize = (*env)->GetArrayLength(env, jparentArray);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   525
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   526
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   527
        if (arraySize != parentArraySize) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   528
            JNU_ThrowIllegalArgumentException(env, "array sizes not equal");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   529
            return 0;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   530
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   531
    }
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   532
    if (jstimesArray != NULL) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   533
        stimesSize = (*env)->GetArrayLength(env, jstimesArray);
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   534
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   535
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   536
        if (arraySize != stimesSize) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   537
            JNU_ThrowIllegalArgumentException(env, "array sizes not equal");
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   538
            return 0;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   539
        }
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   540
    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   541
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   542
    /*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   543
     * To locate the children we scan /proc looking for files that have a
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   544
     * position integer as a filename.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   545
     */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   546
    if ((dir = opendir("/proc")) == NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   547
        JNU_ThrowByNameWithLastError(env,
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   548
            "java/lang/RuntimeException", "Unable to open /proc");
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   549
        return -1;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   550
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   551
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   552
    do { // Block to break out of on Exception
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   553
        pids = (*env)->GetLongArrayElements(env, jarray, NULL);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   554
        if (pids == NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   555
            break;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   556
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   557
        if (jparentArray != NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   558
            ppids  = (*env)->GetLongArrayElements(env, jparentArray, NULL);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   559
            if (ppids == NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   560
                break;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   561
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   562
        }
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   563
        if (jstimesArray != NULL) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   564
            stimes  = (*env)->GetLongArrayElements(env, jstimesArray, NULL);
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   565
            if (stimes == NULL) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   566
                break;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   567
            }
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   568
        }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   569
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   570
        while ((ptr = readdir(dir)) != NULL) {
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   571
            pid_t ppid = 0;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   572
            jlong totalTime = 0L;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   573
            jlong startTime = 0L;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   574
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   575
            /* skip files that aren't numbers */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   576
            pid_t childpid = (pid_t) atoi(ptr->d_name);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   577
            if ((int) childpid <= 0) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   578
                continue;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   579
            }
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   580
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   581
            // Get the parent pid, and start time
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   582
            ppid = os_getParentPidAndTimings(env, childpid, &totalTime, &startTime);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   583
            if (ppid >= 0 && (pid == 0 || ppid == pid)) {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   584
                if (count < arraySize) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   585
                    // Only store if it fits
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   586
                    pids[count] = (jlong) childpid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   587
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   588
                    if (ppids != NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   589
                        // Store the parentPid
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   590
                        ppids[count] = (jlong) ppid;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   591
                    }
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   592
                    if (stimes != NULL) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   593
                        // Store the process start time
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   594
                        stimes[count] = startTime;
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   595
                    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   596
                }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   597
                count++; // Count to tabulate size needed
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   598
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   599
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   600
    } while (0);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   601
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   602
    if (pids != NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   603
        (*env)->ReleaseLongArrayElements(env, jarray, pids, 0);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   604
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   605
    if (ppids != NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   606
        (*env)->ReleaseLongArrayElements(env, jparentArray, ppids, 0);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   607
    }
31682
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   608
    if (stimes != NULL) {
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   609
        (*env)->ReleaseLongArrayElements(env, jstimesArray, stimes, 0);
c19dcf5e0b6d 8078099: (process) ProcessHandle should uniquely identify processes
rriggs
parents: 30899
diff changeset
   610
    }
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   611
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   612
    closedir(dir);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   613
    // If more pids than array had size for; count will be greater than array size
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   614
    return count;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   615
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   616
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   617
#endif // defined(__solaris__) || defined (__linux__) || defined(_AIX)
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   618
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   619
/*
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   620
 * The following functions are common on Solaris and AIX.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   621
 */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   622
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   623
#if defined(__solaris__) || defined(_AIX)
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   624
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   625
/**
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   626
 * Helper function to get the 'psinfo_t' data from "/proc/%d/psinfo".
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   627
 * Returns 0 on success and -1 on error.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   628
 */
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   629
static int getPsinfo(pid_t pid, psinfo_t *psinfo) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   630
    FILE* fp;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   631
    char fn[32];
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   632
    int ret;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   633
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   634
    /*
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   635
     * Try to open /proc/%d/psinfo
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   636
     */
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   637
    snprintf(fn, sizeof fn, "/proc/%d/psinfo", pid);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   638
    fp = fopen(fn, "r");
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   639
    if (fp == NULL) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   640
        return -1;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   641
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   642
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   643
    ret = fread(psinfo, 1, sizeof(psinfo_t), fp);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   644
    fclose(fp);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   645
    if (ret < sizeof(psinfo_t)) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   646
        return -1;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   647
    }
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   648
    return 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   649
}
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   650
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   651
/**
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   652
 * Read /proc/<pid>/psinfo and return the ppid, total cputime and start time.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   653
 * Return: -1 is fail;  >=  0 is parent pid
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   654
 * 'total' will contain the running time of 'pid' in nanoseconds.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   655
 * 'start' will contain the start time of 'pid' in milliseconds since epoch.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   656
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   657
pid_t unix_getParentPidAndTimings(JNIEnv *env, pid_t pid,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   658
                                  jlong *totalTime, jlong* startTime) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   659
    psinfo_t psinfo;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   660
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   661
    if (getPsinfo(pid, &psinfo) < 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   662
        return -1;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   663
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   664
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   665
    *totalTime = psinfo.pr_time.tv_sec * 1000000000L + psinfo.pr_time.tv_nsec;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   666
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   667
    *startTime = psinfo.pr_start.tv_sec * (jlong)1000 +
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   668
                 psinfo.pr_start.tv_nsec / 1000000;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   669
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   670
    return (pid_t) psinfo.pr_ppid;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   671
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   672
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   673
void unix_getCmdlineAndUserInfo(JNIEnv *env, jobject jinfo, pid_t pid) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   674
    psinfo_t psinfo;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   675
    char fn[32];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   676
    char exePath[PATH_MAX];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   677
    char prargs[PRARGSZ + 1];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   678
    jstring cmdexe = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   679
    int ret;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   680
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   681
    /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   682
     * On Solaris, the full path to the executable command is the link in
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   683
     * /proc/<pid>/paths/a.out. But it is only readable for processes we own.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   684
     */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   685
#if defined(__solaris__)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   686
    snprintf(fn, sizeof fn, "/proc/%d/path/a.out", pid);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   687
    if ((ret = readlink(fn, exePath, PATH_MAX - 1)) > 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   688
        // null terminate and create String to store for command
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   689
        exePath[ret] = '\0';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   690
        CHECK_NULL(cmdexe = JNU_NewStringPlatform(env, exePath));
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   691
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   692
#endif
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   693
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   694
    /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   695
     * Now try to open /proc/%d/psinfo
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   696
     */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   697
    if (getPsinfo(pid, &psinfo) < 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   698
        unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe, NULL);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   699
        return;
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   700
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   701
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   702
    unix_getUserInfo(env, jinfo, psinfo.pr_uid);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   703
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   704
    /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   705
     * Now read psinfo.pr_psargs which contains the first PRARGSZ characters of the
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   706
     * argument list (i.e. arg[0] arg[1] ...). Unfortunately, PRARGSZ is usually set
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   707
     * to 80 characters only. Nevertheless it's better than nothing :)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   708
     */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   709
    strncpy(prargs, psinfo.pr_psargs, PRARGSZ);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   710
    prargs[PRARGSZ] = '\0';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   711
    if (prargs[0] == '\0') {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   712
        /* If psinfo.pr_psargs didn't contain any strings, use psinfo.pr_fname
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   713
         * (which only contains the last component of exec()ed pathname) as a
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   714
         * last resort. This is true for AIX kernel processes for example.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   715
         */
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   716
        strncpy(prargs, psinfo.pr_fname, PRARGSZ);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   717
        prargs[PRARGSZ] = '\0';
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   718
    }
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   719
    unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   720
                      prargs[0] == '\0' ? NULL : prargs);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   721
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   722
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents: 31682
diff changeset
   723
#endif // defined(__solaris__) || defined(_AIX)