1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* fork.c - Provide fork and waitpid functions for gawk.
*
* Revised 6/2004
* Revised 5/2012 for new extension API.
*/
/*
* Copyright (C) 2001, 2004, 2011, 2012, 2013 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gawkapi.h"
#include "gettext.h"
#define _(msgid) gettext(msgid)
#define N_(msgid) msgid
static const gawk_api_t *api; /* for convenience macros to work */
static awk_ext_id_t *ext_id;
static const char *ext_version = "fork extension: version 1.0";
static awk_bool_t (*init_func)(void) = NULL;
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_value_t index, value;
set_array_element(array,
make_const_string(sub, strlen(sub), & index),
make_number(num, & value));
}
/* do_fork --- provide dynamically loaded fork() builtin for gawk */
static awk_value_t *
do_fork(int nargs, awk_value_t *result)
{
int ret = -1;
assert(result != NULL);
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", AWK_ARRAY, & procinfo)) {
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;
assert(result != NULL);
if (do_lint && nargs > 1)
lintwarn(ext_id, _("waitpid: called with too many arguments"));
if (get_argument(0, AWK_NUMBER, &pid)) {
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;
assert(result != NULL);
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, "")
|