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

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

kash: Hammering on threaded mode. Got shexit half working. Incomplete sh_destroy.

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