/* * fork.c - Provide fork and waitpid functions for gawk. * * Revised 6/2004 */ /* * Copyright (C) 2001, 2004, 2011 the Free Software Foundation, Inc. * * This file is part of GAWK, the GNU implementation of the * AWK Programming Language. * * GAWK is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GAWK is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include #include #include #include #include "config.h" #include "gawkapi.h" static const gawk_api_t *api; /* for convenience macros to work */ static awk_ext_id_t *ext_id; int plugin_is_GPL_compatible; /* array_set --- set an array element */ static void array_set_numeric(awk_array_t array, const char *sub, double num) { awk_element_t element; awk_value_t tmp; memset(& element, 0, sizeof(element)); element.index = dup_string(sub, strlen(sub), & tmp)->str_value; make_number(num, &element.value); set_array_element(array, & element); } /* do_fork --- provide dynamically loaded fork() builtin for gawk */ static awk_value_t * do_fork(int nargs, awk_value_t *result) { int ret = -1; if (do_lint && nargs > 0) lintwarn(ext_id, "fork: called with too many arguments"); ret = fork(); if (ret < 0) update_ERRNO_int(errno); else if (ret == 0) { /* update PROCINFO in the child, if the array exists */ awk_value_t procinfo; if (sym_lookup("PROCINFO", &procinfo) != NULL) { if (procinfo.val_type != AWK_ARRAY) { if (do_lint) lintwarn(ext_id, "fork: PROCINFO is not an array!"); } else { array_set_numeric(procinfo.array_cookie, "pid", getpid()); array_set_numeric(procinfo.array_cookie, "ppid", getppid()); } } } /* Set the return value */ return make_number(ret, result); } /* do_waitpid --- provide dynamically loaded waitpid() builtin for gawk */ static awk_value_t * do_waitpid(int nargs, awk_value_t *result) { awk_value_t pid; int ret = -1; int options = 0; if (do_lint && nargs > 1) lintwarn(ext_id, "waitpid: called with too many arguments"); if (get_curfunc_param(0, AWK_NUMBER, &pid) != NULL) { options = WNOHANG|WUNTRACED; ret = waitpid(pid.num_value, NULL, options); if (ret < 0) update_ERRNO_int(errno); } else if (do_lint) lintwarn(ext_id, "wait: called with no arguments"); /* Set the return value */ return make_number(ret, result); } /* do_wait --- provide dynamically loaded wait() builtin for gawk */ static awk_value_t * do_wait(int nargs, awk_value_t *result) { int ret; if (do_lint && nargs > 0) lintwarn(ext_id, "wait: called with too many arguments"); ret = wait(NULL); if (ret < 0) update_ERRNO_int(errno); /* Set the return value */ return make_number(ret, result); } static awk_ext_func_t func_table[] = { { "fork", do_fork, 0 }, { "waitpid", do_waitpid, 1 }, { "wait", do_wait, 0 }, }; /* define the dl_load function using the boilerplate macro */ dl_load_func(func_table, fork, "")