| version 1.3, 2001/09/21 19:54:45 |
version 1.4, 2001/12/02 23:30:07 |
|
|
| /* |
/* |
| * $Log$ |
* $Log$ |
| |
* Revision 1.4 2001/12/02 23:30:07 jombik9 |
| |
* A lot of new functions added. |
| |
* Various improvements. |
| |
* |
| * Revision 1.3 2001/09/21 19:54:45 host8 |
* Revision 1.3 2001/09/21 19:54:45 host8 |
| * Cosmetics changes in *.c |
* Cosmetics changes in *.c |
| * |
* |
| * Revision 1.2 2001/09/21 19:44:51 host8 |
* Revision 1.2 2001/09/21 19:44:51 host8 |
| * Added $Log$ |
* Added $Log$ |
| |
* Added Revision 1.4 2001/12/02 23:30:07 jombik9 |
| |
* Added A lot of new functions added. |
| |
* Added Various improvements. |
| |
* Added |
| * Added Revision 1.3 2001/09/21 19:54:45 host8 |
* Added Revision 1.3 2001/09/21 19:54:45 host8 |
| * Added Cosmetics changes in *.c |
* Added Cosmetics changes in *.c |
| * Added keyword substitution |
* Added keyword substitution |
|
|
| # include <sys/wait.h> |
# include <sys/wait.h> |
| #endif |
#endif |
| |
|
| |
#include <cfg/str/strdyn.h> |
| |
|
| #include "proctable.h" |
#include "proctable.h" |
| #include "process.h" |
#include "process.h" |
| Line 50 proctable_get_size(ptbl) |
|
| Line 59 proctable_get_size(ptbl) |
|
| return i; |
return i; |
| } |
} |
| |
|
| |
PROCTABLE proctable_create(const int size) |
| |
{ |
| |
PROCTABLE ptbl; |
| |
register int i; |
| |
|
| |
ptbl = (PROCTABLE) malloc((size + 1) * sizeof(struct process *)); |
| |
if (ptbl == NULL) |
| |
return NULL; |
| |
|
| |
ptbl[size] = NULL; |
| |
|
| |
for (i = 0; i < size; i++) { |
| |
ptbl[i] = (struct process *) malloc(sizeof(struct process)); |
| |
if (ptbl[i] == NULL) { |
| |
proctable_destroy(&ptbl); |
| |
return NULL; |
| |
} |
| |
|
| |
process_init(ptbl[i]); |
| |
/* |
| |
printf("after process_init(): %p, %p, %p\n", |
| |
ptbl[i]->fd[0].link, |
| |
ptbl[i]->fd[1].link, |
| |
ptbl[i]->fd[2].link); |
| |
*/ |
| |
/* memset((void *) ptbl[i], '\0', sizeof(struct process)); */ |
| |
} |
| |
|
| |
return ptbl; |
| |
} |
| |
|
| PROCTABLE |
PROCTABLE |
| proctable_get_from_cmdline(argc, argv) |
proctable_get_from_cmdline(argc, argv) |
| Line 81 proctable_get_from_array(size, array) |
|
| Line 120 proctable_get_from_array(size, array) |
|
| |
|
| /* Process table is a list of process ended with NULL. */ |
/* Process table is a list of process ended with NULL. */ |
| ptbl = (PROCTABLE) realloc(ptbl, |
ptbl = (PROCTABLE) realloc(ptbl, |
| (n_proc + 2) * sizeof(struct process *)); |
(n_proc + 2) * sizeof(struct process *)); |
| if (ptbl == NULL) { |
if (ptbl == NULL) { |
| msg_error_print("realloc() failure\n"); |
msg_error_print("realloc() failure\n"); |
| return NULL; |
return NULL; |
| Line 101 proctable_get_from_array(size, array) |
|
| Line 140 proctable_get_from_array(size, array) |
|
| |
|
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_get_from_array(): " |
msg_debug_print("proctable_get_from_array(): " |
| "begin:end = %d:%d\n", begin_proc_id, end_proc_id); |
"begin:end = %d:%d\n", begin_proc_id, end_proc_id); |
| #endif |
#endif |
| |
|
| if (end_proc_id - begin_proc_id == 0) { |
if (end_proc_id - begin_proc_id == 0) { |
| Line 111 proctable_get_from_array(size, array) |
|
| Line 150 proctable_get_from_array(size, array) |
|
| } |
} |
| |
|
| if (process_init_by_array(ptbl[n_proc], |
if (process_init_by_array(ptbl[n_proc], |
| end_proc_id - begin_proc_id, |
end_proc_id - begin_proc_id, |
| array + begin_proc_id)) ; |
array + begin_proc_id)) ; |
| else { |
else { |
| proctable_destroy(&ptbl); |
proctable_destroy(&ptbl); |
| return NULL; |
return NULL; |
| Line 132 proctable_destroy(p_ptbl) |
|
| Line 171 proctable_destroy(p_ptbl) |
|
| if (p_ptbl != NULL) { |
if (p_ptbl != NULL) { |
| register int i; |
register int i; |
| |
|
| for (i = 0; *p_ptbl[i] != NULL; i++) { |
/* TODO: |
| process_destroy(*p_ptbl[i]); |
* Here was written *p_ptbl[i] insted (*p_ptbl)[i] on several places. |
| free(*p_ptbl[i]); |
* I supposed that it is the same. LEARN OPERATOR PRIORITIES ASAP!! |
| |
* -- Nepto [2/12/2001] |
| |
*/ |
| |
|
| |
for (i = 0; (*p_ptbl)[i] != NULL; i++) { |
| |
process_destroy((*p_ptbl)[i]); |
| |
free((*p_ptbl)[i]); |
| } |
} |
| free(*p_ptbl); |
|
| |
|
| |
free(*p_ptbl); |
| *p_ptbl = NULL; |
*p_ptbl = NULL; |
| } |
} |
| } |
} |
| Line 157 proctable_get_process_index_by_pid(ptbl, |
|
| Line 202 proctable_get_process_index_by_pid(ptbl, |
|
| return -1; |
return -1; |
| } |
} |
| |
|
| |
int proctable_get_process_index_by_name(PROCTABLE ptbl, char *name) |
| |
{ |
| |
register int i = 0; |
| |
|
| |
for (i = 0; ptbl[i] != NULL; i++) |
| |
if (ptbl[i]->name != NULL && ! strdyn_casecmp(ptbl[i]->name, name)) |
| |
return i; |
| |
|
| |
return -1; |
| |
} |
| |
|
| |
|
| int |
int |
| proctable_add_std_process(p_ptbl) |
proctable_add_std_process(p_ptbl) |
| Line 210 proctable_update(ptbl) |
|
| Line 266 proctable_update(ptbl) |
|
| |
|
| /* Invalid link? */ |
/* Invalid link? */ |
| if (ptbl[i]->fd[k].link[j].proc_id |
if (ptbl[i]->fd[k].link[j].proc_id |
| >= proctable_get_size(ptbl)) { |
>= proctable_get_size(ptbl)) { |
| msg_error_print("invalid process number -- %d\n", |
msg_error_print("invalid process number -- %d\n", |
| ptbl[i]->fd[k].link[j].proc_id); |
ptbl[i]->fd[k].link[j].proc_id); |
| return -1; |
return -1; |
| } |
} |
| |
|
| /* Selflink? */ |
/* Selflink? */ |
| if (i == ptbl[i]->fd[k].link[j].proc_id) { |
if (i == ptbl[i]->fd[k].link[j].proc_id) { |
| msg_error_print("cannot self link -- [%d].%s -> [%d].%s\n", |
msg_error_print("cannot self link -- [%d].%s -> [%d].%s\n", |
| i, fd_str[0][k], |
i, fd_str[0][k], |
| ptbl[i]->fd[k].link[j].proc_id, |
ptbl[i]->fd[k].link[j].proc_id, |
| fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
| return -1; |
return -1; |
| } |
} |
| |
|
| /* Duplicated link? */ |
/* Duplicated link? */ |
| if (fd_is_proclink(&ptbl[i]->fd[k], |
if (fd_is_proclink(&ptbl[i]->fd[k], |
| &ptbl[i]->fd[k].link[j]) - 1 != j) { |
&ptbl[i]->fd[k].link[j]) - 1 != j) { |
| msg_error_print( |
msg_error_print( |
| "duplicated link -- [%d].%s -> [%d].%s\n", |
"duplicated link -- [%d].%s -> [%d].%s\n", |
| i, fd_str[0][k], |
i, fd_str[0][k], |
| ptbl[i]->fd[k].link[j].proc_id, |
ptbl[i]->fd[k].link[j].proc_id, |
| fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
| return -1; |
return -1; |
| } |
} |
| |
|
| Line 268 proctable_update(ptbl) |
|
| Line 324 proctable_update(ptbl) |
|
| |
|
| } |
} |
| msg_error_print( |
msg_error_print( |
| "incorrect link -- [%d].%s -> [%d].%s\n", |
"incorrect link -- [%d].%s -> [%d].%s\n", |
| i, fd_str[0][k], |
i, fd_str[0][k], |
| ptbl[i]->fd[k].link[j].proc_id, |
ptbl[i]->fd[k].link[j].proc_id, |
| fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
| return -1; |
return -1; |
| } |
} |
| |
|
| /* If equivalent link doesn't exist, create it. */ |
/* If equivalent link doesn't exist, create it. */ |
| if (fd_is_proclink(&ptbl[ptbl[i]->fd[k].link[j].proc_id]-> |
if (fd_is_proclink(&ptbl[ptbl[i]->fd[k].link[j].proc_id]-> |
| fd[ptbl[i]->fd[k].link[j].fd_id], &link)) |
fd[ptbl[i]->fd[k].link[j].fd_id], &link)) |
| ; |
; |
| else |
else |
| fd_add_proclink(&ptbl[ptbl[i]->fd[k].link[j].proc_id]-> |
fd_add_proclink(&ptbl[ptbl[i]->fd[k].link[j].proc_id]-> |
| fd[ptbl[i]->fd[k].link[j].fd_id], &link); |
fd[ptbl[i]->fd[k].link[j].fd_id], &link); |
| } |
} |
| } |
} |
| } |
} |
| Line 306 proctable_create_pipes(ptbl) |
|
| Line 362 proctable_create_pipes(ptbl) |
|
| else { |
else { |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("creating pipe ptbl[%d]->fd[%d].pipe", |
msg_debug_print("creating pipe ptbl[%d]->fd[%d].pipe", |
| i, k); |
i, k); |
| #endif |
#endif |
| |
|
| if (pipe(ptbl[i]->fd[k].pipe) < 0) { |
if (pipe(ptbl[i]->fd[k].pipe) < 0) { |
| Line 318 proctable_create_pipes(ptbl) |
|
| Line 374 proctable_create_pipes(ptbl) |
|
| |
|
| #if DEBUG |
#if DEBUG |
| msg_print(" = (%d, %d)\n", |
msg_print(" = (%d, %d)\n", |
| ptbl[i]->fd[k].pipe[0], ptbl[i]->fd[k].pipe[1]); |
ptbl[i]->fd[k].pipe[0], ptbl[i]->fd[k].pipe[1]); |
| #endif |
#endif |
| } |
} |
| } |
} |
| Line 451 proctable_remove_process(p_ptbl, index) |
|
| Line 507 proctable_remove_process(p_ptbl, index) |
|
| /* So the final is '+1'. */ |
/* So the final is '+1'. */ |
| |
|
| *p_ptbl = (PROCTABLE) realloc(*p_ptbl, |
*p_ptbl = (PROCTABLE) realloc(*p_ptbl, |
| (proctable_get_size(*p_ptbl) + 1) * sizeof(struct process *)); |
(proctable_get_size(*p_ptbl) + 1) |
| |
* sizeof(struct process *)); |
| |
|
| if (*p_ptbl == NULL) { |
if (*p_ptbl == NULL) { |
| msg_error_print("proctable_remove_process(): realloc() failure\n"); |
msg_error_print("proctable_remove_process(): realloc() failure\n"); |
| Line 469 proctable_print(ptbl) |
|
| Line 526 proctable_print(ptbl) |
|
| register int i, j, k; |
register int i, j, k; |
| |
|
| for (i = 0; ptbl[i] != NULL; i++) { |
for (i = 0; ptbl[i] != NULL; i++) { |
| msg_print("Process %d: ", i); |
msg_print("Process %d (", i); |
| |
process_print_names(ptbl[i]); |
| |
msg_print("): "); |
| process_print_cmdline(ptbl[i]); |
process_print_cmdline(ptbl[i]); |
| msg_print("\n"); |
msg_print("\n"); |
| |
|
| for (k = 0; k < N_FD; k++) { |
for (k = 0; k < N_FD; k++) { |
| msg_print(" %s = (%d, %d)\n", fd_str[0][k], |
msg_print(" %s = (%d, %d)\n", fd_str[0][k], |
| ptbl[i]->fd[k].pipe[0], ptbl[i]->fd[k].pipe[1]); |
ptbl[i]->fd[k].pipe[0], ptbl[i]->fd[k].pipe[1]); |
| for (j = 0; j < ptbl[i]->fd[k].n_link; j++) |
for (j = 0; j < ptbl[i]->fd[k].n_link; j++) |
| msg_print(" [%d].%s %s [%d].%s\n", |
msg_print(" [%d].%s %s [%d].%s\n", |
| i, fd_str[0][k], |
i, fd_str[0][k], |
| ((i&&k) || (!i&&!k)) ? "->" : "<-", |
((i&&k) || (!i&&!k)) ? "->" : "<-", |
| ptbl[i]->fd[k].link[j].proc_id, |
ptbl[i]->fd[k].link[j].proc_id, |
| fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
fd_str[0][ptbl[i]->fd[k].link[j].fd_id]); |
| } |
} |
| } |
} |
| } |
} |
| Line 501 proctable_read(p_ptbl, p_fds) |
|
| Line 560 proctable_read(p_ptbl, p_fds) |
|
| register int j; |
register int j; |
| |
|
| datasize = read((*p_ptbl)[i]->fd[k].pipe[0], |
datasize = read((*p_ptbl)[i]->fd[k].pipe[0], |
| buf, BUFSIZE); |
buf, BUFSIZE); |
| |
|
| if (datasize == 0) |
if (datasize == 0) |
| close((*p_ptbl)[i]->fd[k].pipe[0]); |
close((*p_ptbl)[i]->fd[k].pipe[0]); |
| Line 509 proctable_read(p_ptbl, p_fds) |
|
| Line 568 proctable_read(p_ptbl, p_fds) |
|
| if (datasize < 0) { |
if (datasize < 0) { |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_read(): " |
msg_debug_print("proctable_read(): " |
| "read() failure, %d returned\n", |
"read() failure, %d returned\n", |
| datasize); |
datasize); |
| #endif |
#endif |
| msg_errno_print(); |
msg_errno_print(); |
| msg_print("Terminating..."); |
msg_print("Terminating..."); |
| Line 535 proctable_read(p_ptbl, p_fds) |
|
| Line 594 proctable_read(p_ptbl, p_fds) |
|
| p2.fd_id = (*p_ptbl)[i]->fd[k].link[j].fd_id; |
p2.fd_id = (*p_ptbl)[i]->fd[k].link[j].fd_id; |
| |
|
| fd_remove_proclink( |
fd_remove_proclink( |
| &(((*p_ptbl)[p2.proc_id])->fd[p2.fd_id]), &p1); |
&(((*p_ptbl)[p2.proc_id])->fd[p2.fd_id]), &p1); |
| fd_remove_proclink( |
fd_remove_proclink( |
| &(((*p_ptbl)[p1.proc_id])->fd[p1.fd_id]), &p2); |
&(((*p_ptbl)[p1.proc_id])->fd[p1.fd_id]), &p2); |
| |
|
| if (((*p_ptbl)[p2.proc_id])->fd[p2.fd_id].n_link == 0) |
if (((*p_ptbl)[p2.proc_id])->fd[p2.fd_id].n_link == 0) |
| close(((*p_ptbl)[p2.proc_id])-> |
close(((*p_ptbl)[p2.proc_id])-> |
| fd[p2.fd_id].pipe[1]); |
fd[p2.fd_id].pipe[1]); |
| |
|
| if (((*p_ptbl)[p2.proc_id])->fd[p2.fd_id].n_link == 0) |
if (((*p_ptbl)[p2.proc_id])->fd[p2.fd_id].n_link == 0) |
| close(((*p_ptbl)[p2.proc_id])-> |
close(((*p_ptbl)[p2.proc_id])-> |
| fd[p2.fd_id].pipe[1]); |
fd[p2.fd_id].pipe[1]); |
| |
|
| } |
} |
| else { |
else { |
| Line 553 proctable_read(p_ptbl, p_fds) |
|
| Line 612 proctable_read(p_ptbl, p_fds) |
|
| case -1: |
case -1: |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_read(): " |
msg_debug_print("proctable_read(): " |
| "write() failed: %s\n", |
"write() failed: %s\n", |
| strerror(errno)); |
strerror(errno)); |
| #endif |
#endif |
| break; |
break; |
| case 0: |
case 0: |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_read(): " |
msg_debug_print("proctable_read(): " |
| "write() returns 0: %s\n", |
"write() returns 0: %s\n", |
| strerror(errno)); |
strerror(errno)); |
| #endif |
#endif |
| break; |
break; |
| default: |
default: |
| Line 621 proctable_wait(p_ptbl) |
|
| Line 680 proctable_wait(p_ptbl) |
|
| switch (pid) { |
switch (pid) { |
| case -1: |
case -1: |
| msg_error_print("proctable_wait(): " |
msg_error_print("proctable_wait(): " |
| "waitpid() failure: %s\n", strerror(errno)); |
"waitpid() failure: %s\n", strerror(errno)); |
| exit(1); |
exit(1); |
| break; |
break; |
| |
|
| case 0: // no child available |
case 0: // no child available |
| msg_warn_print("proctable_wait(): " |
msg_warn_print("proctable_wait(): " |
| "waitpid() warning: no child available\n"); |
"waitpid() warning: no child available\n"); |
| break; |
break; |
| |
|
| default: |
default: |
| Line 637 proctable_wait(p_ptbl) |
|
| Line 696 proctable_wait(p_ptbl) |
|
| index = proctable_get_process_index_by_pid(*p_ptbl, pid); |
index = proctable_get_process_index_by_pid(*p_ptbl, pid); |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_wait(): " |
msg_debug_print("proctable_wait(): " |
| "PID = %d, index = %d\n", |
"PID = %d, index = %d\n", |
| index, pid); |
index, pid); |
| |
|
| msg_debug_print("proctable_wait(): " |
msg_debug_print("proctable_wait(): " |
| "proctable_get_size() = %d\n", |
"proctable_get_size() = %d\n", |
| proctable_get_size(*p_ptbl)); |
proctable_get_size(*p_ptbl)); |
| #endif |
#endif |
| if (WIFEXITED(status)) |
if (WIFEXITED(status)) |
| if (!WEXITSTATUS(status)) { |
if (!WEXITSTATUS(status)) { |
| Line 652 proctable_wait(p_ptbl) |
|
| Line 711 proctable_wait(p_ptbl) |
|
| } |
} |
| else { |
else { |
| msg_print("[%d] - error %d: %s - ", |
msg_print("[%d] - error %d: %s - ", |
| pid, |
pid, |
| WEXITSTATUS(status), |
WEXITSTATUS(status), |
| strerror( |
strerror( |
| WEXITSTATUS(status))); |
WEXITSTATUS(status))); |
| msg_array_print((*p_ptbl)[index]->argv); |
msg_array_print((*p_ptbl)[index]->argv); |
| msg_print("\n"); |
msg_print("\n"); |
| } |
} |
| |
|
| if (WIFSIGNALED(status)) { |
if (WIFSIGNALED(status)) { |
| msg_print("[%d] - killed by signal %d - ", |
msg_print("[%d] - killed by signal %d - ", |
| pid, |
pid, |
| WTERMSIG(status) ); |
WTERMSIG(status) ); |
| msg_array_print((*p_ptbl)[index]->argv); |
msg_array_print((*p_ptbl)[index]->argv); |
| msg_print("\n"); |
msg_print("\n"); |
| } |
} |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_wait(): " |
msg_debug_print("proctable_wait(): " |
| "removing process from process table\n"); |
"removing process from process table\n"); |
| #endif |
#endif |
| proctable_remove_process(p_ptbl, index); |
proctable_remove_process(p_ptbl, index); |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_wait(): " |
msg_debug_print("proctable_wait(): " |
| "proctable_get_size() = %d\n", |
"proctable_get_size() = %d\n", |
| proctable_get_size(*p_ptbl)); |
proctable_get_size(*p_ptbl)); |
| #endif |
#endif |
| } |
} |
| break; |
break; |