process.c (fab95c55e3b94e219044dc7a558632d08c198771) process.c (512b6fb1c14d4c34f23a3419b0789ad01914a899)
1/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>
9#include <signal.h>
1/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>
9#include <signal.h>
10#include <fcntl.h>
10#include <sys/mman.h>
11#include <sys/ptrace.h>
12#include <sys/wait.h>
13#include <asm/unistd.h>
14#include "init.h"
15#include "kern_constants.h"
16#include "longjmp.h"
17#include "os.h"

--- 5 unchanged lines hidden (view full) ---

23#define FAILURE_PID -1
24
25#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
26#define COMM_SCANF "%*[^)])"
27
28unsigned long os_process_pc(int pid)
29{
30 char proc_stat[STAT_PATH_LEN], buf[256];
11#include <sys/mman.h>
12#include <sys/ptrace.h>
13#include <sys/wait.h>
14#include <asm/unistd.h>
15#include "init.h"
16#include "kern_constants.h"
17#include "longjmp.h"
18#include "os.h"

--- 5 unchanged lines hidden (view full) ---

24#define FAILURE_PID -1
25
26#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
27#define COMM_SCANF "%*[^)])"
28
29unsigned long os_process_pc(int pid)
30{
31 char proc_stat[STAT_PATH_LEN], buf[256];
31 unsigned long pc;
32 unsigned long pc = ARBITRARY_ADDR;
32 int fd, err;
33
34 sprintf(proc_stat, "/proc/%d/stat", pid);
33 int fd, err;
34
35 sprintf(proc_stat, "/proc/%d/stat", pid);
35 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
36 fd = open(proc_stat, O_RDONLY, 0);
36 if (fd < 0) {
37 printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
37 if (fd < 0) {
38 printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
38 "err = %d\n", proc_stat, -fd);
39 return ARBITRARY_ADDR;
39 "errno = %d\n", proc_stat, errno);
40 goto out;
40 }
41 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
42 if (err < 0) {
43 printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
44 "err = %d\n", proc_stat, errno);
41 }
42 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
43 if (err < 0) {
44 printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
45 "err = %d\n", proc_stat, errno);
45 os_close_file(fd);
46 return ARBITRARY_ADDR;
46 goto out_close;
47 }
48 os_close_file(fd);
49 pc = ARBITRARY_ADDR;
50 if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
47 }
48 os_close_file(fd);
49 pc = ARBITRARY_ADDR;
50 if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
51 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc) != 1) {
51 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc) != 1)
53 printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
54 buf);
53 printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
54 buf);
55 }
55 out_close:
56 close(fd);
57 out:
56 return pc;
57}
58
59int os_process_parent(int pid)
60{
61 char stat[STAT_PATH_LEN];
62 char data[256];
58 return pc;
59}
60
61int os_process_parent(int pid)
62{
63 char stat[STAT_PATH_LEN];
64 char data[256];
63 int parent, n, fd;
65 int parent = FAILURE_PID, n, fd;
64
65 if (pid == -1)
66
67 if (pid == -1)
66 return -1;
68 return parent;
67
68 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
69
70 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
69 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
71 fd = open(stat, O_RDONLY, 0);
70 if (fd < 0) {
72 if (fd < 0) {
71 printk(UM_KERN_ERR "Couldn't open '%s', err = %d\n", stat, -fd);
72 return FAILURE_PID;
73 printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
74 errno);
75 return parent;
73 }
74
75 CATCH_EINTR(n = read(fd, data, sizeof(data)));
76 }
77
78 CATCH_EINTR(n = read(fd, data, sizeof(data)));
76 os_close_file(fd);
79 close(fd);
77
78 if (n < 0) {
80
81 if (n < 0) {
79 printk(UM_KERN_ERR "Couldn't read '%s', err = %d\n", stat,
82 printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
80 errno);
83 errno);
81 return FAILURE_PID;
84 return parent;
82 }
83
84 parent = FAILURE_PID;
85 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
86 if (n != 1)
87 printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
88
89 return parent;

--- 174 unchanged lines hidden ---
85 }
86
87 parent = FAILURE_PID;
88 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
89 if (n != 1)
90 printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
91
92 return parent;

--- 174 unchanged lines hidden ---