| 1 | /* $Id: shinstance.c 2292 2009-02-28 04:46:25Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * The shell instance methods.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | *
|
|---|
| 10 | * This file is part of kBuild.
|
|---|
| 11 | *
|
|---|
| 12 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 13 | * it under the terms of the GNU General Public License as published by
|
|---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 15 | * (at your option) any later version.
|
|---|
| 16 | *
|
|---|
| 17 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 20 | * GNU General Public License for more details.
|
|---|
| 21 | *
|
|---|
| 22 | * You should have received a copy of the GNU General Public License
|
|---|
| 23 | * along with kBuild; if not, write to the Free Software
|
|---|
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | /*******************************************************************************
|
|---|
| 29 | * Header Files *
|
|---|
| 30 | *******************************************************************************/
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <assert.h>
|
|---|
| 34 | #ifdef _MSC_VER
|
|---|
| 35 | # include <process.h>
|
|---|
| 36 | #else
|
|---|
| 37 | # include <unistd.h>
|
|---|
| 38 | # include <pwd.h>
|
|---|
| 39 | #endif
|
|---|
| 40 | #include "shinstance.h"
|
|---|
| 41 |
|
|---|
| 42 | #if K_OS == K_OS_WINDOWS
|
|---|
| 43 | extern pid_t shfork_do_it(void); /* shforkA-win.asm */
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | /*******************************************************************************
|
|---|
| 48 | * Global Variables *
|
|---|
| 49 | *******************************************************************************/
|
|---|
| 50 | /** The mutex protecting the the globals and some shell instance members (sigs). */
|
|---|
| 51 | static shmtx g_sh_mtx;
|
|---|
| 52 | /** The root shell instance. */
|
|---|
| 53 | static shinstance *g_sh_root;
|
|---|
| 54 | /** The first shell instance. */
|
|---|
| 55 | static shinstance *g_sh_head;
|
|---|
| 56 | /** The last shell instance. */
|
|---|
| 57 | static shinstance *g_sh_tail;
|
|---|
| 58 | /** The number of shells. */
|
|---|
| 59 | static int volatile g_num_shells;
|
|---|
| 60 | /** Per signal state for determining a common denominator.
|
|---|
| 61 | * @remarks defaults and unmasked actions aren't counted. */
|
|---|
| 62 | struct shsigstate
|
|---|
| 63 | {
|
|---|
| 64 | /** The current signal action. */
|
|---|
| 65 | #ifndef _MSC_VER
|
|---|
| 66 | struct sigaction sa;
|
|---|
| 67 | #else
|
|---|
| 68 | struct
|
|---|
| 69 | {
|
|---|
| 70 | void (*sa_handler)(int);
|
|---|
| 71 | int sa_flags;
|
|---|
| 72 | shsigset_t sa_mask;
|
|---|
| 73 | } sa;
|
|---|
| 74 | #endif
|
|---|
| 75 | /** The number of restarts (siginterrupt / SA_RESTART). */
|
|---|
| 76 | int num_restart;
|
|---|
| 77 | /** The number of ignore handlers. */
|
|---|
| 78 | int num_ignore;
|
|---|
| 79 | /** The number of specific handlers. */
|
|---|
| 80 | int num_specific;
|
|---|
| 81 | /** The number of threads masking it. */
|
|---|
| 82 | int num_masked;
|
|---|
| 83 | } g_sig_state[NSIG];
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | int shmtx_init(shmtx *pmtx)
|
|---|
| 88 | {
|
|---|
| 89 | pmtx->b[0] = 0;
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void shmtx_delete(shmtx *pmtx)
|
|---|
| 94 | {
|
|---|
| 95 | pmtx->b[0] = 0;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void shmtx_enter(shmtx *pmtx, shmtxtmp *ptmp)
|
|---|
| 99 | {
|
|---|
| 100 | pmtx->b[0] = 0;
|
|---|
| 101 | ptmp->i = 0;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void shmtx_leave(shmtx *pmtx, shmtxtmp *ptmp)
|
|---|
| 105 | {
|
|---|
| 106 | pmtx->b[0] = 0;
|
|---|
| 107 | ptmp->i = 432;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Links the shell instance.
|
|---|
| 112 | *
|
|---|
| 113 | * @param psh The shell.
|
|---|
| 114 | */
|
|---|
| 115 | static void sh_int_link(shinstance *psh)
|
|---|
| 116 | {
|
|---|
| 117 | shmtxtmp tmp;
|
|---|
| 118 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 119 |
|
|---|
| 120 | if (psh->rootshell)
|
|---|
| 121 | g_sh_root = psh;
|
|---|
| 122 |
|
|---|
| 123 | psh->next = NULL;
|
|---|
| 124 | psh->prev = g_sh_tail;
|
|---|
| 125 | if (g_sh_tail)
|
|---|
| 126 | g_sh_tail->next = psh;
|
|---|
| 127 | else
|
|---|
| 128 | g_sh_tail = g_sh_head = psh;
|
|---|
| 129 | g_sh_tail = psh;
|
|---|
| 130 |
|
|---|
| 131 | g_num_shells++;
|
|---|
| 132 |
|
|---|
| 133 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | #if 0
|
|---|
| 137 | /**
|
|---|
| 138 | * Unlink the shell instance.
|
|---|
| 139 | *
|
|---|
| 140 | * @param psh The shell.
|
|---|
| 141 | */
|
|---|
| 142 | static void sh_int_unlink(shinstance *psh)
|
|---|
| 143 | {
|
|---|
| 144 | shmtxtmp tmp;
|
|---|
| 145 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 146 |
|
|---|
| 147 | g_num_shells--;
|
|---|
| 148 |
|
|---|
| 149 | if (g_sh_tail == psh)
|
|---|
| 150 | g_sh_tail = psh->prev;
|
|---|
| 151 | else
|
|---|
| 152 | psh->next->prev = psh->prev;
|
|---|
| 153 |
|
|---|
| 154 | if (g_sh_head == psh)
|
|---|
| 155 | g_sh_head = psh->next;
|
|---|
| 156 | else
|
|---|
| 157 | psh->prev->next = psh->next;
|
|---|
| 158 |
|
|---|
| 159 | if (g_sh_root == psh)
|
|---|
| 160 | g_sh_root = 0;
|
|---|
| 161 |
|
|---|
| 162 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 163 | }
|
|---|
| 164 | #endif
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Destroys the shell instance.
|
|---|
| 168 | *
|
|---|
| 169 | * This will work on partially initialized instances (because I'm lazy).
|
|---|
| 170 | *
|
|---|
| 171 | * @param psh The shell instance to be destroyed.
|
|---|
| 172 | */
|
|---|
| 173 | static void sh_destroy(shinstance *psh)
|
|---|
| 174 | {
|
|---|
| 175 | memset(psh, 0, sizeof(*psh));
|
|---|
| 176 | sh_free(NULL, psh);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * Clones an environment vector.
|
|---|
| 181 | *
|
|---|
| 182 | * @returns 0 on success, -1 and errno on failure.
|
|---|
| 183 | * @param dstp Where to store the clone.
|
|---|
| 184 | * @param src The vector to be cloned.
|
|---|
| 185 | */
|
|---|
| 186 | static int sh_env_clone(char ***dstp, char **src)
|
|---|
| 187 | {
|
|---|
| 188 | char **dst;
|
|---|
| 189 | size_t items;
|
|---|
| 190 |
|
|---|
| 191 | /* count first */
|
|---|
| 192 | items = 0;
|
|---|
| 193 | while (src[items])
|
|---|
| 194 | items++;
|
|---|
| 195 |
|
|---|
| 196 | /* alloc clone array. */
|
|---|
| 197 | *dstp = dst = sh_malloc(NULL, sizeof(*dst) * items + 1);
|
|---|
| 198 | if (!dst)
|
|---|
| 199 | return -1;
|
|---|
| 200 |
|
|---|
| 201 | /* copy the items */
|
|---|
| 202 | dst[items] = NULL;
|
|---|
| 203 | while (items-- > 0)
|
|---|
| 204 | {
|
|---|
| 205 | dst[items] = sh_strdup(NULL, src[items]);
|
|---|
| 206 | if (!dst[items])
|
|---|
| 207 | {
|
|---|
| 208 | /* allocation error, clean up. */
|
|---|
| 209 | while (dst[++items])
|
|---|
| 210 | sh_free(NULL, dst[items]);
|
|---|
| 211 | sh_free(NULL, dst);
|
|---|
| 212 | errno = ENOMEM;
|
|---|
| 213 | return -1;
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | return 0;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Creates a root shell instance.
|
|---|
| 222 | *
|
|---|
| 223 | * @param inherit The shell to inherit from. If NULL inherit from environment and such.
|
|---|
| 224 | * @param argc The argument count.
|
|---|
| 225 | * @param argv The argument vector.
|
|---|
| 226 | * @param envp The environment vector.
|
|---|
| 227 | *
|
|---|
| 228 | * @returns pointer to root shell on success, NULL on failure.
|
|---|
| 229 | */
|
|---|
| 230 | shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv, char **envp)
|
|---|
| 231 | {
|
|---|
| 232 | shinstance *psh;
|
|---|
| 233 | int i;
|
|---|
| 234 |
|
|---|
| 235 | /*
|
|---|
| 236 | * The allocations.
|
|---|
| 237 | */
|
|---|
| 238 | psh = sh_calloc(NULL, sizeof(*psh), 1);
|
|---|
| 239 | if (psh)
|
|---|
| 240 | {
|
|---|
| 241 | /* Init it enought for sh_destroy() to not get upset */
|
|---|
| 242 | /* ... */
|
|---|
| 243 |
|
|---|
| 244 | /* Call the basic initializers. */
|
|---|
| 245 | if ( !sh_env_clone(&psh->shenviron, envp)
|
|---|
| 246 | && !shfile_init(&psh->fdtab, inherit ? &inherit->fdtab : NULL))
|
|---|
| 247 | {
|
|---|
| 248 | /* the special stuff. */
|
|---|
| 249 | #ifdef _MSC_VER
|
|---|
| 250 | psh->pid = _getpid();
|
|---|
| 251 | #else
|
|---|
| 252 | psh->pid = getpid();
|
|---|
| 253 | #endif
|
|---|
| 254 | /*sh_sigemptyset(&psh->sigrestartset);*/
|
|---|
| 255 | for (i = 0; i < NSIG; i++)
|
|---|
| 256 | psh->sigactions[i].sh_handler = SH_SIG_UNK;
|
|---|
| 257 | if (inherit)
|
|---|
| 258 | psh->sigmask = psh->sigmask;
|
|---|
| 259 | else
|
|---|
| 260 | {
|
|---|
| 261 | #if defined(SH_PURE_STUB_MODE) || defined(_MSC_VER)
|
|---|
| 262 | sh_sigemptyset(&psh->sigmask);
|
|---|
| 263 | #else
|
|---|
| 264 | sigprocmask(SIG_SETMASK, NULL, &psh->sigmask);
|
|---|
| 265 | #endif
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /* memalloc.c */
|
|---|
| 269 | psh->stacknleft = MINSIZE;
|
|---|
| 270 | psh->herefd = -1;
|
|---|
| 271 | psh->stackp = &psh->stackbase;
|
|---|
| 272 | psh->stacknxt = psh->stackbase.space;
|
|---|
| 273 |
|
|---|
| 274 | /* input.c */
|
|---|
| 275 | psh->plinno = 1;
|
|---|
| 276 | psh->init_editline = 0;
|
|---|
| 277 | psh->parsefile = &psh->basepf;
|
|---|
| 278 |
|
|---|
| 279 | /* output.c */
|
|---|
| 280 | psh->output.bufsize = OUTBUFSIZ;
|
|---|
| 281 | psh->output.fd = 1;
|
|---|
| 282 | psh->output.psh = psh;
|
|---|
| 283 | psh->errout.bufsize = 100;
|
|---|
| 284 | psh->errout.fd = 2;
|
|---|
| 285 | psh->errout.psh = psh;
|
|---|
| 286 | psh->memout.fd = MEM_OUT;
|
|---|
| 287 | psh->memout.psh = psh;
|
|---|
| 288 | psh->out1 = &psh->output;
|
|---|
| 289 | psh->out2 = &psh->errout;
|
|---|
| 290 |
|
|---|
| 291 | /* jobs.c */
|
|---|
| 292 | psh->backgndpid = -1;
|
|---|
| 293 | #if JOBS
|
|---|
| 294 | psh->curjob = -1;
|
|---|
| 295 | #else
|
|---|
| 296 | # error asdf
|
|---|
| 297 | #endif
|
|---|
| 298 | psh->ttyfd = -1;
|
|---|
| 299 |
|
|---|
| 300 | /* link it. */
|
|---|
| 301 | sh_int_link(psh);
|
|---|
| 302 | return psh;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | sh_destroy(psh);
|
|---|
| 306 | }
|
|---|
| 307 | return NULL;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | char *sh_getenv(shinstance *psh, const char *var)
|
|---|
| 311 | {
|
|---|
| 312 | size_t len;
|
|---|
| 313 | int i = 0;
|
|---|
| 314 |
|
|---|
| 315 | if (!var)
|
|---|
| 316 | return NULL;
|
|---|
| 317 |
|
|---|
| 318 | len = strlen(var);
|
|---|
| 319 | i = 0;
|
|---|
| 320 | while (psh->shenviron[i])
|
|---|
| 321 | {
|
|---|
| 322 | const char *item = psh->shenviron[i];
|
|---|
| 323 | if ( !strncmp(item, var, len)
|
|---|
| 324 | && item[len] == '=')
|
|---|
| 325 | return (char *)item + len + 1;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | return NULL;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | char **sh_environ(shinstance *psh)
|
|---|
| 332 | {
|
|---|
| 333 | return psh->shenviron;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | const char *sh_gethomedir(shinstance *psh, const char *user)
|
|---|
| 337 | {
|
|---|
| 338 | const char *ret = NULL;
|
|---|
| 339 |
|
|---|
| 340 | #ifdef _MSC_VER
|
|---|
| 341 | ret = sh_getenv(psh, "HOME");
|
|---|
| 342 | if (!ret)
|
|---|
| 343 | ret = sh_getenv(psh, "USERPROFILE");
|
|---|
| 344 | #else
|
|---|
| 345 | struct passwd *pwd = getpwnam(user); /** @todo use getpwdnam_r */
|
|---|
| 346 | (void)psh;
|
|---|
| 347 | ret = pwd ? pwd->pw_dir : NULL;
|
|---|
| 348 | #endif
|
|---|
| 349 |
|
|---|
| 350 | return ret;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /**
|
|---|
| 354 | * Lazy initialization of a signal state, globally.
|
|---|
| 355 | *
|
|---|
| 356 | * @param psh The shell doing the lazy work.
|
|---|
| 357 | * @param signo The signal (valid).
|
|---|
| 358 | */
|
|---|
| 359 | static void sh_int_lazy_init_sigaction(shinstance *psh, int signo)
|
|---|
| 360 | {
|
|---|
| 361 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
|---|
| 362 | {
|
|---|
| 363 | shmtxtmp tmp;
|
|---|
| 364 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 365 |
|
|---|
| 366 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
|---|
| 367 | {
|
|---|
| 368 | shsigaction_t shold;
|
|---|
| 369 | shinstance *cur;
|
|---|
| 370 | #ifndef _MSC_VER
|
|---|
| 371 | struct sigaction old;
|
|---|
| 372 | if (!sigaction(signo, NULL, &old))
|
|---|
| 373 | {
|
|---|
| 374 | /* convert */
|
|---|
| 375 | shold.sh_flags = old.sa_flags;
|
|---|
| 376 | shold.sh_mask = old.sa_mask;
|
|---|
| 377 | if (old.sa_handler == SIG_DFL)
|
|---|
| 378 | shold.sh_handler = SH_SIG_DFL;
|
|---|
| 379 | else
|
|---|
| 380 | {
|
|---|
| 381 | assert(old.sa_handler == SIG_IGN);
|
|---|
| 382 | shold.sh_handler = SH_SIG_IGN;
|
|---|
| 383 | }
|
|---|
| 384 | }
|
|---|
| 385 | else
|
|---|
| 386 | #endif
|
|---|
| 387 | {
|
|---|
| 388 | /* fake */
|
|---|
| 389 | #ifndef _MSC_VER
|
|---|
| 390 | assert(0);
|
|---|
| 391 | old.sa_handler = SIG_DFL;
|
|---|
| 392 | old.sa_flags = 0;
|
|---|
| 393 | sigemptyset(&shold.sh_mask);
|
|---|
| 394 | sigaddset(&shold.sh_mask, signo);
|
|---|
| 395 | #endif
|
|---|
| 396 | shold.sh_flags = 0;
|
|---|
| 397 | sh_sigemptyset(&shold.sh_mask);
|
|---|
| 398 | sh_sigaddset(&shold.sh_mask, signo);
|
|---|
| 399 | shold.sh_handler = SH_SIG_DFL;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /* update globals */
|
|---|
| 403 | #ifndef _MSC_VER
|
|---|
| 404 | g_sig_state[signo].sa = old;
|
|---|
| 405 | #else
|
|---|
| 406 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
|---|
| 407 | g_sig_state[signo].sa.sa_flags = 0;
|
|---|
| 408 | g_sig_state[signo].sa.sa_mask = shold.sh_mask;
|
|---|
| 409 | #endif
|
|---|
| 410 | TRACE2((psh, "sh_int_lazy_init_sigaction: signo=%d:%s sa_handler=%p sa_flags=%#x\n",
|
|---|
| 411 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
|---|
| 412 |
|
|---|
| 413 | /* update all shells */
|
|---|
| 414 | for (cur = g_sh_head; cur; cur = cur->next)
|
|---|
| 415 | {
|
|---|
| 416 | assert(cur->sigactions[signo].sh_handler == SH_SIG_UNK);
|
|---|
| 417 | cur->sigactions[signo] = shold;
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 422 | }
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | /**
|
|---|
| 426 | * Perform the default signal action on the shell.
|
|---|
| 427 | *
|
|---|
| 428 | * @param psh The shell instance.
|
|---|
| 429 | * @param signo The signal.
|
|---|
| 430 | */
|
|---|
| 431 | static void sh_sig_do_default(shinstance *psh, int signo)
|
|---|
| 432 | {
|
|---|
| 433 | /** @todo */
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | /**
|
|---|
| 437 | * Deliver a signal to a shell.
|
|---|
| 438 | *
|
|---|
| 439 | * @param psh The shell instance.
|
|---|
| 440 | * @param pshDst The shell instance to signal.
|
|---|
| 441 | * @param signo The signal.
|
|---|
| 442 | * @param locked Whether we're owning the lock or not.
|
|---|
| 443 | */
|
|---|
| 444 | static void sh_sig_do_signal(shinstance *psh, shinstance *pshDst, int signo, int locked)
|
|---|
| 445 | {
|
|---|
| 446 | shsig_t pfn = pshDst->sigactions[signo].sh_handler;
|
|---|
| 447 | if (pfn == SH_SIG_UNK)
|
|---|
| 448 | {
|
|---|
| 449 | sh_int_lazy_init_sigaction(pshDst, signo);
|
|---|
| 450 | pfn = pshDst->sigactions[signo].sh_handler;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | if (pfn == SH_SIG_DFL)
|
|---|
| 454 | sh_sig_do_default(pshDst, signo);
|
|---|
| 455 | else if (pfn == SH_SIG_IGN)
|
|---|
| 456 | /* ignore it */;
|
|---|
| 457 | else
|
|---|
| 458 | {
|
|---|
| 459 | assert(pfn != SH_SIG_ERR);
|
|---|
| 460 | pfn(pshDst, signo);
|
|---|
| 461 | }
|
|---|
| 462 | (void)locked;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | /**
|
|---|
| 466 | * Handler for external signals.
|
|---|
| 467 | *
|
|---|
| 468 | * @param signo The signal.
|
|---|
| 469 | */
|
|---|
| 470 | static void sh_sig_common_handler(int signo)
|
|---|
| 471 | {
|
|---|
| 472 | shinstance *psh;
|
|---|
| 473 |
|
|---|
| 474 | fprintf(stderr, "sh_sig_common_handler: signo=%d:%s\n", signo, sys_signame[signo]);
|
|---|
| 475 |
|
|---|
| 476 | /*
|
|---|
| 477 | * No need to take locks if there is only one shell.
|
|---|
| 478 | * Since this will be the initial case, just avoid the deadlock
|
|---|
| 479 | * hell for a litte while...
|
|---|
| 480 | */
|
|---|
| 481 | if (g_num_shells <= 1)
|
|---|
| 482 | {
|
|---|
| 483 | psh = g_sh_head;
|
|---|
| 484 | if (psh)
|
|---|
| 485 | sh_sig_do_signal(NULL, psh, signo, 0 /* no lock */);
|
|---|
| 486 | }
|
|---|
| 487 | else
|
|---|
| 488 | {
|
|---|
| 489 | shmtxtmp tmp;
|
|---|
| 490 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 491 |
|
|---|
| 492 | /** @todo signal focus chain or something? Atm there will only be one shell,
|
|---|
| 493 | * so it's not really important until we go threaded for real... */
|
|---|
| 494 | psh = g_sh_tail;
|
|---|
| 495 | while (psh != NULL)
|
|---|
| 496 | {
|
|---|
| 497 | sh_sig_do_signal(NULL, psh, signo, 1 /* locked */);
|
|---|
| 498 | psh = psh->prev;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 502 | }
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
|
|---|
| 506 | {
|
|---|
| 507 | if (newp)
|
|---|
| 508 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=%p:{.sh_handler=%p, .sh_flags=%#x} oldp=%p\n",
|
|---|
| 509 | signo, sys_signame[signo], newp, newp->sh_handler, newp->sh_flags, oldp));
|
|---|
| 510 | else
|
|---|
| 511 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=NULL oldp=%p\n", signo, sys_signame[signo], oldp));
|
|---|
| 512 |
|
|---|
| 513 | /*
|
|---|
| 514 | * Input validation.
|
|---|
| 515 | */
|
|---|
| 516 | if (signo >= NSIG || signo <= 0)
|
|---|
| 517 | {
|
|---|
| 518 | errno = EINVAL;
|
|---|
| 519 | return -1;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 523 | return -1;
|
|---|
| 524 | #else
|
|---|
| 525 |
|
|---|
| 526 | /*
|
|---|
| 527 | * Make sure our data is correct.
|
|---|
| 528 | */
|
|---|
| 529 | sh_int_lazy_init_sigaction(psh, signo);
|
|---|
| 530 |
|
|---|
| 531 | /*
|
|---|
| 532 | * Get the old one if requested.
|
|---|
| 533 | */
|
|---|
| 534 | if (oldp)
|
|---|
| 535 | *oldp = psh->sigactions[signo];
|
|---|
| 536 |
|
|---|
| 537 | /*
|
|---|
| 538 | * Set the new one if it has changed.
|
|---|
| 539 | *
|
|---|
| 540 | * This will be attempted coordinated with the other signal handlers so
|
|---|
| 541 | * that we can arrive at a common denominator.
|
|---|
| 542 | */
|
|---|
| 543 | if ( newp
|
|---|
| 544 | && memcmp(&psh->sigactions[signo], newp, sizeof(*newp)))
|
|---|
| 545 | {
|
|---|
| 546 | shmtxtmp tmp;
|
|---|
| 547 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 548 |
|
|---|
| 549 | /* Undo the accounting for the current entry. */
|
|---|
| 550 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
|---|
| 551 | g_sig_state[signo].num_ignore--;
|
|---|
| 552 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
|---|
| 553 | g_sig_state[signo].num_specific--;
|
|---|
| 554 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
|---|
| 555 | g_sig_state[signo].num_restart--;
|
|---|
| 556 |
|
|---|
| 557 | /* Set the new entry. */
|
|---|
| 558 | psh->sigactions[signo] = *newp;
|
|---|
| 559 |
|
|---|
| 560 | /* Add the bits for the new action entry. */
|
|---|
| 561 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
|---|
| 562 | g_sig_state[signo].num_ignore++;
|
|---|
| 563 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
|---|
| 564 | g_sig_state[signo].num_specific++;
|
|---|
| 565 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
|---|
| 566 | g_sig_state[signo].num_restart++;
|
|---|
| 567 |
|
|---|
| 568 | /*
|
|---|
| 569 | * Calc new common action.
|
|---|
| 570 | *
|
|---|
| 571 | * This is quit a bit ASSUMPTIVE about the limited use. We will not
|
|---|
| 572 | * bother synching the mask, and we pretend to care about SA_RESTART.
|
|---|
| 573 | * The only thing we really actually care about is the sh_handler.
|
|---|
| 574 | *
|
|---|
| 575 | * On second though, it's possible we should just tie this to the root
|
|---|
| 576 | * shell since it only really applies to external signal ...
|
|---|
| 577 | */
|
|---|
| 578 | if ( g_sig_state[signo].num_specific
|
|---|
| 579 | || g_sig_state[signo].num_ignore != g_num_shells)
|
|---|
| 580 | g_sig_state[signo].sa.sa_handler = sh_sig_common_handler;
|
|---|
| 581 | else if (g_sig_state[signo].num_ignore)
|
|---|
| 582 | g_sig_state[signo].sa.sa_handler = SIG_IGN;
|
|---|
| 583 | else
|
|---|
| 584 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
|---|
| 585 | g_sig_state[signo].sa.sa_flags = psh->sigactions[signo].sh_flags & SA_RESTART;
|
|---|
| 586 |
|
|---|
| 587 | TRACE2((psh, "sh_sigaction: setting signo=%d:%s to {.sa_handler=%p, .sa_flags=%#x}\n",
|
|---|
| 588 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
|---|
| 589 | # ifdef _MSC_VER
|
|---|
| 590 | if (signal(signo, g_sig_state[signo].sa.sa_handler) == SIG_ERR)
|
|---|
| 591 | # else
|
|---|
| 592 | if (sigaction(signo, &g_sig_state[signo].sa, NULL))
|
|---|
| 593 | # endif
|
|---|
| 594 | assert(0);
|
|---|
| 595 |
|
|---|
| 596 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | return 0;
|
|---|
| 600 | #endif
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
|
|---|
| 604 | {
|
|---|
| 605 | shsigaction_t sa;
|
|---|
| 606 | shsig_t ret;
|
|---|
| 607 |
|
|---|
| 608 | /*
|
|---|
| 609 | * Implementation using sh_sigaction.
|
|---|
| 610 | */
|
|---|
| 611 | if (sh_sigaction(psh, signo, NULL, &sa))
|
|---|
| 612 | return SH_SIG_ERR;
|
|---|
| 613 |
|
|---|
| 614 | ret = sa.sh_handler;
|
|---|
| 615 | sa.sh_flags &= SA_RESTART;
|
|---|
| 616 | sa.sh_handler = handler;
|
|---|
| 617 | sh_sigemptyset(&sa.sh_mask);
|
|---|
| 618 | sh_sigaddset(&sa.sh_mask, signo); /* ?? */
|
|---|
| 619 | if (sh_sigaction(psh, signo, &sa, NULL))
|
|---|
| 620 | return SH_SIG_ERR;
|
|---|
| 621 |
|
|---|
| 622 | return ret;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
|---|
| 626 | {
|
|---|
| 627 | shsigaction_t sa;
|
|---|
| 628 | int oldflags = 0;
|
|---|
| 629 |
|
|---|
| 630 | /*
|
|---|
| 631 | * Implementation using sh_sigaction.
|
|---|
| 632 | */
|
|---|
| 633 | if (sh_sigaction(psh, signo, NULL, &sa))
|
|---|
| 634 | return -1;
|
|---|
| 635 | oldflags = sa.sh_flags;
|
|---|
| 636 | if (interrupt)
|
|---|
| 637 | sa.sh_flags &= ~SA_RESTART;
|
|---|
| 638 | else
|
|---|
| 639 | sa.sh_flags |= ~SA_RESTART;
|
|---|
| 640 | if (!((oldflags ^ sa.sh_flags) & SA_RESTART))
|
|---|
| 641 | return 0; /* unchanged. */
|
|---|
| 642 |
|
|---|
| 643 | return sh_sigaction(psh, signo, &sa, NULL);
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | void sh_sigemptyset(shsigset_t *setp)
|
|---|
| 647 | {
|
|---|
| 648 | memset(setp, 0, sizeof(*setp));
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | void sh_sigfillset(shsigset_t *setp)
|
|---|
| 652 | {
|
|---|
| 653 | memset(setp, 0xff, sizeof(*setp));
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | void sh_sigaddset(shsigset_t *setp, int signo)
|
|---|
| 657 | {
|
|---|
| 658 | #ifdef _MSC_VER
|
|---|
| 659 | *setp |= 1U << signo;
|
|---|
| 660 | #else
|
|---|
| 661 | sigaddset(setp, signo);
|
|---|
| 662 | #endif
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | void sh_sigdelset(shsigset_t *setp, int signo)
|
|---|
| 666 | {
|
|---|
| 667 | #ifdef _MSC_VER
|
|---|
| 668 | *setp &= ~(1U << signo);
|
|---|
| 669 | #else
|
|---|
| 670 | sigdelset(setp, signo);
|
|---|
| 671 | #endif
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | int sh_sigismember(shsigset_t const *setp, int signo)
|
|---|
| 675 | {
|
|---|
| 676 | #ifdef _MSC_VER
|
|---|
| 677 | return !!(*setp & (1U << signo));
|
|---|
| 678 | #else
|
|---|
| 679 | return !!sigismember(setp, signo);
|
|---|
| 680 | #endif
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
|
|---|
| 684 | {
|
|---|
| 685 | int rc;
|
|---|
| 686 |
|
|---|
| 687 | if ( operation != SIG_BLOCK
|
|---|
| 688 | && operation != SIG_UNBLOCK
|
|---|
| 689 | && operation != SIG_SETMASK)
|
|---|
| 690 | {
|
|---|
| 691 | errno = EINVAL;
|
|---|
| 692 | return -1;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | #if (defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)) && !defined(_MSC_VER)
|
|---|
| 696 | rc = sigprocmask(operation, newp, oldp);
|
|---|
| 697 | if (!rc && newp)
|
|---|
| 698 | psh->sigmask = *newp;
|
|---|
| 699 |
|
|---|
| 700 | #else
|
|---|
| 701 | if (oldp)
|
|---|
| 702 | *oldp = psh->sigmask;
|
|---|
| 703 | if (newp)
|
|---|
| 704 | {
|
|---|
| 705 | /* calc the new mask */
|
|---|
| 706 | shsigset_t mask = psh->sigmask;
|
|---|
| 707 | switch (operation)
|
|---|
| 708 | {
|
|---|
| 709 | case SIG_BLOCK:
|
|---|
| 710 | for (rc = 0; rc < NSIG; rc++)
|
|---|
| 711 | if (sh_sigismember(newp, rc))
|
|---|
| 712 | sh_sigaddset(&mask, rc);
|
|---|
| 713 | break;
|
|---|
| 714 | case SIG_UNBLOCK:
|
|---|
| 715 | for (rc = 0; rc < NSIG; rc++)
|
|---|
| 716 | if (sh_sigismember(newp, rc))
|
|---|
| 717 | sh_sigdelset(&mask, rc);
|
|---|
| 718 | break;
|
|---|
| 719 | case SIG_SETMASK:
|
|---|
| 720 | mask = *newp;
|
|---|
| 721 | break;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | # if defined(SH_STUB_MODE) || defined(_MSC_VER)
|
|---|
| 725 | rc = 0;
|
|---|
| 726 | # else
|
|---|
| 727 | rc = sigprocmask(operation, &mask, NULL);
|
|---|
| 728 | if (!rc)
|
|---|
| 729 | # endif
|
|---|
| 730 | psh->sigmask = mask;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | #endif
|
|---|
| 734 | return rc;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | void sh_abort(shinstance *psh)
|
|---|
| 738 | {
|
|---|
| 739 | shsigset_t set;
|
|---|
| 740 | TRACE2((psh, "sh_abort\n"));
|
|---|
| 741 |
|
|---|
| 742 | /* block other async signals */
|
|---|
| 743 | sh_sigfillset(&set);
|
|---|
| 744 | sh_sigdelset(&set, SIGABRT);
|
|---|
| 745 | sh_sigprocmask(psh, SIG_SETMASK, &set, NULL);
|
|---|
| 746 |
|
|---|
| 747 | sh_sig_do_signal(psh, psh, SIGABRT, 0 /* no lock */);
|
|---|
| 748 |
|
|---|
| 749 | /** @todo die in a nicer manner. */
|
|---|
| 750 | *(char *)1 = 3;
|
|---|
| 751 |
|
|---|
| 752 | TRACE2((psh, "sh_abort returns!\n"));
|
|---|
| 753 | (void)psh;
|
|---|
| 754 | abort();
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | void sh_raise_sigint(shinstance *psh)
|
|---|
| 758 | {
|
|---|
| 759 | TRACE2((psh, "sh_raise(SIGINT)\n"));
|
|---|
| 760 |
|
|---|
| 761 | sh_sig_do_signal(psh, psh, SIGINT, 0 /* no lock */);
|
|---|
| 762 |
|
|---|
| 763 | TRACE2((psh, "sh_raise(SIGINT) returns\n"));
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
|---|
| 767 | {
|
|---|
| 768 | shinstance *pshDst;
|
|---|
| 769 | shmtxtmp tmp;
|
|---|
| 770 | int rc;
|
|---|
| 771 |
|
|---|
| 772 | /*
|
|---|
| 773 | * Self or any of the subshells?
|
|---|
| 774 | */
|
|---|
| 775 | shmtx_enter(&g_sh_mtx, &tmp);
|
|---|
| 776 |
|
|---|
| 777 | pshDst = g_sh_tail;
|
|---|
| 778 | while (pshDst != NULL)
|
|---|
| 779 | {
|
|---|
| 780 | if (pshDst->pid == pid)
|
|---|
| 781 | {
|
|---|
| 782 | TRACE2((psh, "sh_kill(%d, %d): pshDst=%p\n", pid, signo, pshDst));
|
|---|
| 783 | sh_sig_do_signal(psh, pshDst, signo, 1 /* locked */);
|
|---|
| 784 |
|
|---|
| 785 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 786 | return 0;
|
|---|
| 787 | }
|
|---|
| 788 | pshDst = pshDst->prev;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | shmtx_leave(&g_sh_mtx, &tmp);
|
|---|
| 792 |
|
|---|
| 793 | /*
|
|---|
| 794 | * Some other process, call kill where possible
|
|---|
| 795 | */
|
|---|
| 796 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 797 | rc = -1;
|
|---|
| 798 |
|
|---|
| 799 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 800 | # ifdef _MSC_VER
|
|---|
| 801 | errno = ENOSYS;
|
|---|
| 802 | rc = -1;
|
|---|
| 803 | # else
|
|---|
| 804 | fprintf(stderr, "kill(%d, %d)\n", pid, signo);
|
|---|
| 805 | rc = kill(pid, signo);
|
|---|
| 806 | # endif
|
|---|
| 807 |
|
|---|
| 808 | #else
|
|---|
| 809 | #endif
|
|---|
| 810 |
|
|---|
| 811 | TRACE2((psh, "sh_kill(%d, %d) -> %d [%d]\n", pid, signo, rc, errno));
|
|---|
| 812 | return rc;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
|---|
| 816 | {
|
|---|
| 817 | int rc;
|
|---|
| 818 |
|
|---|
| 819 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 820 | rc = -1;
|
|---|
| 821 |
|
|---|
| 822 | #elif defined(SH_STUB_MODE)
|
|---|
| 823 | # ifdef _MSC_VER
|
|---|
| 824 | errno = ENOSYS;
|
|---|
| 825 | rc = -1;
|
|---|
| 826 | # else
|
|---|
| 827 | //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
|
|---|
| 828 | rc = killpg(pgid, signo);
|
|---|
| 829 | # endif
|
|---|
| 830 |
|
|---|
| 831 | #else
|
|---|
| 832 | #endif
|
|---|
| 833 |
|
|---|
| 834 | TRACE2((psh, "sh_killpg(%d, %d) -> %d [%d]\n", pgid, signo, rc, errno));
|
|---|
| 835 | (void)psh;
|
|---|
| 836 | return rc;
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
|---|
| 840 | {
|
|---|
| 841 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 842 | return 0;
|
|---|
| 843 |
|
|---|
| 844 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 845 | (void)psh;
|
|---|
| 846 | # ifdef _MSC_VER
|
|---|
| 847 | errno = ENOSYS;
|
|---|
| 848 | return (clock_t)-1;
|
|---|
| 849 | # else
|
|---|
| 850 | return times(tmsp);
|
|---|
| 851 | # endif
|
|---|
| 852 |
|
|---|
| 853 | #else
|
|---|
| 854 | #endif
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | int sh_sysconf_clk_tck(void)
|
|---|
| 858 | {
|
|---|
| 859 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 860 | return 1;
|
|---|
| 861 | #else
|
|---|
| 862 | # ifdef _MSC_VER
|
|---|
| 863 | return CLK_TCK;
|
|---|
| 864 | # else
|
|---|
| 865 | return sysconf(_SC_CLK_TCK);
|
|---|
| 866 | # endif
|
|---|
| 867 | #endif
|
|---|
| 868 | }
|
|---|
| 869 |
|
|---|
| 870 | pid_t sh_fork(shinstance *psh)
|
|---|
| 871 | {
|
|---|
| 872 | pid_t pid;
|
|---|
| 873 | TRACE2((psh, "sh_fork\n"));
|
|---|
| 874 |
|
|---|
| 875 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 876 | pid = -1;
|
|---|
| 877 |
|
|---|
| 878 | #elif K_OS == K_OS_WINDOWS //&& defined(SH_FORKED_MODE)
|
|---|
| 879 | pid = shfork_do_it();
|
|---|
| 880 |
|
|---|
| 881 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 882 | # ifdef _MSC_VER
|
|---|
| 883 | pid = -1;
|
|---|
| 884 | errno = ENOSYS;
|
|---|
| 885 | # else
|
|---|
| 886 | pid = fork();
|
|---|
| 887 | # endif
|
|---|
| 888 |
|
|---|
| 889 | #else
|
|---|
| 890 |
|
|---|
| 891 | #endif
|
|---|
| 892 |
|
|---|
| 893 | TRACE2((psh, "sh_fork -> %d [%d]\n", pid, errno));
|
|---|
| 894 | (void)psh;
|
|---|
| 895 | return pid;
|
|---|
| 896 | }
|
|---|
| 897 |
|
|---|
| 898 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
|---|
| 899 | {
|
|---|
| 900 | pid_t pidret;
|
|---|
| 901 |
|
|---|
| 902 | *statusp = 0;
|
|---|
| 903 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 904 | pidret = -1;
|
|---|
| 905 |
|
|---|
| 906 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 907 | # ifdef _MSC_VER
|
|---|
| 908 | pidret = -1;
|
|---|
| 909 | errno = ENOSYS;
|
|---|
| 910 | # else
|
|---|
| 911 | pidret = waitpid(pid, statusp, flags);
|
|---|
| 912 | # endif
|
|---|
| 913 |
|
|---|
| 914 | #else
|
|---|
| 915 | #endif
|
|---|
| 916 |
|
|---|
| 917 | TRACE2((psh, "waitpid(%d, %p, %#x) -> %d [%d] *statusp=%#x (rc=%d)\n", pid, statusp, flags,
|
|---|
| 918 | pidret, errno, *statusp, WEXITSTATUS(*statusp)));
|
|---|
| 919 | (void)psh;
|
|---|
| 920 | return pidret;
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | void sh__exit(shinstance *psh, int rc)
|
|---|
| 924 | {
|
|---|
| 925 | TRACE2((psh, "sh__exit(%d)\n", rc));
|
|---|
| 926 | (void)psh;
|
|---|
| 927 |
|
|---|
| 928 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 929 | return -1;
|
|---|
| 930 |
|
|---|
| 931 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 932 | _exit(rc);
|
|---|
| 933 |
|
|---|
| 934 | #else
|
|---|
| 935 | #endif
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
|---|
| 939 | {
|
|---|
| 940 | #ifdef _MSC_VER
|
|---|
| 941 | intptr_t rc;
|
|---|
| 942 | #else
|
|---|
| 943 | int rc;
|
|---|
| 944 | #endif
|
|---|
| 945 |
|
|---|
| 946 | #ifdef DEBUG
|
|---|
| 947 | /* log it all */
|
|---|
| 948 | TRACE2((psh, "sh_execve(%p:{%s}, %p, %p}\n", exe, exe, argv, envp));
|
|---|
| 949 | for (rc = 0; argv[rc]; rc++)
|
|---|
| 950 | TRACE2((psh, " argv[%d]=%p:{%s}\n", rc, argv[rc], argv[rc]));
|
|---|
| 951 | #endif
|
|---|
| 952 |
|
|---|
| 953 | if (!envp)
|
|---|
| 954 | envp = sh_environ(psh);
|
|---|
| 955 |
|
|---|
| 956 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 957 | rc = -1;
|
|---|
| 958 |
|
|---|
| 959 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 960 | # ifdef _MSC_VER
|
|---|
| 961 | rc = _spawnve(_P_WAIT, exe, (char **)argv, (char **)envp);
|
|---|
| 962 | # else
|
|---|
| 963 | rc = execve(exe, (char **)argv, (char **)envp);
|
|---|
| 964 | # endif
|
|---|
| 965 |
|
|---|
| 966 | #else
|
|---|
| 967 | #endif
|
|---|
| 968 |
|
|---|
| 969 | TRACE2((psh, "sh_execve -> %d [%d]\n", rc, errno));
|
|---|
| 970 | (void)psh;
|
|---|
| 971 | return (int)rc;
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | uid_t sh_getuid(shinstance *psh)
|
|---|
| 975 | {
|
|---|
| 976 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 977 | uid_t uid = 0;
|
|---|
| 978 |
|
|---|
| 979 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 980 | # ifdef _MSC_VER
|
|---|
| 981 | uid_t uid = 0;
|
|---|
| 982 | # else
|
|---|
| 983 | uid_t uid = getuid();
|
|---|
| 984 | # endif
|
|---|
| 985 |
|
|---|
| 986 | #else
|
|---|
| 987 | #endif
|
|---|
| 988 |
|
|---|
| 989 | TRACE2((psh, "sh_getuid() -> %d [%d]\n", uid, errno));
|
|---|
| 990 | (void)psh;
|
|---|
| 991 | return uid;
|
|---|
| 992 | }
|
|---|
| 993 |
|
|---|
| 994 | uid_t sh_geteuid(shinstance *psh)
|
|---|
| 995 | {
|
|---|
| 996 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 997 | uid_t euid = 0;
|
|---|
| 998 |
|
|---|
| 999 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1000 | # ifdef _MSC_VER
|
|---|
| 1001 | uid_t euid = 0;
|
|---|
| 1002 | # else
|
|---|
| 1003 | uid_t euid = geteuid();
|
|---|
| 1004 | # endif
|
|---|
| 1005 |
|
|---|
| 1006 | #else
|
|---|
| 1007 | #endif
|
|---|
| 1008 |
|
|---|
| 1009 | TRACE2((psh, "sh_geteuid() -> %d [%d]\n", euid, errno));
|
|---|
| 1010 | (void)psh;
|
|---|
| 1011 | return euid;
|
|---|
| 1012 | }
|
|---|
| 1013 |
|
|---|
| 1014 | gid_t sh_getgid(shinstance *psh)
|
|---|
| 1015 | {
|
|---|
| 1016 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1017 | gid_t gid = 0;
|
|---|
| 1018 |
|
|---|
| 1019 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1020 | # ifdef _MSC_VER
|
|---|
| 1021 | gid_t gid = 0;
|
|---|
| 1022 | # else
|
|---|
| 1023 | gid_t gid = getgid();
|
|---|
| 1024 | # endif
|
|---|
| 1025 |
|
|---|
| 1026 | #else
|
|---|
| 1027 | #endif
|
|---|
| 1028 |
|
|---|
| 1029 | TRACE2((psh, "sh_getgid() -> %d [%d]\n", gid, errno));
|
|---|
| 1030 | (void)psh;
|
|---|
| 1031 | return gid;
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| 1034 | gid_t sh_getegid(shinstance *psh)
|
|---|
| 1035 | {
|
|---|
| 1036 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1037 | gid_t egid = 0;
|
|---|
| 1038 |
|
|---|
| 1039 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1040 | # ifdef _MSC_VER
|
|---|
| 1041 | gid_t egid = 0;
|
|---|
| 1042 | # else
|
|---|
| 1043 | gid_t egid = getegid();
|
|---|
| 1044 | # endif
|
|---|
| 1045 |
|
|---|
| 1046 | #else
|
|---|
| 1047 | #endif
|
|---|
| 1048 |
|
|---|
| 1049 | TRACE2((psh, "sh_getegid() -> %d [%d]\n", egid, errno));
|
|---|
| 1050 | (void)psh;
|
|---|
| 1051 | return egid;
|
|---|
| 1052 | }
|
|---|
| 1053 |
|
|---|
| 1054 | pid_t sh_getpid(shinstance *psh)
|
|---|
| 1055 | {
|
|---|
| 1056 | pid_t pid;
|
|---|
| 1057 |
|
|---|
| 1058 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1059 | pid = 0;
|
|---|
| 1060 |
|
|---|
| 1061 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1062 | # ifdef _MSC_VER
|
|---|
| 1063 | pid = _getpid();
|
|---|
| 1064 | # else
|
|---|
| 1065 | pid = getpid();
|
|---|
| 1066 | # endif
|
|---|
| 1067 | #else
|
|---|
| 1068 | #endif
|
|---|
| 1069 |
|
|---|
| 1070 | (void)psh;
|
|---|
| 1071 | return pid;
|
|---|
| 1072 | }
|
|---|
| 1073 |
|
|---|
| 1074 | pid_t sh_getpgrp(shinstance *psh)
|
|---|
| 1075 | {
|
|---|
| 1076 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1077 | pid_t pgrp = 0;
|
|---|
| 1078 |
|
|---|
| 1079 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1080 | # ifdef _MSC_VER
|
|---|
| 1081 | pid_t pgrp = _getpid();
|
|---|
| 1082 | # else
|
|---|
| 1083 | pid_t pgrp = getpgrp();
|
|---|
| 1084 | # endif
|
|---|
| 1085 |
|
|---|
| 1086 | #else
|
|---|
| 1087 | #endif
|
|---|
| 1088 |
|
|---|
| 1089 | TRACE2((psh, "sh_getpgrp() -> %d [%d]\n", pgrp, errno));
|
|---|
| 1090 | (void)psh;
|
|---|
| 1091 | return pgrp;
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
|---|
| 1095 | {
|
|---|
| 1096 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1097 | pid_t pgid = pid;
|
|---|
| 1098 |
|
|---|
| 1099 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1100 | # ifdef _MSC_VER
|
|---|
| 1101 | pid_t pgid = pid;
|
|---|
| 1102 | # else
|
|---|
| 1103 | pid_t pgid = getpgid(pid);
|
|---|
| 1104 | # endif
|
|---|
| 1105 |
|
|---|
| 1106 | #else
|
|---|
| 1107 | #endif
|
|---|
| 1108 |
|
|---|
| 1109 | TRACE2((psh, "sh_getpgid(%d) -> %d [%d]\n", pid, pgid, errno));
|
|---|
| 1110 | (void)psh;
|
|---|
| 1111 | return pgid;
|
|---|
| 1112 | }
|
|---|
| 1113 |
|
|---|
| 1114 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
|---|
| 1115 | {
|
|---|
| 1116 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1117 | int rc = -1;
|
|---|
| 1118 |
|
|---|
| 1119 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1120 | # ifdef _MSC_VER
|
|---|
| 1121 | int rc = -1;
|
|---|
| 1122 | errno = ENOSYS;
|
|---|
| 1123 | # else
|
|---|
| 1124 | int rc = setpgid(pid, pgid);
|
|---|
| 1125 | # endif
|
|---|
| 1126 |
|
|---|
| 1127 | #else
|
|---|
| 1128 | #endif
|
|---|
| 1129 |
|
|---|
| 1130 | TRACE2((psh, "sh_setpgid(%d, %d) -> %d [%d]\n", pid, pgid, rc, errno));
|
|---|
| 1131 | (void)psh;
|
|---|
| 1132 | return rc;
|
|---|
| 1133 | }
|
|---|
| 1134 |
|
|---|
| 1135 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
|---|
| 1136 | {
|
|---|
| 1137 | pid_t pgrp;
|
|---|
| 1138 |
|
|---|
| 1139 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1140 | pgrp = -1;
|
|---|
| 1141 |
|
|---|
| 1142 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1143 | # ifdef _MSC_VER
|
|---|
| 1144 | pgrp = -1;
|
|---|
| 1145 | errno = ENOSYS;
|
|---|
| 1146 | # else
|
|---|
| 1147 | pgrp = tcgetpgrp(fd);
|
|---|
| 1148 | # endif
|
|---|
| 1149 |
|
|---|
| 1150 | #else
|
|---|
| 1151 | #endif
|
|---|
| 1152 |
|
|---|
| 1153 | TRACE2((psh, "sh_tcgetpgrp(%d) -> %d [%d]\n", fd, pgrp, errno));
|
|---|
| 1154 | (void)psh;
|
|---|
| 1155 | return pgrp;
|
|---|
| 1156 | }
|
|---|
| 1157 |
|
|---|
| 1158 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
|---|
| 1159 | {
|
|---|
| 1160 | int rc;
|
|---|
| 1161 | TRACE2((psh, "sh_tcsetpgrp(%d, %d)\n", fd, pgrp));
|
|---|
| 1162 |
|
|---|
| 1163 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1164 | rc = -1;
|
|---|
| 1165 |
|
|---|
| 1166 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1167 | # ifdef _MSC_VER
|
|---|
| 1168 | rc = -1;
|
|---|
| 1169 | errno = ENOSYS;
|
|---|
| 1170 | # else
|
|---|
| 1171 | rc = tcsetpgrp(fd, pgrp);
|
|---|
| 1172 | # endif
|
|---|
| 1173 |
|
|---|
| 1174 | #else
|
|---|
| 1175 | #endif
|
|---|
| 1176 |
|
|---|
| 1177 | TRACE2((psh, "sh_tcsetpgrp(%d, %d) -> %d [%d]\n", fd, pgrp, rc, errno));
|
|---|
| 1178 | (void)psh;
|
|---|
| 1179 | return rc;
|
|---|
| 1180 | }
|
|---|
| 1181 |
|
|---|
| 1182 | int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
|
|---|
| 1183 | {
|
|---|
| 1184 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1185 | int rc = -1;
|
|---|
| 1186 |
|
|---|
| 1187 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1188 | # ifdef _MSC_VER
|
|---|
| 1189 | int rc = -1;
|
|---|
| 1190 | errno = ENOSYS;
|
|---|
| 1191 | # else
|
|---|
| 1192 | int rc = getrlimit(resid, limp);
|
|---|
| 1193 | # endif
|
|---|
| 1194 |
|
|---|
| 1195 | #else
|
|---|
| 1196 | /* returned the stored limit */
|
|---|
| 1197 | #endif
|
|---|
| 1198 |
|
|---|
| 1199 | TRACE2((psh, "sh_getrlimit(%d, %p) -> %d [%d] {%ld,%ld}\n",
|
|---|
| 1200 | resid, limp, rc, errno, (long)limp->rlim_cur, (long)limp->rlim_max));
|
|---|
| 1201 | (void)psh;
|
|---|
| 1202 | return rc;
|
|---|
| 1203 | }
|
|---|
| 1204 |
|
|---|
| 1205 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
|---|
| 1206 | {
|
|---|
| 1207 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 1208 | int rc = -1;
|
|---|
| 1209 |
|
|---|
| 1210 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
|---|
| 1211 | # ifdef _MSC_VER
|
|---|
| 1212 | int rc = -1;
|
|---|
| 1213 | errno = ENOSYS;
|
|---|
| 1214 | # else
|
|---|
| 1215 | int rc = setrlimit(resid, limp);
|
|---|
| 1216 | # endif
|
|---|
| 1217 |
|
|---|
| 1218 | #else
|
|---|
| 1219 | /* if max(shell) < limp; then setrlimit; fi
|
|---|
| 1220 | if success; then store limit for later retrival and maxing. */
|
|---|
| 1221 |
|
|---|
| 1222 | #endif
|
|---|
| 1223 |
|
|---|
| 1224 | TRACE2((psh, "sh_setrlimit(%d, %p:{%ld,%ld}) -> %d [%d]\n",
|
|---|
| 1225 | resid, limp, (long)limp->rlim_cur, (long)limp->rlim_max, rc, errno));
|
|---|
| 1226 | (void)psh;
|
|---|
| 1227 | return rc;
|
|---|
| 1228 | }
|
|---|
| 1229 |
|
|---|