/* * ep - extended pipelining * * process.c - process structure implementation * ____________________________________________________________ * * Developed by Ondrej Jombik * and Lubomir Host * Copyright (c) 2000-2003 Platon SDG, http://platon.sk/ * All rights reserved. * * See README file for more information about this software. * See COPYING file for license information. * * Download the latest version from * http://platon.sk/projects/ep/ */ /* $Platon: ep/src/process.c,v 1.21 2003/05/03 09:58:19 nepto Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #ifdef STDC_HEADERS # include # include # include #endif #if HAVE_UNISTD_H # include # include #endif #include #include "platon-str.h" #include "process.h" #include "message.h" /* msg_*_print() */ #include "conf.h" /* cfg */ void process_init(proc) struct process *proc; { register int k; for (k = 0; k < N_FD; k++) { proc->fd[k].n_link = 0; proc->fd[k].link = NULL; proc->fd[k].pipe[0] = proc->fd[k].pipe[1] = 0; } proc->pid = 0; proc->argv = NULL; proc->name = NULL; } void process_destroy(proc) struct process *proc; { register int k; #if DEBUG msg_debug_print("process_destroy(): destroyed process = %p\n", proc); #endif for (k = 0; k < N_FD; k++) { #if 0 && DEBUG msg_debug_print("process_destroy(): proc->fd[%d] = %p\n", k, proc->fd[k].link); #endif if (proc->fd[k].n_link > 0 && ! cfg.quiet) msg_warn_print("process_destroy(): removing process %p with %d" " active links in fd %d\n", proc, proc->fd[k].n_link, k); if (proc->fd[k].link != NULL) free(proc->fd[k].link); proc->fd[k].link = NULL; proc->fd[k].n_link = 0; } PLATON_FUNC(strdyn_safe_free)(proc->argv); proc->argv = NULL; PLATON_FUNC(strdyn_safe_free)(proc->name); proc->name = NULL; } int process_argv_init_by_array(proc, size, array) struct process *proc; int size; char **array; { register int i; #if DEBUG msg_debug_print("process_argv_init_by_array(): size = %d\n", size); #endif if (proc == NULL || array == NULL || size == 0) return 0; proc->argv = (char**)malloc((size + 1) * sizeof(char*)); if (proc->argv == NULL) { return 0; } proc->argv[size] = NULL; for (i = 0; i < size; i++) { proc->argv[i] = strdup(array[i]); if (proc->argv[i] == NULL) { PLATON_FUNC(strdyn_free)(proc->argv); proc->argv = NULL; return 0; } } return 1; } void process_print_cmdline(proc) struct process *proc; { register int j = 0; if (proc == NULL) { msg_warn_print("NULL struct process pointer in " "process_print_cmdline()\n"); return; } while (proc->argv != NULL && proc->argv[j] != NULL) { msg_print(proc->argv[j]); msg_print(" "); j++; } } void process_print_names(proc) struct process *proc; { register int j = 0; if (proc == NULL) { msg_warn_print("NULL struct process pointer in " "process_print_names()\n"); return; } while (proc->name != NULL && proc->name[j] != NULL) { msg_print(proc->name[j]); if (proc->name[j + 1] != NULL) msg_print(", "); j++; } } void process_print_struct(proc) struct process *proc; { register int i; struct stat buf; if (proc == NULL) { msg_warn_print("NULL struct process pointer in " "process_print_struct()\n"); return; } process_print_cmdline(proc); msg_print("\n\tPID = %d", proc->pid); for (i = 0; i < N_FD; i++) msg_print("\n\tfd[%d].n_link = %d",i, proc->fd[i].n_link); for (i = 0; i < N_FD; i++) msg_print("\n\tfd[%d].pipe = (%d, %d)",i, fstat(proc->fd[i].pipe[0], &buf), fstat(proc->fd[i].pipe[1], &buf)); } void process_close_2nd_sides_of_pipes(proc) struct process *proc; { register int k; for (k = 0; k < N_FD; k++) if (1 || proc->fd[k].n_link != 0) /* Closing the right descriptor according to the fd number. */ close(proc->fd[k].pipe[k != 0 ? 1 : 0]); }