VirtualBox

source: kBuild/trunk/src/kash/shinstance.h@ 3459

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

kash: more sh_destroy work.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette