Advertisement
n8r0n

Modified Darwin for Getting Process Information

Jul 21st, 2012
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 KB | None | 0 0
  1. /*-
  2.  * This code relies heavily on the Darwin "ps" command, which is available
  3.  * from Apple in the adv_cmds portion of the Darwin distribution. The portions
  4.  * of this code which were included from that source are:
  5.  *
  6.  * Copyright (c) 1990, 1993, 1994
  7.  *  The Regents of the University of California.  All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *  This product includes software developed by the University of
  20.  *  California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  *
  37.  * The portions of this code which were necessary to tie into the Perl
  38.  * Proc::ProcessTable module are:
  39.  *
  40.  * Copyright (c) 2003, Thomas R. Wyant, III
  41.  *
  42.  * and may be reused under the same terms as Perl itself.
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <sys/types.h>
  48. #include <sys/time.h>
  49. #include <mach/mach_init.h>
  50. #include <mach/mach_port.h>
  51. #include <mach/mach_traps.h>
  52. #include <mach/mach_types.h>
  53. //#include <mach/shared_memory_server.h>   not available for iOS!
  54. #include <mach/task.h>
  55. #include <mach/thread_act.h>
  56. #include <mach/time_value.h>
  57. #include <mach/vm_map.h>
  58. #include <stdlib.h>
  59. #include <sys/proc.h>
  60. #include <sys/resource.h>
  61. #include <sys/stat.h>
  62. #include <sys/sysctl.h>
  63. //#include <kvm.h>
  64. #include <unistd.h>
  65.  
  66.  
  67. /* these are in sys/sysctl.h */
  68.  
  69. #define KI_PROC(ki) (&(ki)->ki_p->kp_proc)
  70. #define KI_EPROC(ki) (&(ki)->ki_p->kp_eproc)
  71. #define STATE_MAX       7
  72.  
  73. typedef struct thread_values {
  74.     struct thread_basic_info tb;
  75.     /* struct policy_infos  schedinfo; */
  76.     union {
  77.         struct policy_timeshare_info tshare;
  78.         struct policy_rr_info rr;
  79.         struct policy_fifo_info fifo;
  80.     } schedinfo;
  81. } thread_values_t;
  82.  
  83. struct usave {
  84.     struct  timeval u_start;
  85.     struct  rusage u_ru;
  86.     struct  rusage u_cru;
  87.     char    u_acflag;
  88.     char    u_valid;
  89. };
  90.  
  91. typedef struct kinfo {
  92.     struct kinfo_proc *ki_p;    /* proc structure */
  93.     struct usave ki_u;  /* interesting parts of user */
  94.     char *ki_args;      /* exec args */
  95.     char *ki_env;       /* environment */
  96.     task_port_t task;
  97.     int state;
  98.     int cpu_usage;
  99.     int curpri;
  100.     int basepri;
  101.     int swapped;
  102.     struct task_basic_info tasks_info;
  103.     struct task_thread_times_info times;
  104.     /* struct policy_infos  schedinfo; */
  105.     union {
  106.         struct policy_timeshare_info tshare;
  107.         struct policy_rr_info rr;
  108.         struct policy_fifo_info fifo;
  109.     } schedinfo;
  110.     int invalid_tinfo;
  111.     int thread_count;
  112.     thread_port_array_t thread_list;
  113.     thread_values_t *thval;
  114.     int invalid_thinfo;
  115. } KINFO;
  116.  
  117.  
  118. #define TIME_IN_MICROSECONDS 1
  119.  
  120. #ifdef TESTING
  121.  
  122. /* Note that FREE_BUFFERS is to be defined appropriately before
  123.  * DIE_HORRIBLY is used.
  124.  */
  125. #define DIE_HORRIBLY(s) {FREE_BUFFERS perror (s); exit (1);}
  126.  
  127. #else /* def TESTING */
  128.  
  129. #define DIE_HORRIBLY(s) {FREE_BUFFERS perror (s); return -1;}
  130.  
  131. extern void bless_into_proc(char* format, char** fields, ...);
  132.  
  133. #ifdef TIME_IN_MICROSECONDS
  134. static char *Format = "iiiiiiiiiiiiiiijjjllslsssss";
  135. #else /* def TIME_IN_MICROSECONDS */
  136. static char *Format = "iiiiiiiiiiiiiiilllllslsssss";
  137. #endif /* def TIME_IN_MICROSECONDS */
  138.  
  139. static char *Fields[] = {
  140.     "pid",      /* Process ID */
  141. #               define F_PID    0
  142.     "ppid",     /* Parent pid */
  143. #               define F_PPID   1
  144.     "pgrp",     /* Process group leader */
  145. #               define F_PGRP   2
  146.     "uid",      /* real uid */
  147. #               define F_UID    3
  148.     "gid",      /* real gid */
  149. #               define F_GID    4
  150.     "euid",     /* effective uid */
  151. #               define F_EID    5
  152.     "egid",     /* effective gid */
  153. #               define F_EGID   6
  154.     "suid",     /* saved uid */
  155. #               define F_SUID   7
  156.     "sgid",     /* saved gid */
  157. #               define F_SGID   8
  158.     "priority", /* proiority */
  159. #               define F_PRIORITY 9
  160.     "size",     /* virtual size (Kbytes) */
  161. #               define F_SIZE   10
  162.     "rss",      /* resident set size (Kbytes) */
  163. #               define F_RSS    11
  164.     "flags",    /* process flags */
  165. #               define F_FLAGS  12
  166.     "nice",     /* nice for CPU usage */
  167. #               define F_NICE   13
  168.     "sess",     /* session pointer */
  169. #               define F_SESS   14
  170.     "time",     /* total time (system + user, centi- or microseconds, depending) */
  171. #               define F_CPTICKS    15
  172.     "stime",    /* system time (centi- or micrseconds, depending) */
  173. #               define F_STIME      16
  174.     "utime",    /* user time (centi- or microseconds, depending) */
  175. #               define F_UTIME      17
  176.     "start",    /* Start time in seconds since epoch */
  177. #               define F_START      18
  178.     "wchan",    /* Wait channel (addr of system call) */
  179. #               define F_WCHAN      19
  180.     "ttydev",
  181. #               define F_TTYDEV     20
  182.     "ttynum",   /* device number for tty, or -1 */
  183. #               define F_TTYNUM     21
  184.     "pctcpu",   /* Percent cpu as string representing float */
  185. #               define F_PCTCPU     22
  186.     "pctmem",   /* Percent memory as string representing float */
  187. #               define F_PCTMEM     23
  188.     "state",    /* current run state (e.g. sleep, wait ...) */
  189. #               define F_STATE      24
  190.     "cmndline", /* entire command line */
  191. #               define F_CMNDLINE   25
  192.     "fname",    /* filename */
  193. #               define F_FNAME      26
  194. #               define F_LASTFIELD  26
  195. };
  196.  
  197. #endif /* def TESTING */
  198.  
  199. // pass in buffer to populate with results, and returns the number of bytes placed in buffer
  200. //  no overflow checking performed!
  201. int OS_get_table(char* buffer, size_t buflen);
  202. char* OS_initialize(void);
  203.  
  204.  
  205. static char *States[] = {
  206.     "", "idle", "run", "sleep", "stop", "zombie"
  207. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement