VirtualBox

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

Last change on this file since 3480 was 3480, checked in by bird, 4 years ago

kash: build fixes (darwin).

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

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