source: trunk/src/kash/shinstance.h@ 3460

Last change on this file since 3460 was 3460, checked in by bird, 5 years ago

kash: Use event semaphores to signal parent shell early before the subshell thread has performed cleanups.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 21.3 KB
Line 
1/* $Id: shinstance.h 3460 2020-09-15 12:31:01Z bird $ */
2/** @file
3 * The shell instance and it's methods.
4 */
5
6/*
7 * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@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#ifndef ___shinstance_h
29#define ___shinstance_h
30
31#include <stdio.h> /* BUFSIZ */
32#include <signal.h> /* NSIG */
33#ifndef _MSC_VER
34# include <termios.h>
35# include <sys/types.h>
36# include <sys/ioctl.h>
37# include <sys/resource.h>
38#endif
39#include <errno.h>
40#ifdef _MSC_VER
41# define EWOULDBLOCK 140
42#endif
43
44#include "shtypes.h"
45#include "shthread.h"
46#include "shfile.h"
47#include "shheap.h"
48#include "shell.h"
49#include "output.h"
50#include "options.h"
51
52#include "expand.h"
53#include "exec.h"
54#include "var.h"
55#include "show.h"
56
57#ifdef _MSC_VER
58# define strcasecmp stricmp
59# define strncasecmp strnicmp
60#endif
61
62#ifndef SH_FORKED_MODE
63extern shmtx g_sh_exec_inherit_mtx;
64#endif
65
66#ifndef SH_FORKED_MODE
67/**
68 * Subshell status.
69 */
70typedef struct shsubshellstatus
71{
72 unsigned volatile refs; /**< Reference counter. */
73 int volatile status; /**< The exit code. */
74 KBOOL volatile done; /**< Set if done (valid exit code). */
75 void *towaiton; /**< Event semaphore / whatever to wait on. */
76# if K_OS == K_OS_WINDOWS
77 uintptr_t volatile hThread; /**< The thread handle (child closes this). */
78# endif
79 struct shsubshellstatus *next; /**< Next free one on the free chain. */
80} shsubshellstatus;
81#endif
82
83/**
84 * A child process.
85 */
86typedef struct shchild
87{
88 shpid pid; /**< The pid. */
89#if K_OS == K_OS_WINDOWS
90 void *hChild; /**< The handle to wait on. */
91#endif
92#ifndef SH_FORKED_MODE
93 shsubshellstatus *subshellstatus; /**< Pointer to the subshell status structure. NULL if child process. */
94#endif
95} shchild;
96
97/* memalloc.c */
98#define MINSIZE 504 /* minimum size of a block */
99struct stack_block {
100 struct stack_block *prev;
101 char space[MINSIZE];
102};
103
104#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
105/** Parser stack allocator block.
106 * These are reference counted so they can be shared between the parent and
107 * child shells. They are also using as an alternative to copying function
108 * definitions, here the final goal is to automatically emit separate
109 * pstack_blocks for function while parsing to make it more flexible. */
110typedef struct pstack_block {
111 /** Pointer to the next unallocated byte (= stacknxt). */
112 char *nextbyte;
113 /** Number of bytes available in the current stack block (= stacknleft). */
114 size_t avail;
115 /* Number of chars left for string data (PSTPUTC, PSTUPUTC, et al) (= sstrnleft). */
116 size_t strleft;
117 /** Top of the allocation stack (nextbyte points within this). */
118 struct stack_block *top;
119 /** Size of the top stack element (user space only). */
120 size_t topsize;
121 /** @name statistics
122 * @{ */
123 size_t allocations;
124 size_t bytesalloced;
125 size_t nodesalloced;
126 size_t entriesalloced;
127 size_t strbytesalloced;
128 size_t blocks;
129 size_t fragmentation;
130 /** @} */
131 /** Reference counter. */
132 unsigned volatile refs;
133 unsigned padding;
134 /** The first stack block. */
135 struct stack_block first;
136} pstack_block;
137#endif
138
139/* input.c */
140struct strpush {
141 struct strpush *prev; /* preceding string on stack */
142 char *prevstring;
143 int prevnleft;
144 int prevlleft;
145 struct alias *ap; /* if push was associated with an alias */
146};
147
148/*
149 * The parsefile structure pointed to by the global variable parsefile
150 * contains information about the current file being read.
151 */
152struct parsefile {
153 struct parsefile *prev; /* preceding file on stack */
154 int linno; /* current line */
155 int fd; /* file descriptor (or -1 if string) */
156 int nleft; /* number of chars left in this line */
157 int lleft; /* number of chars left in this buffer */
158 char *nextc; /* next char in buffer */
159 char *buf; /* input buffer */
160 struct strpush *strpush; /* for pushing strings at this level */
161 struct strpush basestrpush; /* so pushing one is fast */
162};
163
164/* exec.c */
165#define CMDTABLESIZE 31 /* should be prime */
166#define ARB 1 /* actual size determined at run time */
167
168struct tblentry {
169 struct tblentry *next; /* next entry in hash chain */
170 union param param; /* definition of builtin function */
171 short cmdtype; /* index identifying command */
172 char rehash; /* if set, cd done since entry created */
173 char cmdname[ARB]; /* name of command */
174};
175
176/* expand.c */
177/*
178 * Structure specifying which parts of the string should be searched
179 * for IFS characters.
180 */
181struct ifsregion {
182 struct ifsregion *next; /* next region in list */
183 int begoff; /* offset of start of region */
184 int endoff; /* offset of end of region */
185 int inquotes; /* search for nul bytes only */
186};
187
188/* redir.c */
189struct redirtab {
190 struct redirtab *next;
191 short renamed[10];
192};
193
194/**
195 * This is a replacement for temporary node field nfile.expfname.
196 * Uses stack allocator, created by expredir(), duplicated by
197 * subshellinitredir() and popped (but not freed) by expredircleanup().
198 */
199typedef struct redirexpfnames
200{
201 struct redirexpfnames *prev; /**< Previous record. */
202 unsigned depth; /**< Nesting depth. */
203 unsigned count; /**< Number of expanded filenames in the array. */
204 char *names[1]; /**< Variable size. */
205} redirexpfnames;
206
207
208/**
209 * A shell instance.
210 *
211 * This is the core structure of the shell, it contains all
212 * the data associated with a shell process except that it's
213 * running in a thread and not a separate process.
214 */
215struct shinstance
216{
217 struct shinstance *next; /**< The next shell instance. */
218 struct shinstance *prev; /**< The previous shell instance. */
219 struct shinstance *parent; /**< The parent shell instance. */
220 shpid pid; /**< The (fake) process id of this shell instance. */
221 shtid tid; /**< The thread identifier of the thread for this shell. */
222 shpid pgid; /**< Process group ID. */
223 shfdtab fdtab; /**< The file descriptor table. */
224 shsigaction_t sigactions[NSIG]; /**< The signal actions registered with this shell instance. */
225 shsigset_t sigmask; /**< Our signal mask. */
226 char **shenviron; /**< The environment vector. */
227 int linked; /**< Set if we're still linked. */
228 unsigned num_children; /**< Number of children in the array. */
229 shchild *children; /**< The child array. */
230#ifndef SH_FORKED_MODE
231 int (*thread)(struct shinstance *, void *); /**< The thread procedure. */
232 void *threadarg; /**< The thread argument. */
233 struct jmploc *exitjmp; /**< Long jump target in sh_thread_wrapper for use by sh__exit. */
234 shsubshellstatus *subshellstatus; /**< Pointer to the subshell status structure (NULL if root). */
235#endif
236
237 /* alias.c */
238#define ATABSIZE 39
239 struct alias *atab[ATABSIZE];
240 unsigned aliases; /**< Number of active aliases. */
241
242 /* cd.c */
243 char *curdir; /**< current working directory */
244 char *prevdir; /**< previous working directory */
245 char *cdcomppath; /**< (stalloc) */
246 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
247
248 /* error.h */
249 struct jmploc *handler;
250 int exception;
251 int exerrno/* = 0 */; /**< Last exec error */
252 int volatile suppressint;
253 int volatile intpending;
254
255 /* error.c */
256 char errmsg_buf[16]; /**< static in errmsg. (bss) */
257
258 /* eval.h */
259 char *commandname; /**< currently executing command */
260 int exitstatus; /**< exit status of last command */
261 int back_exitstatus;/**< exit status of backquoted command */
262 struct strlist *cmdenviron; /**< environment for builtin command (varlist from evalcommand()) */
263 int funcnest; /**< depth of function calls */
264 int evalskip; /**< set if we are skipping commands */
265 int skipcount; /**< number of levels to skip */
266 int loopnest; /**< current loop nesting level */
267 int commandnamemalloc; /**< Set if commandname is malloc'ed (only subshells). */
268
269 /* expand.c */
270 char *expdest; /**< output of current string (stack) */
271 struct nodelist *argbackq; /**< list of back quote expressions */
272 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
273 struct ifsregion *ifslastp; /**< last struct in list */
274 struct arglist exparg; /**< holds expanded arg list (stack) */
275 char *expdir; /**< Used by expandmeta. */
276
277 /* exec.h */
278 const char *pathopt; /**< set by padvance */
279
280 /* exec.c */
281 struct tblentry *cmdtable[CMDTABLESIZE];
282 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
283
284 /* input.h */
285 int plinno/* = 1 */;/**< input line number */
286 int parsenleft; /**< number of characters left in input buffer */
287 char *parsenextc; /**< next character in input buffer */
288 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
289
290 /* input.c */
291 int parselleft; /**< copy of parsefile->lleft */
292 struct parsefile basepf; /**< top level input file */
293 char basebuf[BUFSIZ];/**< buffer for top level input file */
294 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
295#ifndef SMALL
296 EditLine *el; /**< cookie for editline package */
297#endif
298
299 /* jobs.h */
300 shpid backgndpid/* = -1 */; /**< pid of last background process */
301 int job_warning; /**< user was warned about stopped jobs */
302
303 /* jobs.c */
304 struct job *jobtab; /**< array of jobs */
305 int njobs; /**< size of array */
306 int jobs_invalid; /**< set in child */
307 shpid initialpgrp; /**< pgrp of shell on invocation */
308 int curjob/* = -1*/;/**< current job */
309 int ttyfd/* = -1*/;
310 int jobctl; /**< job control enabled / disabled */
311 char *cmdnextc;
312 int cmdnleft;
313
314
315 /* mail.c */
316#define MAXMBOXES 10
317 int nmboxes; /**< number of mailboxes */
318 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
319
320 /* main.h */
321 shpid rootpid; /**< pid of main shell. */
322 int rootshell; /**< true if we aren't a child of the main shell. */
323 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
324
325 /* memalloc.h */
326 char *stacknxt/* = stackbase.space*/;
327 int stacknleft/* = MINSIZE*/;
328 int sstrnleft;
329 int herefd/* = -1 */;
330
331 /* memalloc.c */
332 struct stack_block stackbase;
333 struct stack_block *stackp/* = &stackbase*/;
334 struct stackmark *markp;
335
336#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
337 pstack_block *curpstack; /**< The pstack entry we're currently allocating from (NULL when not in parse.c). */
338 pstack_block **pstack; /**< Stack of parsed stuff. */
339 unsigned pstacksize; /**< Number of entries in pstack. */
340 unsigned pstackalloced; /**< The allocated size of pstack. */
341#endif
342
343 /* myhistedit.h */
344 int displayhist;
345#ifndef SMALL
346 History *hist;
347 EditLine *el;
348#endif
349
350 /* output.h */
351 struct output output;
352 struct output errout;
353 struct output memout;
354 struct output *out1;
355 struct output *out2;
356
357 /* output.c */
358#define OUTBUFSIZ BUFSIZ
359#define MEM_OUT -3 /**< output to dynamically allocated memory */
360
361 /* options.h */
362 struct optent optlist[NOPTS];
363 char *minusc; /**< argument to -c option */
364 char *arg0; /**< $0 */
365 struct shparam shellparam; /**< $@ */
366 char **argptr; /**< argument list for builtin commands */
367 char *optionarg; /**< set by nextopt */
368 char *optptr; /**< used by nextopt */
369 char **orgargv; /**< The original argument vector (for cleanup). */
370 int arg0malloc; /**< Indicates whether arg0 was allocated or is part of orgargv. */
371
372 /* parse.h */
373 int tokpushback;
374 int whichprompt; /**< 1 == PS1, 2 == PS2 */
375
376 /* parser.c */
377 int noalias/* = 0*/;/**< when set, don't handle aliases */
378 struct heredoc *heredoclist; /**< list of here documents to read */
379 int parsebackquote; /**< nonzero if we are inside backquotes */
380 int doprompt; /**< if set, prompt the user */
381 int needprompt; /**< true if interactive and at start of line */
382 int lasttoken; /**< last token read */
383 char *wordtext; /**< text of last word returned by readtoken */
384 int checkkwd; /**< 1 == check for kwds, 2 == also eat newlines */
385 struct nodelist *backquotelist;
386 union node *redirnode;
387 struct heredoc *heredoc;
388 int quoteflag; /**< set if (part of) last token was quoted */
389 int startlinno; /**< line # where last token started */
390
391 /* redir.c */
392 struct redirtab *redirlist;
393 int fd0_redirected/* = 0*/;
394 redirexpfnames *expfnames; /**< Expanded filenames for current redirection setup. */
395
396 /* show.c */
397 char tracebuf[1024];
398 size_t tracepos;
399 int tracefd;
400
401 /* trap.h */
402 int pendingsigs; /**< indicates some signal received */
403
404 /* trap.c */
405 char gotsig[NSIG]; /**< indicates specified signal received */
406 char *trap[NSIG+1]; /**< trap handler commands */
407 char sigmode[NSIG]; /**< current value of signal */
408
409 /* var.h */
410 struct localvar *localvars;
411 struct var vatty;
412 struct var vifs;
413 struct var vmail;
414 struct var vmpath;
415 struct var vpath;
416#ifdef _MSC_VER
417 struct var vpath2;
418#endif
419 struct var vps1;
420 struct var vps2;
421 struct var vps4;
422#ifndef SMALL
423 struct var vterm;
424 struct var vhistsize;
425#endif
426 struct var voptind;
427#ifdef PC_OS2_LIBPATHS
428 struct var libpath_vars[4];
429#endif
430#ifdef SMALL
431# define VTABSIZE 39
432#else
433# define VTABSIZE 517
434#endif
435 struct var *vartab[VTABSIZE];
436
437 /* builtins.h */
438
439 /* bltin/test.c */
440 char **t_wp;
441 struct t_op const *t_wp_op;
442};
443
444
445extern shinstance *sh_create_root_shell(char **, char **);
446extern shinstance *sh_create_child_shell(shinstance *);
447
448/* environment & pwd.h */
449char *sh_getenv(shinstance *, const char *);
450char **sh_environ(shinstance *);
451const char *sh_gethomedir(shinstance *, const char *);
452
453/* signals */
454#define SH_SIG_UNK ((shsig_t)(intptr_t)-199)
455#define SH_SIG_DFL ((shsig_t)(intptr_t)SIG_DFL)
456#define SH_SIG_IGN ((shsig_t)(intptr_t)SIG_IGN)
457#define SH_SIG_ERR ((shsig_t)(intptr_t)SIG_ERR)
458#ifdef _MSC_VER
459# define SA_RESTART 0x02
460# define SIG_BLOCK 1
461# define SIG_UNBLOCK 2
462# define SIG_SETMASK 3
463
464# define SIGHUP 1 /* _SIGHUP_IGNORE */
465/*# define SIGINT 2 */
466# define SIGQUIT 3 /* _SIGQUIT_IGNORE */
467/*# define SIGILL 4 */
468/*# define SIGFPE 8 */
469/*# define SIGSEGV 11 */
470# define SIGPIPE 13 /* _SIGPIPE_IGNORE */
471/*# define SIGTERM 15 */
472# define SIGTTIN 16 /* _SIGIOINT_IGNORE */
473# define SIGTSTP 17 /* _SIGSTOP_IGNORE */
474# define SIGTTOU 18
475# define SIGCONT 20
476/*# define SIGBREAK 21 */
477/*# define SIGABRT 22 */
478const char *strsignal(int iSig);
479#endif /* _MSC_VER */
480#ifndef HAVE_SYS_SIGNAME
481extern const char * const sys_signame[NSIG];
482#endif
483
484int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
485shsig_t sh_signal(shinstance *, int, shsig_t);
486int sh_siginterrupt(shinstance *, int, int);
487void sh_sigemptyset(shsigset_t *);
488void sh_sigfillset(shsigset_t *);
489void sh_sigaddset(shsigset_t *, int);
490void sh_sigdelset(shsigset_t *, int);
491int sh_sigismember(shsigset_t const *, int);
492int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
493SH_NORETURN_1 void sh_abort(shinstance *) SH_NORETURN_2;
494void sh_raise_sigint(shinstance *);
495int sh_kill(shinstance *, shpid, int);
496int sh_killpg(shinstance *, shpid, int);
497
498/* times */
499#include <time.h>
500#ifdef _MSC_VER
501 typedef struct shtms
502 {
503 clock_t tms_utime;
504 clock_t tms_stime;
505 clock_t tms_cutime;
506 clock_t tms_cstime;
507 } shtms;
508#else
509# include <sys/times.h>
510 typedef struct tms shtms;
511#endif
512clock_t sh_times(shinstance *, shtms *);
513int sh_sysconf_clk_tck(void);
514
515/* wait / process */
516int sh_add_child(shinstance *psh, shpid pid, void *hChild, shsubshellstatus *sts);
517#ifdef _MSC_VER
518# include <process.h>
519# define WNOHANG 1 /* Don't hang in wait. */
520# define WUNTRACED 2 /* Tell about stopped, untraced children. */
521# define WCONTINUED 4 /* Report a job control continued process. */
522# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
523# define WCOREFLAG 0200
524# define _WSTATUS(x) (_W_INT(x) & 0177)
525# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
526# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
527# define WSTOPSIG(x) (_W_INT(x) >> 8)
528# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
529# define WTERMSIG(x) (_WSTATUS(x))
530# define WIFEXITED(x) (_WSTATUS(x) == 0)
531# define WEXITSTATUS(x) (_W_INT(x) >> 8)
532# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
533# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
534# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
535# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
536#else
537# include <sys/wait.h>
538# ifdef __HAIKU__
539# define WCOREDUMP(x) WIFCORED(x)
540# endif
541#endif
542#ifdef SH_FORKED_MODE
543shpid sh_fork(shinstance *);
544#else
545shpid sh_thread_start(shinstance *pshparent, shinstance *pshchild, int (*thread)(shinstance *, void *), void *arg);
546#endif
547shpid sh_waitpid(shinstance *, shpid, int *, int);
548SH_NORETURN_1 void sh__exit(shinstance *, int) SH_NORETURN_2;
549int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
550uid_t sh_getuid(shinstance *);
551uid_t sh_geteuid(shinstance *);
552gid_t sh_getgid(shinstance *);
553gid_t sh_getegid(shinstance *);
554shpid sh_getpid(shinstance *);
555shpid sh_getpgrp(shinstance *);
556shpid sh_getpgid(shinstance *, shpid);
557int sh_setpgid(shinstance *, shpid, shpid);
558
559/* tc* */
560shpid sh_tcgetpgrp(shinstance *, int);
561int sh_tcsetpgrp(shinstance *, int, shpid);
562
563/* sys/resource.h */
564#ifdef _MSC_VER
565 typedef int64_t shrlim_t;
566 typedef struct shrlimit
567 {
568 shrlim_t rlim_cur;
569 shrlim_t rlim_max;
570 } shrlimit;
571# define RLIMIT_CPU 0
572# define RLIMIT_FSIZE 1
573# define RLIMIT_DATA 2
574# define RLIMIT_STACK 3
575# define RLIMIT_CORE 4
576# define RLIMIT_RSS 5
577# define RLIMIT_MEMLOCK 6
578# define RLIMIT_NPROC 7
579# define RLIMIT_NOFILE 8
580# define RLIMIT_SBSIZE 9
581# define RLIMIT_VMEM 10
582# define RLIM_NLIMITS 11
583# define RLIM_INFINITY (0x7fffffffffffffffLL)
584#else
585 typedef rlim_t shrlim_t;
586 typedef struct rlimit shrlimit;
587#endif
588int sh_getrlimit(shinstance *, int, shrlimit *);
589int sh_setrlimit(shinstance *, int, const shrlimit *);
590
591/* string.h */
592const char *sh_strerror(shinstance *, int);
593
594#ifdef DEBUG
595# define TRACE2(param) trace param
596# define TRACE2V(param) tracev param
597#else
598# define TRACE2(param) do { } while (0)
599# define TRACE2V(param) do { } while (0)
600#endif
601
602#endif
Note: See TracBrowser for help on using the repository browser.