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

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

kash: forked-mode build fixes.

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