| 1 | /* $Id: shinstance.c 1240 2007-10-10 02:32:54Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * The shell instance methods.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kBuild; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include <string.h>
|
|---|
| 28 | #include <stdlib.h>
|
|---|
| 29 | #ifndef _MSC_VER
|
|---|
| 30 | # include <unistd.h>
|
|---|
| 31 | # include <pwd.h>
|
|---|
| 32 | extern char **environ;
|
|---|
| 33 | #endif
|
|---|
| 34 | #include "shinstance.h"
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Creates a root shell instance.
|
|---|
| 39 | *
|
|---|
| 40 | * @param inherit The shell to inherit from. If NULL inherit from environment and such.
|
|---|
| 41 | * @param argc The argument count.
|
|---|
| 42 | * @param argv The argument vector.
|
|---|
| 43 | *
|
|---|
| 44 | * @returns pointer to root shell on success, NULL on failure.
|
|---|
| 45 | */
|
|---|
| 46 | shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
|
|---|
| 47 | {
|
|---|
| 48 | shinstance *psh;
|
|---|
| 49 |
|
|---|
| 50 | psh = calloc(sizeof(*psh), 1);
|
|---|
| 51 | if (psh)
|
|---|
| 52 | {
|
|---|
| 53 | /* the special stuff. */
|
|---|
| 54 | #ifdef _MSC_VER
|
|---|
| 55 | psh->pid = _getpid();
|
|---|
| 56 | #else
|
|---|
| 57 | psh->pid = getpid();
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | /* memalloc.c */
|
|---|
| 61 | psh->stacknleft = MINSIZE;
|
|---|
| 62 | psh->herefd = -1;
|
|---|
| 63 | psh->stackp = &psh->stackbase;
|
|---|
| 64 | psh->stacknxt = psh->stackbase.space;
|
|---|
| 65 |
|
|---|
| 66 | /* input.c */
|
|---|
| 67 | psh->plinno = 1;
|
|---|
| 68 | psh->init_editline = 0;
|
|---|
| 69 | psh->parsefile = &psh->basepf;
|
|---|
| 70 |
|
|---|
| 71 | /* output.c */
|
|---|
| 72 | psh->output.bufsize = OUTBUFSIZ;
|
|---|
| 73 | psh->output.fd = 1;
|
|---|
| 74 | psh->output.psh = psh;
|
|---|
| 75 | psh->errout.bufsize = 100;
|
|---|
| 76 | psh->errout.fd = 2;
|
|---|
| 77 | psh->errout.psh = psh;
|
|---|
| 78 | psh->memout.fd = MEM_OUT;
|
|---|
| 79 | psh->memout.psh = psh;
|
|---|
| 80 | psh->out1 = &psh->output;
|
|---|
| 81 | psh->out2 = &psh->errout;
|
|---|
| 82 |
|
|---|
| 83 | /* jobs.c */
|
|---|
| 84 | psh->backgndpid = -1;
|
|---|
| 85 | #if JOBS
|
|---|
| 86 | psh->curjob = -1;
|
|---|
| 87 | #endif
|
|---|
| 88 | psh->ttyfd = -1;
|
|---|
| 89 |
|
|---|
| 90 | }
|
|---|
| 91 | return psh;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | char *sh_getenv(shinstance *psh, const char *var)
|
|---|
| 96 | {
|
|---|
| 97 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 98 | return NULL;
|
|---|
| 99 | #elif defined(SH_STUB_MODE)
|
|---|
| 100 | return getenv(var);
|
|---|
| 101 | #else
|
|---|
| 102 | #endif
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | char **sh_environ(shinstance *psh)
|
|---|
| 106 | {
|
|---|
| 107 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 108 | static char *s_null[2] = {0,0};
|
|---|
| 109 | return &s_null[0];
|
|---|
| 110 | #elif defined(SH_STUB_MODE)
|
|---|
| 111 | return environ;
|
|---|
| 112 | #else
|
|---|
| 113 | #endif
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | const char *sh_gethomedir(shinstance *psh, const char *user)
|
|---|
| 117 | {
|
|---|
| 118 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 119 | return NULL;
|
|---|
| 120 | #elif defined(SH_STUB_MODE)
|
|---|
| 121 | # ifdef _MSC_VER
|
|---|
| 122 | return NULL;
|
|---|
| 123 | # else
|
|---|
| 124 | struct passwd *pwd = getpwnam(user);
|
|---|
| 125 | return pwd ? pwd->pw_dir : NULL;
|
|---|
| 126 | # endif
|
|---|
| 127 | #else
|
|---|
| 128 | #endif
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
|
|---|
| 132 | {
|
|---|
| 133 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 134 | return -1;
|
|---|
| 135 | #elif defined(SH_STUB_MODE)
|
|---|
| 136 | # ifdef _MSC_VER
|
|---|
| 137 | return -1;
|
|---|
| 138 | # else
|
|---|
| 139 | struct sigaction old;
|
|---|
| 140 | if (newp)
|
|---|
| 141 | return -1;
|
|---|
| 142 | if (sigaction(signo, NULL, &old))
|
|---|
| 143 | return -1;
|
|---|
| 144 | oldp->sh_flags = old.sa_flags;
|
|---|
| 145 | oldp->sh_handler = old.sa_handler;
|
|---|
| 146 | oldp->sh_mask = old.sa_mask;
|
|---|
| 147 | return 0;
|
|---|
| 148 | # endif
|
|---|
| 149 | #else
|
|---|
| 150 | #endif
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
|
|---|
| 154 | {
|
|---|
| 155 | return (shsig_t)-1;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
|---|
| 159 | {
|
|---|
| 160 | return -1;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | void sh_sigemptyset(shsigset_t *setp)
|
|---|
| 164 | {
|
|---|
| 165 | memset(setp, 0, sizeof(*setp));
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
|
|---|
| 169 | {
|
|---|
| 170 | return -1;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | void sh_abort(shinstance *psh)
|
|---|
| 174 | {
|
|---|
| 175 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 176 | #elif defined(SH_STUB_MODE)
|
|---|
| 177 | abort();
|
|---|
| 178 | #else
|
|---|
| 179 | #endif
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | void sh_raise_sigint(shinstance *psh)
|
|---|
| 183 | {
|
|---|
| 184 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 185 | #elif defined(SH_STUB_MODE)
|
|---|
| 186 | raise(SIGINT);
|
|---|
| 187 | #else
|
|---|
| 188 | #endif
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
|---|
| 192 | {
|
|---|
| 193 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 194 | return -1;
|
|---|
| 195 | #elif defined(SH_STUB_MODE)
|
|---|
| 196 | # ifdef _MSC_VER
|
|---|
| 197 | return -1;
|
|---|
| 198 | # else
|
|---|
| 199 | //fprintf(stderr, "kill(%d, %d)\n", pid, signo);
|
|---|
| 200 | return kill(pid, signo);
|
|---|
| 201 | # endif
|
|---|
| 202 | #else
|
|---|
| 203 | #endif
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
|---|
| 207 | {
|
|---|
| 208 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 209 | return -1;
|
|---|
| 210 | #elif defined(SH_STUB_MODE)
|
|---|
| 211 | # ifdef _MSC_VER
|
|---|
| 212 | return -1;
|
|---|
| 213 | # else
|
|---|
| 214 | //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
|
|---|
| 215 | return killpg(pgid, signo);
|
|---|
| 216 | # endif
|
|---|
| 217 | #else
|
|---|
| 218 | #endif
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
|---|
| 222 | {
|
|---|
| 223 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 224 | return 0;
|
|---|
| 225 | #elif defined(SH_STUB_MODE)
|
|---|
| 226 | # ifdef _MSC_VER
|
|---|
| 227 | return 0;
|
|---|
| 228 | # else
|
|---|
| 229 | return times(tmsp);
|
|---|
| 230 | # endif
|
|---|
| 231 | #else
|
|---|
| 232 | #endif
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | int sh_sysconf_clk_tck(void)
|
|---|
| 236 | {
|
|---|
| 237 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 238 | return 1;
|
|---|
| 239 | #else
|
|---|
| 240 | # ifdef _MSC_VER
|
|---|
| 241 | return CLK_TCK;
|
|---|
| 242 | # else
|
|---|
| 243 | return sysconf(_SC_CLK_TCK);
|
|---|
| 244 | # endif
|
|---|
| 245 | #endif
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | pid_t sh_fork(shinstance *psh)
|
|---|
| 249 | {
|
|---|
| 250 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 251 | return -1;
|
|---|
| 252 | #elif defined(SH_STUB_MODE)
|
|---|
| 253 | # ifdef _MSC_VER
|
|---|
| 254 | return -1;
|
|---|
| 255 | # else
|
|---|
| 256 | return fork();
|
|---|
| 257 | # endif
|
|---|
| 258 | #else
|
|---|
| 259 | #endif
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
|---|
| 263 | {
|
|---|
| 264 | *statusp = 0;
|
|---|
| 265 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 266 | return -1;
|
|---|
| 267 | #elif defined(SH_STUB_MODE)
|
|---|
| 268 | # ifdef _MSC_VER
|
|---|
| 269 | return -1;
|
|---|
| 270 | # else
|
|---|
| 271 | pid = waitpid(pid, statusp, flags);
|
|---|
| 272 | //fprintf(stderr, "waitpid -> %d *statusp=%d (rc=%d) flags=%#x\n", pid, *statusp, WEXITSTATUS(*statusp), flags);
|
|---|
| 273 | return pid;
|
|---|
| 274 | # endif
|
|---|
| 275 | #else
|
|---|
| 276 | #endif
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | void sh__exit(shinstance *psh, int rc)
|
|---|
| 280 | {
|
|---|
| 281 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 282 | return -1;
|
|---|
| 283 | #elif defined(SH_STUB_MODE)
|
|---|
| 284 | _exit(rc);
|
|---|
| 285 | #else
|
|---|
| 286 | #endif
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
|---|
| 290 | {
|
|---|
| 291 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 292 | return -1;
|
|---|
| 293 | #elif defined(SH_STUB_MODE)
|
|---|
| 294 | # ifdef _MSC_VER
|
|---|
| 295 | return -1;
|
|---|
| 296 | # else
|
|---|
| 297 | return execve(exe, (char **)argv, (char **)envp);
|
|---|
| 298 | # endif
|
|---|
| 299 | #else
|
|---|
| 300 | #endif
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | uid_t sh_getuid(shinstance *psh)
|
|---|
| 304 | {
|
|---|
| 305 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 306 | return 0;
|
|---|
| 307 | #elif defined(SH_STUB_MODE)
|
|---|
| 308 | # ifdef _MSC_VER
|
|---|
| 309 | return 0;
|
|---|
| 310 | # else
|
|---|
| 311 | return getuid();
|
|---|
| 312 | # endif
|
|---|
| 313 | #else
|
|---|
| 314 | #endif
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | uid_t sh_geteuid(shinstance *psh)
|
|---|
| 318 | {
|
|---|
| 319 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 320 | return 0;
|
|---|
| 321 | #elif defined(SH_STUB_MODE)
|
|---|
| 322 | # ifdef _MSC_VER
|
|---|
| 323 | return 0;
|
|---|
| 324 | # else
|
|---|
| 325 | return geteuid();
|
|---|
| 326 | # endif
|
|---|
| 327 | #else
|
|---|
| 328 | #endif
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | gid_t sh_getgid(shinstance *psh)
|
|---|
| 332 | {
|
|---|
| 333 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 334 | return 0;
|
|---|
| 335 | #elif defined(SH_STUB_MODE)
|
|---|
| 336 | # ifdef _MSC_VER
|
|---|
| 337 | return 0;
|
|---|
| 338 | # else
|
|---|
| 339 | return getgid();
|
|---|
| 340 | # endif
|
|---|
| 341 | #else
|
|---|
| 342 | #endif
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | gid_t sh_getegid(shinstance *psh)
|
|---|
| 346 | {
|
|---|
| 347 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 348 | return 0;
|
|---|
| 349 | #elif defined(SH_STUB_MODE)
|
|---|
| 350 | # ifdef _MSC_VER
|
|---|
| 351 | return 0;
|
|---|
| 352 | # else
|
|---|
| 353 | return getegid();
|
|---|
| 354 | # endif
|
|---|
| 355 | #else
|
|---|
| 356 | #endif
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | pid_t sh_getpid(shinstance *psh)
|
|---|
| 360 | {
|
|---|
| 361 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 362 | return 0;
|
|---|
| 363 | #elif defined(SH_STUB_MODE)
|
|---|
| 364 | # ifdef _MSC_VER
|
|---|
| 365 | return _getpid();
|
|---|
| 366 | # else
|
|---|
| 367 | return getpid();
|
|---|
| 368 | # endif
|
|---|
| 369 | #else
|
|---|
| 370 | #endif
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | pid_t sh_getpgrp(shinstance *psh)
|
|---|
| 374 | {
|
|---|
| 375 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 376 | return 0;
|
|---|
| 377 | #elif defined(SH_STUB_MODE)
|
|---|
| 378 | # ifdef _MSC_VER
|
|---|
| 379 | return _getpid();
|
|---|
| 380 | # else
|
|---|
| 381 | return getpgrp();
|
|---|
| 382 | # endif
|
|---|
| 383 | #else
|
|---|
| 384 | #endif
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
|---|
| 388 | {
|
|---|
| 389 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 390 | return pid;
|
|---|
| 391 | #elif defined(SH_STUB_MODE)
|
|---|
| 392 | # ifdef _MSC_VER
|
|---|
| 393 | return pid;
|
|---|
| 394 | # else
|
|---|
| 395 | return getpgid(pid);
|
|---|
| 396 | # endif
|
|---|
| 397 | #else
|
|---|
| 398 | #endif
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
|---|
| 402 | {
|
|---|
| 403 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 404 | return -1;
|
|---|
| 405 | #elif defined(SH_STUB_MODE)
|
|---|
| 406 | # ifdef _MSC_VER
|
|---|
| 407 | return -1;
|
|---|
| 408 | # else
|
|---|
| 409 | int rc;
|
|---|
| 410 | rc = setpgid(pid, pgid);
|
|---|
| 411 | //fprintf(stderr, "setpgid(%d,%d) -> %d\n", pid, pgid, rc);
|
|---|
| 412 | return rc;
|
|---|
| 413 | # endif
|
|---|
| 414 | #else
|
|---|
| 415 | #endif
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
|---|
| 419 | {
|
|---|
| 420 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 421 | return -1;
|
|---|
| 422 | #elif defined(SH_STUB_MODE)
|
|---|
| 423 | # ifdef _MSC_VER
|
|---|
| 424 | return -1;
|
|---|
| 425 | # else
|
|---|
| 426 | return tcgetpgrp(fd);
|
|---|
| 427 | # endif
|
|---|
| 428 | #else
|
|---|
| 429 | #endif
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
|---|
| 433 | {
|
|---|
| 434 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 435 | return -1;
|
|---|
| 436 | #elif defined(SH_STUB_MODE)
|
|---|
| 437 | # ifdef _MSC_VER
|
|---|
| 438 | return -1;
|
|---|
| 439 | # else
|
|---|
| 440 | return tcsetpgrp(fd, pgrp);
|
|---|
| 441 | # endif
|
|---|
| 442 | #else
|
|---|
| 443 | #endif
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
|
|---|
| 447 | {
|
|---|
| 448 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 449 | return -1;
|
|---|
| 450 | #elif defined(SH_STUB_MODE)
|
|---|
| 451 | # ifdef _MSC_VER
|
|---|
| 452 | return -1;
|
|---|
| 453 | # else
|
|---|
| 454 | return getrlimit(resid, limp);
|
|---|
| 455 | # endif
|
|---|
| 456 | #else
|
|---|
| 457 | #endif
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
|---|
| 461 | {
|
|---|
| 462 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 463 | return -1;
|
|---|
| 464 | #elif defined(SH_STUB_MODE)
|
|---|
| 465 | # ifdef _MSC_VER
|
|---|
| 466 | return -1;
|
|---|
| 467 | # else
|
|---|
| 468 | return setrlimit(resid, limp);
|
|---|
| 469 | # endif
|
|---|
| 470 | #else
|
|---|
| 471 | #endif
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|