jdk/src/java.base/linux/native/libjava/ProcessHandleImpl_linux.c
author rriggs
Mon, 08 Aug 2016 13:55:16 -0400
changeset 40179 b5d59a6f093a
parent 32517 47fa336854c3
permissions -rw-r--r--
8161431: JNI pending exceptions in ProcessHandleImpl_linux.c and ProcessHandleImpl_unix.c Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     1
/*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     4
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    10
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    15
 * accompanied this code).
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    16
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    20
 *
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    23
 * questions.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    24
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    25
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    26
#include "jni.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    27
#include "jni_util.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    28
#include "java_lang_ProcessHandleImpl.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    29
#include "java_lang_ProcessHandleImpl_Info.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    30
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    31
#include "ProcessHandleImpl_unix.h"
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    32
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    33
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    34
#include <fcntl.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    35
#include <limits.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    36
#include <stdlib.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    37
#include <unistd.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    38
#include <sys/types.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    39
#include <sys/stat.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    40
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    41
#include <string.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    42
#include <ctype.h>
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    43
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    44
/*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    45
 * Implementation of native ProcessHandleImpl functions for Linux.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    46
 * See ProcessHandleImpl_unix.c for more details.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    47
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    48
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    49
/* Signatures for internal OS specific functions. */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    50
static long long getBoottime(JNIEnv *env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    51
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    52
/* A static offset in milliseconds since boot. */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    53
static long long bootTime_ms;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    54
static long clock_ticks_per_second;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    55
static int pageSize;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    56
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    57
void os_initNative(JNIEnv *env, jclass clazz) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    58
    bootTime_ms = getBoottime(env);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    59
    clock_ticks_per_second = sysconf(_SC_CLK_TCK);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    60
    pageSize = sysconf(_SC_PAGESIZE);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    61
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    62
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    63
jint os_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    64
                    jlongArray jparentArray, jlongArray jstimesArray) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    65
    return unix_getChildren(env, jpid, jarray, jparentArray, jstimesArray);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    66
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    67
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    68
/**
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    69
 * Read /proc/<pid>/stat and return the ppid, total cputime and start time.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    70
 * -1 is fail;  >=  0 is parent pid
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    71
 * 'total' will contain the running time of 'pid' in nanoseconds.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    72
 * 'start' will contain the start time of 'pid' in milliseconds since epoch.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    73
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    74
pid_t os_getParentPidAndTimings(JNIEnv *env, pid_t pid,
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    75
                                jlong *totalTime, jlong* startTime) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    76
    FILE* fp;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    77
    char buffer[2048];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    78
    int statlen;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    79
    char fn[32];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    80
    char* s;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    81
    int parentPid;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    82
    long unsigned int utime = 0;      // clock tics
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    83
    long unsigned int stime = 0;      // clock tics
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    84
    long long unsigned int start = 0; // microseconds
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    85
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    86
    /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    87
     * Try to stat and then open /proc/%d/stat
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    88
     */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    89
    snprintf(fn, sizeof fn, "/proc/%d/stat", pid);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    90
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    91
    fp = fopen(fn, "r");
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    92
    if (fp == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    93
        return -1;              // fail, no such /proc/pid/stat
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    94
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    95
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    96
    /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    97
     * The format is: pid (command) state ppid ...
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    98
     * As the command could be anything we must find the right most
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
    99
     * ")" and then skip the white spaces that follow it.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   100
     */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   101
    statlen = fread(buffer, 1, (sizeof buffer - 1), fp);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   102
    fclose(fp);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   103
    if (statlen < 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   104
        return -1;               // parent pid is not available
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   105
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   106
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   107
    buffer[statlen] = '\0';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   108
    s = strchr(buffer, '(');
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   109
    if (s == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   110
        return -1;               // parent pid is not available
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   111
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   112
    // Found start of command, skip to end
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   113
    s++;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   114
    s = strrchr(s, ')');
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   115
    if (s == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   116
        return -1;               // parent pid is not available
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   117
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   118
    s++;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   119
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   120
    // Scan the needed fields from status, retaining only ppid(4),
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   121
    // utime (14), stime(15), starttime(22)
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   122
    if (4 != sscanf(s, " %*c %d %*d %*d %*d %*d %*d %*u %*u %*u %*u %lu %lu %*d %*d %*d %*d %*d %*d %llu",
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   123
            &parentPid, &utime, &stime, &start)) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   124
        return 0;              // not all values parsed; return error
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   125
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   126
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   127
    *totalTime = (utime + stime) * (jlong)(1000000000 / clock_ticks_per_second);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   128
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   129
    *startTime = bootTime_ms + ((start * 1000) / clock_ticks_per_second);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   130
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   131
    return parentPid;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   132
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   133
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   134
void os_getCmdlineAndUserInfo(JNIEnv *env, jobject jinfo, pid_t pid) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   135
    int fd;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   136
    int cmdlen = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   137
    char *cmdline = NULL, *cmdEnd = NULL; // used for command line args and exe
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   138
    char *args = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   139
    jstring cmdexe = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   140
    char fn[32];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   141
    struct stat stat_buf;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   142
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   143
    /*
32517
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   144
     * Stat /proc/<pid> to get the user id
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   145
     */
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   146
    snprintf(fn, sizeof fn, "/proc/%d", pid);
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   147
    if (stat(fn, &stat_buf) == 0) {
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   148
        unix_getUserInfo(env, jinfo, stat_buf.st_uid);
40179
b5d59a6f093a 8161431: JNI pending exceptions in ProcessHandleImpl_linux.c and ProcessHandleImpl_unix.c
rriggs
parents: 32517
diff changeset
   149
        JNU_CHECK_EXCEPTION(env);
32517
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   150
    }
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   151
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   152
    /*
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   153
     * Try to open /proc/<pid>/cmdline
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   154
     */
32517
47fa336854c3 8133552: java/lang/ProcessHandle/InfoTest.java fails intermittently - incorrect user
rriggs
parents: 32209
diff changeset
   155
    strncat(fn, "/cmdline", sizeof fn - strnlen(fn, sizeof fn) - 1);
32209
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   156
    if ((fd = open(fn, O_RDONLY)) < 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   157
        return;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   158
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   159
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   160
    do {                // Block to break out of on errors
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   161
        int i, truncated = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   162
        int count;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   163
        char *s;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   164
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   165
        /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   166
         * The path name read by readlink() is limited to PATH_MAX characters.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   167
         * The content of /proc/<pid>/cmdline is limited to PAGE_SIZE characters.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   168
         */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   169
        cmdline = (char*)malloc((PATH_MAX > pageSize ? PATH_MAX : pageSize) + 1);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   170
        if (cmdline == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   171
            break;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   172
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   173
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   174
        /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   175
         * On Linux, the full path to the executable command is the link in
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   176
         * /proc/<pid>/exe. But it is only readable for processes we own.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   177
         */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   178
        snprintf(fn, sizeof fn, "/proc/%d/exe", pid);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   179
        if ((cmdlen = readlink(fn, cmdline, PATH_MAX)) > 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   180
            // null terminate and create String to store for command
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   181
            cmdline[cmdlen] = '\0';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   182
            cmdexe = JNU_NewStringPlatform(env, cmdline);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   183
            (*env)->ExceptionClear(env);        // unconditionally clear any exception
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   184
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   185
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   186
        /*
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   187
         * The command-line arguments appear as a set of strings separated by
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   188
         * null bytes ('\0'), with a further null byte after the last
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   189
         * string. The last string is only null terminated if the whole command
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   190
         * line is not exceeding (PAGE_SIZE - 1) characters.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   191
         */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   192
        cmdlen = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   193
        s = cmdline;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   194
        while ((count = read(fd, s, pageSize - cmdlen)) > 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   195
            cmdlen += count;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   196
            s += count;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   197
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   198
        if (count < 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   199
            break;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   200
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   201
        // We have to null-terminate because the process may have changed argv[]
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   202
        // or because the content in /proc/<pid>/cmdline is truncated.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   203
        cmdline[cmdlen] = '\0';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   204
        if (cmdlen == pageSize && cmdline[pageSize - 1] != '\0') {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   205
            truncated = 1;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   206
        } else if (cmdlen == 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   207
            // /proc/<pid>/cmdline was empty. This usually happens for kernel processes
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   208
            // like '[kthreadd]'. We could try to read /proc/<pid>/comm in the future.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   209
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   210
        if (cmdlen > 0 && (cmdexe == NULL || truncated)) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   211
            // We have no exact command or the arguments are truncated.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   212
            // In this case we save the command line from /proc/<pid>/cmdline.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   213
            args = (char*)malloc(pageSize + 1);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   214
            if (args != NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   215
                memcpy(args, cmdline, cmdlen + 1);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   216
                for (i = 0; i < cmdlen; i++) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   217
                    if (args[i] == '\0') {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   218
                        args[i] = ' ';
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   219
                    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   220
                }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   221
            }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   222
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   223
        i = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   224
        if (!truncated) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   225
            // Count the arguments
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   226
            cmdEnd = &cmdline[cmdlen];
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   227
            for (s = cmdline; *s != '\0' && (s < cmdEnd); i++) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   228
                s += strnlen(s, (cmdEnd - s)) + 1;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   229
            }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   230
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   231
        unix_fillArgArray(env, jinfo, i, cmdline, cmdEnd, cmdexe, args);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   232
    } while (0);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   233
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   234
    if (cmdline != NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   235
        free(cmdline);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   236
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   237
    if (args != NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   238
        free(args);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   239
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   240
    if (fd >= 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   241
        close(fd);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   242
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   243
}
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   244
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   245
/**
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   246
 * Read the boottime from /proc/stat.
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   247
 */
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   248
static long long getBoottime(JNIEnv *env) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   249
    FILE *fp;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   250
    char *line = NULL;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   251
    size_t len = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   252
    long long bootTime = 0;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   253
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   254
    fp = fopen("/proc/stat", "r");
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   255
    if (fp == NULL) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   256
        return -1;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   257
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   258
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   259
    while (getline(&line, &len, fp) != -1) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   260
        if (sscanf(line, "btime %llu", &bootTime) == 1) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   261
            break;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   262
        }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   263
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   264
    free(line);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   265
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   266
    if (fp != 0) {
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   267
        fclose(fp);
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   268
    }
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   269
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   270
    return bootTime * 1000;
24bb680a1609 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
diff changeset
   271
}