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

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

kash: Avoid handle inheritance on windows when possible, allowing concurrent CreateProcess calls.

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