< prev index next >

src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c

Print this page
rev 59105 : imported patch corelibs


  28 #include "java_lang_ProcessHandleImpl.h"
  29 #include "java_lang_ProcessHandleImpl_Info.h"
  30 
  31 #include "ProcessHandleImpl_unix.h"
  32 
  33 
  34 #include <stdio.h>
  35 #include <errno.h>
  36 #include <fcntl.h>
  37 #include <signal.h>
  38 #include <stdlib.h>
  39 #include <unistd.h>
  40 #include <string.h>
  41 #include <dirent.h>
  42 #include <ctype.h>
  43 #include <limits.h>
  44 #include <sys/types.h>
  45 #include <sys/stat.h>
  46 #include <sys/wait.h>
  47 
  48 /* For POSIX-compliant getpwuid_r on Solaris */
  49 #if defined(__solaris__)
  50 #define _POSIX_PTHREAD_SEMANTICS
  51 #endif
  52 #include <pwd.h>
  53 
  54 #ifdef _AIX
  55 #include <sys/procfs.h>
  56 #endif
  57 #ifdef __solaris__
  58 #include <procfs.h>
  59 #endif
  60 
  61 #if defined(_AIX)

  62   #define DIR DIR64
  63   #define dirent dirent64
  64   #define opendir opendir64
  65   #define readdir readdir64
  66   #define closedir closedir64
  67 #endif
  68 
  69 /**
  70  * This file contains the implementation of the native ProcessHandleImpl
  71  * functions which are common to all Unix variants.
  72  *
  73  * The currently supported Unix variants are Solaris, Linux, MaxOS X and AIX.
  74  * The various similarities and differences between these systems make it hard
  75  * to find a clear boundary between platform specific and shared code.
  76  *
  77  * In order to ease code sharing between the platforms while still keeping the
  78  * code as clean as possible (i.e. free of preprocessor macros) we use the
  79  * following source code layout (remember that ProcessHandleImpl_unix.c will
  80  * be compiled on EVERY Unix platform while ProcessHandleImpl_<os>.c will be
  81  * only compiled on the specific OS):


 121  * functionality into corresponding, platform-specific os_ functions.
 122  */
 123 
 124 
 125 #ifndef WIFEXITED
 126 #define WIFEXITED(status) (((status)&0xFF) == 0)
 127 #endif
 128 
 129 #ifndef WEXITSTATUS
 130 #define WEXITSTATUS(status) (((status)>>8)&0xFF)
 131 #endif
 132 
 133 #ifndef WIFSIGNALED
 134 #define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
 135 #endif
 136 
 137 #ifndef WTERMSIG
 138 #define WTERMSIG(status) ((status)&0x7F)
 139 #endif
 140 
 141 #ifdef __solaris__
 142 /* The child exited because of a signal.
 143  * The best value to return is 0x80 + signal number,
 144  * because that is what all Unix shells do, and because
 145  * it allows callers to distinguish between process exit and
 146  * process death by signal.
 147  * Unfortunately, the historical behavior on Solaris is to return
 148  * the signal number, and we preserve this for compatibility. */
 149 #define WTERMSIG_RETURN(status) WTERMSIG(status)
 150 #else
 151 #define WTERMSIG_RETURN(status) (WTERMSIG(status) + 0x80)
 152 #endif
 153 
 154 #define RESTARTABLE(_cmd, _result) do { \
 155   do { \
 156     _result = _cmd; \
 157   } while((_result == -1) && (errno == EINTR)); \
 158 } while(0)
 159 
 160 #define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
 161   do { \
 162     _result = _cmd; \
 163   } while((_result == NULL) && (errno == EINTR)); \
 164 } while(0)
 165 
 166 
 167 /* Field id for jString 'command' in java.lang.ProcessHandleImpl.Info */
 168 jfieldID ProcessHandleImpl_Info_commandID;
 169 
 170 /* Field id for jString 'commandLine' in java.lang.ProcessHandleImpl.Info */
 171 jfieldID ProcessHandleImpl_Info_commandLineID;
 172 


 486         struct passwd pwent;
 487         struct passwd* p = NULL;
 488         RESTARTABLE(getpwuid_r(uid, &pwent, pwbuf, (size_t)getpw_buf_size, &p), result);
 489 
 490         // Create the Java String if a name was found
 491         if (result == 0 && p != NULL &&
 492             p->pw_name != NULL && *(p->pw_name) != '\0') {
 493             name = JNU_NewStringPlatform(env, p->pw_name);
 494         }
 495         free(pwbuf);
 496     }
 497     if (name != NULL) {
 498         (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_userID, name);
 499     }
 500 }
 501 
 502 /*
 503  * The following functions are common on Solaris, Linux and AIX.
 504  */
 505 
 506 #if defined(__solaris__) || defined (__linux__) || defined(_AIX)
 507 
 508 /*
 509  * Returns the children of the requested pid and optionally each parent and
 510  * start time.
 511  * Reads /proc and accumulates any process who parent pid matches.
 512  * The resulting pids are stored into the array of longs.
 513  * The number of pids is returned if they all fit.
 514  * If the array is too short, the negative of the desired length is returned.
 515  */
 516 jint unix_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
 517                       jlongArray jparentArray, jlongArray jstimesArray) {
 518     DIR* dir;
 519     struct dirent* ptr;
 520     pid_t pid = (pid_t) jpid;
 521     jlong* pids = NULL;
 522     jlong* ppids = NULL;
 523     jlong* stimes = NULL;
 524     jsize parentArraySize = 0;
 525     jsize arraySize = 0;
 526     jsize stimesSize = 0;


 605                 count++; // Count to tabulate size needed
 606             }
 607         }
 608     } while (0);
 609 
 610     if (pids != NULL) {
 611         (*env)->ReleaseLongArrayElements(env, jarray, pids, 0);
 612     }
 613     if (ppids != NULL) {
 614         (*env)->ReleaseLongArrayElements(env, jparentArray, ppids, 0);
 615     }
 616     if (stimes != NULL) {
 617         (*env)->ReleaseLongArrayElements(env, jstimesArray, stimes, 0);
 618     }
 619 
 620     closedir(dir);
 621     // If more pids than array had size for; count will be greater than array size
 622     return count;
 623 }
 624 
 625 #endif // defined(__solaris__) || defined (__linux__) || defined(_AIX)
 626 
 627 /*
 628  * The following functions are common on Solaris and AIX.
 629  */
 630 
 631 #if defined(__solaris__) || defined(_AIX)
 632 
 633 /**
 634  * Helper function to get the 'psinfo_t' data from "/proc/%d/psinfo".
 635  * Returns 0 on success and -1 on error.
 636  */
 637 static int getPsinfo(pid_t pid, psinfo_t *psinfo) {
 638     FILE* fp;
 639     char fn[32];
 640     int ret;
 641 
 642     /*
 643      * Try to open /proc/%d/psinfo
 644      */
 645     snprintf(fn, sizeof fn, "/proc/%d/psinfo", pid);
 646     fp = fopen(fn, "r");
 647     if (fp == NULL) {
 648         return -1;
 649     }
 650 
 651     ret = fread(psinfo, 1, sizeof(psinfo_t), fp);


 675         return -1;
 676     }
 677 
 678     *totalTime = psinfo.pr_time.tv_sec * 1000000000L + psinfo.pr_time.tv_nsec;
 679 
 680     *startTime = psinfo.pr_start.tv_sec * (jlong)1000 +
 681                  psinfo.pr_start.tv_nsec / 1000000;
 682 
 683     return (pid_t) psinfo.pr_ppid;
 684 }
 685 
 686 void unix_getCmdlineAndUserInfo(JNIEnv *env, jobject jinfo, pid_t pid) {
 687     psinfo_t psinfo;
 688     char fn[32];
 689     char exePath[PATH_MAX];
 690     char prargs[PRARGSZ + 1];
 691     jstring cmdexe = NULL;
 692     int ret;
 693 
 694     /*
 695      * On Solaris, the full path to the executable command is the link in
 696      * /proc/<pid>/paths/a.out. But it is only readable for processes we own.
 697      */
 698 #if defined(__solaris__)
 699     snprintf(fn, sizeof fn, "/proc/%d/path/a.out", pid);
 700     if ((ret = readlink(fn, exePath, PATH_MAX - 1)) > 0) {
 701         // null terminate and create String to store for command
 702         exePath[ret] = '\0';
 703         CHECK_NULL(cmdexe = JNU_NewStringPlatform(env, exePath));
 704     }
 705 #endif
 706 
 707     /*
 708      * Now try to open /proc/%d/psinfo
 709      */
 710     if (getPsinfo(pid, &psinfo) < 0) {
 711         unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe, NULL);
 712         return;
 713     }
 714 
 715     unix_getUserInfo(env, jinfo, psinfo.pr_uid);
 716 
 717     /*
 718      * Now read psinfo.pr_psargs which contains the first PRARGSZ characters of the
 719      * argument list (i.e. arg[0] arg[1] ...). Unfortunately, PRARGSZ is usually set
 720      * to 80 characters only. Nevertheless it's better than nothing :)
 721      */
 722     strncpy(prargs, psinfo.pr_psargs, PRARGSZ);
 723     prargs[PRARGSZ] = '\0';
 724     if (prargs[0] == '\0') {
 725         /* If psinfo.pr_psargs didn't contain any strings, use psinfo.pr_fname
 726          * (which only contains the last component of exec()ed pathname) as a
 727          * last resort. This is true for AIX kernel processes for example.
 728          */
 729         strncpy(prargs, psinfo.pr_fname, PRARGSZ);
 730         prargs[PRARGSZ] = '\0';
 731     }
 732     unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe,
 733                       prargs[0] == '\0' ? NULL : prargs);
 734 }
 735 
 736 #endif // defined(__solaris__) || defined(_AIX)


  28 #include "java_lang_ProcessHandleImpl.h"
  29 #include "java_lang_ProcessHandleImpl_Info.h"
  30 
  31 #include "ProcessHandleImpl_unix.h"
  32 
  33 
  34 #include <stdio.h>
  35 #include <errno.h>
  36 #include <fcntl.h>
  37 #include <signal.h>
  38 #include <stdlib.h>
  39 #include <unistd.h>
  40 #include <string.h>
  41 #include <dirent.h>
  42 #include <ctype.h>
  43 #include <limits.h>
  44 #include <sys/types.h>
  45 #include <sys/stat.h>
  46 #include <sys/wait.h>
  47 




  48 #include <pwd.h>
  49 







  50 #if defined(_AIX)
  51   #include <sys/procfs.h>
  52   #define DIR DIR64
  53   #define dirent dirent64
  54   #define opendir opendir64
  55   #define readdir readdir64
  56   #define closedir closedir64
  57 #endif
  58 
  59 /**
  60  * This file contains the implementation of the native ProcessHandleImpl
  61  * functions which are common to all Unix variants.
  62  *
  63  * The currently supported Unix variants are Solaris, Linux, MaxOS X and AIX.
  64  * The various similarities and differences between these systems make it hard
  65  * to find a clear boundary between platform specific and shared code.
  66  *
  67  * In order to ease code sharing between the platforms while still keeping the
  68  * code as clean as possible (i.e. free of preprocessor macros) we use the
  69  * following source code layout (remember that ProcessHandleImpl_unix.c will
  70  * be compiled on EVERY Unix platform while ProcessHandleImpl_<os>.c will be
  71  * only compiled on the specific OS):


 111  * functionality into corresponding, platform-specific os_ functions.
 112  */
 113 
 114 
 115 #ifndef WIFEXITED
 116 #define WIFEXITED(status) (((status)&0xFF) == 0)
 117 #endif
 118 
 119 #ifndef WEXITSTATUS
 120 #define WEXITSTATUS(status) (((status)>>8)&0xFF)
 121 #endif
 122 
 123 #ifndef WIFSIGNALED
 124 #define WIFSIGNALED(status) (((status)&0xFF) > 0 && ((status)&0xFF00) == 0)
 125 #endif
 126 
 127 #ifndef WTERMSIG
 128 #define WTERMSIG(status) ((status)&0x7F)
 129 #endif
 130 

 131 /* The child exited because of a signal.
 132  * The best value to return is 0x80 + signal number,
 133  * because that is what all Unix shells do, and because
 134  * it allows callers to distinguish between process exit and
 135  * process death by signal.
 136  */



 137 #define WTERMSIG_RETURN(status) (WTERMSIG(status) + 0x80)

 138 
 139 #define RESTARTABLE(_cmd, _result) do { \
 140   do { \
 141     _result = _cmd; \
 142   } while((_result == -1) && (errno == EINTR)); \
 143 } while(0)
 144 
 145 #define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
 146   do { \
 147     _result = _cmd; \
 148   } while((_result == NULL) && (errno == EINTR)); \
 149 } while(0)
 150 
 151 
 152 /* Field id for jString 'command' in java.lang.ProcessHandleImpl.Info */
 153 jfieldID ProcessHandleImpl_Info_commandID;
 154 
 155 /* Field id for jString 'commandLine' in java.lang.ProcessHandleImpl.Info */
 156 jfieldID ProcessHandleImpl_Info_commandLineID;
 157 


 471         struct passwd pwent;
 472         struct passwd* p = NULL;
 473         RESTARTABLE(getpwuid_r(uid, &pwent, pwbuf, (size_t)getpw_buf_size, &p), result);
 474 
 475         // Create the Java String if a name was found
 476         if (result == 0 && p != NULL &&
 477             p->pw_name != NULL && *(p->pw_name) != '\0') {
 478             name = JNU_NewStringPlatform(env, p->pw_name);
 479         }
 480         free(pwbuf);
 481     }
 482     if (name != NULL) {
 483         (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_userID, name);
 484     }
 485 }
 486 
 487 /*
 488  * The following functions are common on Solaris, Linux and AIX.
 489  */
 490 
 491 #if defined (__linux__) || defined(_AIX)
 492 
 493 /*
 494  * Returns the children of the requested pid and optionally each parent and
 495  * start time.
 496  * Reads /proc and accumulates any process who parent pid matches.
 497  * The resulting pids are stored into the array of longs.
 498  * The number of pids is returned if they all fit.
 499  * If the array is too short, the negative of the desired length is returned.
 500  */
 501 jint unix_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
 502                       jlongArray jparentArray, jlongArray jstimesArray) {
 503     DIR* dir;
 504     struct dirent* ptr;
 505     pid_t pid = (pid_t) jpid;
 506     jlong* pids = NULL;
 507     jlong* ppids = NULL;
 508     jlong* stimes = NULL;
 509     jsize parentArraySize = 0;
 510     jsize arraySize = 0;
 511     jsize stimesSize = 0;


 590                 count++; // Count to tabulate size needed
 591             }
 592         }
 593     } while (0);
 594 
 595     if (pids != NULL) {
 596         (*env)->ReleaseLongArrayElements(env, jarray, pids, 0);
 597     }
 598     if (ppids != NULL) {
 599         (*env)->ReleaseLongArrayElements(env, jparentArray, ppids, 0);
 600     }
 601     if (stimes != NULL) {
 602         (*env)->ReleaseLongArrayElements(env, jstimesArray, stimes, 0);
 603     }
 604 
 605     closedir(dir);
 606     // If more pids than array had size for; count will be greater than array size
 607     return count;
 608 }
 609 
 610 #endif // defined (__linux__) || defined(_AIX)
 611 
 612 /*
 613  * The following functions are for AIX.
 614  */
 615 
 616 #if defined(_AIX)
 617 
 618 /**
 619  * Helper function to get the 'psinfo_t' data from "/proc/%d/psinfo".
 620  * Returns 0 on success and -1 on error.
 621  */
 622 static int getPsinfo(pid_t pid, psinfo_t *psinfo) {
 623     FILE* fp;
 624     char fn[32];
 625     int ret;
 626 
 627     /*
 628      * Try to open /proc/%d/psinfo
 629      */
 630     snprintf(fn, sizeof fn, "/proc/%d/psinfo", pid);
 631     fp = fopen(fn, "r");
 632     if (fp == NULL) {
 633         return -1;
 634     }
 635 
 636     ret = fread(psinfo, 1, sizeof(psinfo_t), fp);


 660         return -1;
 661     }
 662 
 663     *totalTime = psinfo.pr_time.tv_sec * 1000000000L + psinfo.pr_time.tv_nsec;
 664 
 665     *startTime = psinfo.pr_start.tv_sec * (jlong)1000 +
 666                  psinfo.pr_start.tv_nsec / 1000000;
 667 
 668     return (pid_t) psinfo.pr_ppid;
 669 }
 670 
 671 void unix_getCmdlineAndUserInfo(JNIEnv *env, jobject jinfo, pid_t pid) {
 672     psinfo_t psinfo;
 673     char fn[32];
 674     char exePath[PATH_MAX];
 675     char prargs[PRARGSZ + 1];
 676     jstring cmdexe = NULL;
 677     int ret;
 678 
 679     /*













 680      * Now try to open /proc/%d/psinfo
 681      */
 682     if (getPsinfo(pid, &psinfo) < 0) {
 683         unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe, NULL);
 684         return;
 685     }
 686 
 687     unix_getUserInfo(env, jinfo, psinfo.pr_uid);
 688 
 689     /*
 690      * Now read psinfo.pr_psargs which contains the first PRARGSZ characters of the
 691      * argument list (i.e. arg[0] arg[1] ...). Unfortunately, PRARGSZ is usually set
 692      * to 80 characters only. Nevertheless it's better than nothing :)
 693      */
 694     strncpy(prargs, psinfo.pr_psargs, PRARGSZ);
 695     prargs[PRARGSZ] = '\0';
 696     if (prargs[0] == '\0') {
 697         /* If psinfo.pr_psargs didn't contain any strings, use psinfo.pr_fname
 698          * (which only contains the last component of exec()ed pathname) as a
 699          * last resort. This is true for AIX kernel processes for example.
 700          */
 701         strncpy(prargs, psinfo.pr_fname, PRARGSZ);
 702         prargs[PRARGSZ] = '\0';
 703     }
 704     unix_fillArgArray(env, jinfo, 0, NULL, NULL, cmdexe,
 705                       prargs[0] == '\0' ? NULL : prargs);
 706 }
 707 
 708 #endif // defined(_AIX)
< prev index next >