Diffstat (limited to 'noncore/apps/opie-console/procctl.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-console/procctl.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/noncore/apps/opie-console/procctl.cpp b/noncore/apps/opie-console/procctl.cpp index a44529b..5239e26 100644 --- a/noncore/apps/opie-console/procctl.cpp +++ b/noncore/apps/opie-console/procctl.cpp | |||
@@ -4,94 +4,97 @@ | |||
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | 5 | ||
6 | #include "procctl.h" | 6 | #include "procctl.h" |
7 | 7 | ||
8 | ProcContainer *ProcCtl::m_last = 0; | 8 | ProcContainer *ProcCtl::m_last = 0; |
9 | ProcCtl* ProcCtl::m_self = 0; | 9 | ProcCtl* ProcCtl::m_self = 0; |
10 | 10 | ||
11 | ProcCtl::ProcCtl() { | 11 | ProcCtl::ProcCtl() { |
12 | signal( SIGCHLD, signal_handler ); | 12 | signal( SIGCHLD, signal_handler ); |
13 | } | 13 | } |
14 | ProcCtl::~ProcCtl() { | 14 | ProcCtl::~ProcCtl() { |
15 | } | 15 | } |
16 | ProcCtl* ProcCtl::self() { | 16 | ProcCtl* ProcCtl::self() { |
17 | if (!m_self ) { | 17 | if (!m_self ) { |
18 | m_self = new ProcCtl; | 18 | m_self = new ProcCtl; |
19 | } | 19 | } |
20 | return m_self; | 20 | return m_self; |
21 | } | 21 | } |
22 | void ProcCtl::add(pid_t pi, int fd ) { | 22 | void ProcCtl::add(pid_t pi, int fd ) { |
23 | ProcContainer * con = new ProcContainer; | 23 | ProcContainer * con = new ProcContainer; |
24 | //memset(con, 0, sizeof(con) ); | 24 | //memset(con, 0, sizeof(con) ); |
25 | con->pid = pi; | 25 | con->pid = pi; |
26 | con->fd = fd; | 26 | con->fd = fd; |
27 | con->status = 0; | 27 | con->status = 0; |
28 | con->prev = m_last; | 28 | con->prev = m_last; |
29 | 29 | ||
30 | m_last = con; | 30 | m_last = con; |
31 | 31 | ||
32 | } | 32 | } |
33 | void ProcCtl::remove( pid_t pi ) { | 33 | void ProcCtl::remove( pid_t pi ) { |
34 | /* | 34 | /* |
35 | * We first check if the last item | 35 | * We first check if the last item |
36 | * is equal to pi the we | 36 | * is equal to pi the we |
37 | * | 37 | * |
38 | */ | 38 | */ |
39 | ProcContainer* con; | 39 | ProcContainer* con; |
40 | if (m_last->pid == pi ) { | 40 | if (m_last->pid == pi ) { |
41 | con = m_last; | 41 | con = m_last; |
42 | m_last = con->prev; | 42 | m_last = con->prev; |
43 | delete con; | 43 | delete con; |
44 | return; | 44 | return; |
45 | } | 45 | } |
46 | 46 | ||
47 | con = m_last; | 47 | con = m_last; |
48 | ProcContainer* forw = 0l; | 48 | ProcContainer* forw = 0l; |
49 | while (con ) { | 49 | while (con ) { |
50 | /* remove it */ | 50 | /* remove it */ |
51 | if ( pi == con->pid ) { | 51 | if ( pi == con->pid ) { |
52 | forw->prev = con->prev; | 52 | if (forw) |
53 | forw->prev = con->prev; | ||
54 | else | ||
55 | forw = con->prev; | ||
53 | delete con; | 56 | delete con; |
54 | return; | 57 | return; |
55 | } | 58 | } |
56 | 59 | ||
57 | forw = con; | 60 | forw = con; |
58 | con = con->prev; | 61 | con = con->prev; |
59 | } | 62 | } |
60 | 63 | ||
61 | } | 64 | } |
62 | void ProcCtl::remove( ProcContainer con ) { | 65 | void ProcCtl::remove( ProcContainer con ) { |
63 | remove( con.pid ); | 66 | remove( con.pid ); |
64 | } | 67 | } |
65 | int ProcCtl::status(pid_t pid )const{ | 68 | int ProcCtl::status(pid_t pid )const{ |
66 | ProcContainer *con = m_last; | 69 | ProcContainer *con = m_last; |
67 | while (con) { | 70 | while (con) { |
68 | if (con->pid == pid ) | 71 | if (con->pid == pid ) |
69 | return con->status; | 72 | return con->status; |
70 | con = con->prev; | 73 | con = con->prev; |
71 | } | 74 | } |
72 | return -1; | 75 | return -1; |
73 | } | 76 | } |
74 | void ProcCtl::signal_handler(int) { | 77 | void ProcCtl::signal_handler(int) { |
75 | int status; | 78 | int status; |
76 | signal( SIGCHLD, signal_handler ); | 79 | signal( SIGCHLD, signal_handler ); |
77 | pid_t pi = waitpid( -1, &status, WNOHANG ); | 80 | pid_t pi = waitpid( -1, &status, WNOHANG ); |
78 | 81 | ||
79 | /* | 82 | /* |
80 | * find the container for pid | 83 | * find the container for pid |
81 | * | 84 | * |
82 | */ | 85 | */ |
83 | if ( pi < 0 ) { | 86 | if ( pi < 0 ) { |
84 | return; | 87 | return; |
85 | } | 88 | } |
86 | 89 | ||
87 | ProcContainer* con = m_last; | 90 | ProcContainer* con = m_last; |
88 | while (con) { | 91 | while (con) { |
89 | if ( con->pid == pi ) { | 92 | if ( con->pid == pi ) { |
90 | con->status = status; | 93 | con->status = status; |
91 | char result = 1; | 94 | char result = 1; |
92 | /* give a 'signal' */ | 95 | /* give a 'signal' */ |
93 | ::write(con->fd, &result, 1 ); | 96 | ::write(con->fd, &result, 1 ); |
94 | } | 97 | } |
95 | con = con->prev; | 98 | con = con->prev; |
96 | } | 99 | } |
97 | } | 100 | } |