| version 1.8, 2001/12/13 17:33:40 |
version 1.9, 2001/12/20 00:26:52 |
|
|
| /* |
/* |
| * $Log$ |
* $Log$ |
| |
* Revision 1.9 2001/12/20 00:26:52 jombik9 |
| |
* Various Makefile & code improvements. |
| |
* wait() implementation - no error is reported on ctrl+d now |
| |
* |
| * Revision 1.8 2001/12/13 17:33:40 jombik9 |
* Revision 1.8 2001/12/13 17:33:40 jombik9 |
| * Steps to fix stream bug. |
* Steps to fix stream bug. |
| * |
* |
|
|
| * |
* |
| * 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.9 2001/12/20 00:26:52 jombik9 |
| |
* Added Various Makefile & code improvements. |
| |
* Added wait() implementation - no error is reported on ctrl+d now |
| |
* Added |
| * Added Revision 1.8 2001/12/13 17:33:40 jombik9 |
* Added Revision 1.8 2001/12/13 17:33:40 jombik9 |
| * Added Steps to fix stream bug. |
* Added Steps to fix stream bug. |
| * Added |
* Added |
|
|
| * Added Revision 1.6 2001/12/04 21:16:21 jombik9 |
* Added Revision 1.6 2001/12/04 21:16:21 jombik9 |
| * Added Some changes -- try to make support for stream oriented applications. |
* Added Some changes -- try to make support for stream oriented applications. |
| * Added |
* Added |
| * Added Revision 1.5 2001/12/04 17:17:52 jombik9 |
* Added Revision 1.5 2001/12/04 17:17:52 jombik9 |
| * Added n_link sychronizations |
* Added n_link sychronizations |
| * Added |
* Added |
| * Added Revision 1.4 2001/12/02 23:30:07 jombik9 |
* Added Revision 1.4 2001/12/02 23:30:07 jombik9 |
| * Added A lot of new functions added. |
* Added A lot of new functions added. |
| * Added Various improvements. |
* Added Various improvements. |
| * Added |
* 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 |
| Line 105 PROCTABLE proctable_create(const int siz |
|
| Line 113 PROCTABLE proctable_create(const int siz |
|
| } |
} |
| |
|
| process_init(ptbl[i]); |
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; |
return ptbl; |
| Line 147 proctable_get_from_array(size, array) |
|
| Line 148 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 167 proctable_get_from_array(size, array) |
|
| Line 168 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 177 proctable_get_from_array(size, array) |
|
| Line 178 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 293 proctable_update(ptbl) |
|
| Line 294 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 351 proctable_update(ptbl) |
|
| Line 352 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 390 proctable_create_pipes(ptbl) |
|
| Line 391 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 402 proctable_create_pipes(ptbl) |
|
| Line 403 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 444 proctable_exec(ptbl) |
|
| Line 445 proctable_exec(ptbl) |
|
| /* See the '1 ||' below. |
/* See the '1 ||' below. |
| close(ptbl[i]->fd[k].pipe[0]); |
close(ptbl[i]->fd[k].pipe[0]); |
| close(ptbl[i]->fd[k].pipe[1]); |
close(ptbl[i]->fd[k].pipe[1]); |
| */ |
*/ |
| } |
} |
| else { |
else { |
| #if DEBUG |
#if DEBUG |
| msg_debug_print("proctable_exec(): " |
msg_debug_print("proctable_exec(): " |
| "process %d, fd %d, no links defined\n", i, k); |
"process %d, fd %d, no links defined\n", i, k); |
| #endif |
#endif |
| } |
} |
| |
|
| Line 464 proctable_exec(ptbl) |
|
| Line 465 proctable_exec(ptbl) |
|
| for (k = 0; k < N_FD; k++) |
for (k = 0; k < N_FD; k++) |
| if (ptbl[i]->fd[k].n_link == 0) |
if (ptbl[i]->fd[k].n_link == 0) |
| close(k); |
close(k); |
| */ |
*/ |
| /* setvbuf(stdout, NULL, _IONBF, 0); */ |
/* setvbuf(stdout, NULL, _IONBF, 0); */ |
| /* setbuf(stdout, NULL); */ |
/* setbuf(stdout, NULL); */ |
| |
|
| for (k = 0; k < N_FD; k++) { |
/* |
| if (k != 0) { |
for (k = 0; k < N_FD; k++) { |
| fcntl(k, F_SETFL, O_NONBLOCK|O_NDELAY); |
if (k != 0) { |
| /* fcntl(k, F_SETFL, O_NDELAY); */ |
fcntl(k, F_SETFL, O_NONBLOCK|O_NDELAY); |
| msg_print("Running fcntl()\n"); |
msg_print("Running fcntl()\n"); |
| } |
} |
| /* fcntl(k, F_SETFL, O_NONBLOCK); */ |
} |
| } |
*/ |
| |
|
| /* execvp(ptbl[i]->argv[0], ptbl[i]->argv); */ |
execvp(ptbl[i]->argv[0], ptbl[i]->argv); |
| |
//execve(ptbl[i]->argv[0], ptbl[i]->argv, environ); |
| execve(ptbl[i]->argv[0], ptbl[i]->argv, environ); |
|
| /* |
/* |
| * RETURN VALUES |
* RETURN VALUES |
| * If any of the exec functions returns, an error will have |
* If any of the exec functions returns, an error will have |
| Line 551 proctable_remove_process(p_ptbl, index) |
|
| Line 552 proctable_remove_process(p_ptbl, index) |
|
| process_destroy((*p_ptbl)[index]); |
process_destroy((*p_ptbl)[index]); |
| |
|
| for (i = index; (*p_ptbl)[i] != NULL; i++) { |
for (i = index; (*p_ptbl)[i] != NULL; i++) { |
| (*p_ptbl)[i] = (*p_ptbl)[i+1]; |
(*p_ptbl)[i] = (*p_ptbl)[i + 1]; |
| } |
} |
| |
|
| /* Process table is a list of process ended with NULL. */ |
/* Process table is a list of process ended with NULL. */ |
| Line 562 proctable_remove_process(p_ptbl, index) |
|
| Line 563 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) |
(proctable_get_size(*p_ptbl) + 1) |
| * sizeof(struct process *)); |
* 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 589 proctable_print(ptbl) |
|
| Line 590 proctable_print(ptbl) |
|
| |
|
| 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 615 proctable_read(p_ptbl, p_fds) |
|
| Line 616 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 0 && DEBUG |
#if 0 && DEBUG |
| msg_debug_print("proctable_read(): descriptor change:" |
msg_debug_print("proctable_read(): descriptor change:" |
| " process %d, fd %d, datasize %d\n", |
" process %d, fd %d, datasize %d\n", |
| i, k, datasize); |
i, k, datasize); |
| #endif |
#endif |
| |
|
| if (datasize == 0) |
if (datasize == 0) |
| Line 629 proctable_read(p_ptbl, p_fds) |
|
| Line 630 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 655 proctable_read(p_ptbl, p_fds) |
|
| Line 656 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 { |
| #if 0 && DEBUG |
#if 0 && DEBUG |
| msg_debug_print("proctable_read(): descriptor write:" |
msg_debug_print("proctable_read(): descriptor write:" |
| " process %d, fd %d, datasize %d\n", |
" process %d, fd %d, datasize %d\n", |
| proc_idx, fd_write, datasize); |
proc_idx, fd_write, datasize); |
| #endif |
#endif |
| |
|
| switch (write(fd_write, buf, datasize)) { |
switch (write(fd_write, buf, datasize)) { |
| 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 747 proctable_wait(p_ptbl) |
|
| Line 748 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 763 proctable_wait(p_ptbl) |
|
| Line 764 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 778 proctable_wait(p_ptbl) |
|
| Line 779 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; |