3 * Copyright (c) 1987-1999 Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "A ship then new they built for him/of mithril and of elven glass" --Bilbo
15 #define PERL_IN_PERL_C
17 #include "patchlevel.h" /* for local_patches */
19 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
24 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
25 char *getenv (char *); /* Usually in <stdlib.h> */
28 static I32 read_e_script(pTHXo_ int idx, SV *buf_sv, int maxlen);
43 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
50 #define perl_construct Perl_construct
51 #define perl_parse Perl_parse
52 #define perl_run Perl_run
53 #define perl_destruct Perl_destruct
54 #define perl_free Perl_free
57 #ifdef PERL_IMPLICIT_SYS
59 perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
60 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
61 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
62 struct IPerlDir* ipD, struct IPerlSock* ipS,
63 struct IPerlProc* ipP)
65 PerlInterpreter *my_perl;
67 my_perl = (PerlInterpreter*)new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd,
68 ipLIO, ipD, ipS, ipP);
69 PERL_SET_INTERP(my_perl);
71 /* New() needs interpreter, so call malloc() instead */
72 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
73 PERL_SET_INTERP(my_perl);
74 Zero(my_perl, 1, PerlInterpreter);
92 PerlInterpreter *my_perl;
94 /* New() needs interpreter, so call malloc() instead */
95 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
96 PERL_SET_INTERP(my_perl);
97 Zero(my_perl, 1, PerlInterpreter);
100 #endif /* PERL_IMPLICIT_SYS */
103 perl_construct(pTHXx)
108 struct perl_thread *thr = NULL;
109 #endif /* FAKE_THREADS */
110 #endif /* USE_THREADS */
114 PL_perl_destruct_level = 1;
116 if (PL_perl_destruct_level > 0)
120 /* Init the real globals (and main thread)? */
124 #ifdef ALLOC_THREAD_KEY
127 if (pthread_key_create(&PL_thr_key, 0))
128 Perl_croak(aTHX_ "panic: pthread_key_create");
130 MUTEX_INIT(&PL_sv_mutex);
132 * Safe to use basic SV functions from now on (though
133 * not things like mortals or tainting yet).
135 MUTEX_INIT(&PL_eval_mutex);
136 COND_INIT(&PL_eval_cond);
137 MUTEX_INIT(&PL_threads_mutex);
138 COND_INIT(&PL_nthreads_cond);
139 #ifdef EMULATE_ATOMIC_REFCOUNTS
140 MUTEX_INIT(&PL_svref_mutex);
141 #endif /* EMULATE_ATOMIC_REFCOUNTS */
143 MUTEX_INIT(&PL_cred_mutex);
145 thr = init_main_thread();
146 #endif /* USE_THREADS */
148 PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
150 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
152 PL_linestr = NEWSV(65,79);
153 sv_upgrade(PL_linestr,SVt_PVIV);
155 if (!SvREADONLY(&PL_sv_undef)) {
156 /* set read-only and try to insure than we wont see REFCNT==0
159 SvREADONLY_on(&PL_sv_undef);
160 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
162 sv_setpv(&PL_sv_no,PL_No);
164 SvREADONLY_on(&PL_sv_no);
165 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
167 sv_setpv(&PL_sv_yes,PL_Yes);
169 SvREADONLY_on(&PL_sv_yes);
170 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
175 /* PL_sighandlerp = sighandler; */
177 PL_sighandlerp = Perl_sighandler;
179 PL_pidstatus = newHV();
183 * There is no way we can refer to them from Perl so close them to save
184 * space. The other alternative would be to provide STDAUX and STDPRN
187 (void)fclose(stdaux);
188 (void)fclose(stdprn);
192 PL_nrs = newSVpvn("\n", 1);
193 PL_rs = SvREFCNT_inc(PL_nrs);
198 PL_lex_state = LEX_NOTPARSING;
204 SET_NUMERIC_STANDARD();
208 PL_patchlevel = NEWSV(0,4);
209 SvUPGRADE(PL_patchlevel, SVt_PVNV);
210 if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
211 SvGROW(PL_patchlevel,24);
212 s = (U8*)SvPVX(PL_patchlevel);
213 s = uv_to_utf8(s, (UV)PERL_REVISION);
214 s = uv_to_utf8(s, (UV)PERL_VERSION);
215 s = uv_to_utf8(s, (UV)PERL_SUBVERSION);
217 SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
218 SvPOK_on(PL_patchlevel);
219 SvNVX(PL_patchlevel) = (NV)PERL_REVISION
220 + ((NV)PERL_VERSION / (NV)1000)
221 #if defined(PERL_SUBVERSION) && PERL_SUBVERSION > 0
222 + ((NV)PERL_SUBVERSION / (NV)1000000)
225 SvNOK_on(PL_patchlevel); /* dual valued */
226 SvUTF8_on(PL_patchlevel);
227 SvREADONLY_on(PL_patchlevel);
230 #if defined(LOCAL_PATCH_COUNT)
231 PL_localpatches = local_patches; /* For possible -v */
234 PerlIO_init(); /* Hook to IO system */
236 PL_fdpid = newAV(); /* for remembering popen pids by fd */
237 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
246 int destruct_level; /* 0=none, 1=full, 2=full with checks */
252 #endif /* USE_THREADS */
254 /* wait for all pseudo-forked children to finish */
255 PERL_WAIT_FOR_CHILDREN;
259 /* Pass 1 on any remaining threads: detach joinables, join zombies */
261 MUTEX_LOCK(&PL_threads_mutex);
262 DEBUG_S(PerlIO_printf(Perl_debug_log,
263 "perl_destruct: waiting for %d threads...\n",
265 for (t = thr->next; t != thr; t = t->next) {
266 MUTEX_LOCK(&t->mutex);
267 switch (ThrSTATE(t)) {
270 DEBUG_S(PerlIO_printf(Perl_debug_log,
271 "perl_destruct: joining zombie %p\n", t));
272 ThrSETSTATE(t, THRf_DEAD);
273 MUTEX_UNLOCK(&t->mutex);
276 * The SvREFCNT_dec below may take a long time (e.g. av
277 * may contain an object scalar whose destructor gets
278 * called) so we have to unlock threads_mutex and start
281 MUTEX_UNLOCK(&PL_threads_mutex);
283 SvREFCNT_dec((SV*)av);
284 DEBUG_S(PerlIO_printf(Perl_debug_log,
285 "perl_destruct: joined zombie %p OK\n", t));
287 case THRf_R_JOINABLE:
288 DEBUG_S(PerlIO_printf(Perl_debug_log,
289 "perl_destruct: detaching thread %p\n", t));
290 ThrSETSTATE(t, THRf_R_DETACHED);
292 * We unlock threads_mutex and t->mutex in the opposite order
293 * from which we locked them just so that DETACH won't
294 * deadlock if it panics. It's only a breach of good style
295 * not a bug since they are unlocks not locks.
297 MUTEX_UNLOCK(&PL_threads_mutex);
299 MUTEX_UNLOCK(&t->mutex);
302 DEBUG_S(PerlIO_printf(Perl_debug_log,
303 "perl_destruct: ignoring %p (state %u)\n",
305 MUTEX_UNLOCK(&t->mutex);
306 /* fall through and out */
309 /* We leave the above "Pass 1" loop with threads_mutex still locked */
311 /* Pass 2 on remaining threads: wait for the thread count to drop to one */
312 while (PL_nthreads > 1)
314 DEBUG_S(PerlIO_printf(Perl_debug_log,
315 "perl_destruct: final wait for %d threads\n",
317 COND_WAIT(&PL_nthreads_cond, &PL_threads_mutex);
319 /* At this point, we're the last thread */
320 MUTEX_UNLOCK(&PL_threads_mutex);
321 DEBUG_S(PerlIO_printf(Perl_debug_log, "perl_destruct: armageddon has arrived\n"));
322 MUTEX_DESTROY(&PL_threads_mutex);
323 COND_DESTROY(&PL_nthreads_cond);
324 #endif /* !defined(FAKE_THREADS) */
325 #endif /* USE_THREADS */
327 destruct_level = PL_perl_destruct_level;
331 if (s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL")) {
333 if (destruct_level < i)
342 /* We must account for everything. */
344 /* Destroy the main CV and syntax tree */
346 PL_curpad = AvARRAY(PL_comppad);
347 op_free(PL_main_root);
348 PL_main_root = Nullop;
350 PL_curcop = &PL_compiling;
351 PL_main_start = Nullop;
352 SvREFCNT_dec(PL_main_cv);
356 if (PL_sv_objcount) {
358 * Try to destruct global references. We do this first so that the
359 * destructors and destructees still exist. Some sv's might remain.
360 * Non-referenced objects are on their own.
365 /* unhook hooks which will soon be, or use, destroyed data */
366 SvREFCNT_dec(PL_warnhook);
367 PL_warnhook = Nullsv;
368 SvREFCNT_dec(PL_diehook);
371 /* call exit list functions */
372 while (PL_exitlistlen-- > 0)
373 PL_exitlist[PL_exitlistlen].fn(aTHXo_ PL_exitlist[PL_exitlistlen].ptr);
375 Safefree(PL_exitlist);
377 if (destruct_level == 0){
379 DEBUG_P(debprofdump());
381 /* The exit() function will do everything that needs doing. */
385 /* loosen bonds of global variables */
388 (void)PerlIO_close(PL_rsfp);
392 /* Filters for program text */
393 SvREFCNT_dec(PL_rsfp_filters);
394 PL_rsfp_filters = Nullav;
397 PL_preprocess = FALSE;
403 PL_doswitches = FALSE;
404 PL_dowarn = G_WARN_OFF;
405 PL_doextract = FALSE;
406 PL_sawampersand = FALSE; /* must save all match strings */
409 Safefree(PL_inplace);
411 SvREFCNT_dec(PL_patchlevel);
414 SvREFCNT_dec(PL_e_script);
415 PL_e_script = Nullsv;
418 /* magical thingies */
420 Safefree(PL_ofs); /* $, */
423 Safefree(PL_ors); /* $\ */
426 SvREFCNT_dec(PL_rs); /* $/ */
429 SvREFCNT_dec(PL_nrs); /* $/ helper */
432 PL_multiline = 0; /* $* */
434 SvREFCNT_dec(PL_statname);
435 PL_statname = Nullsv;
438 /* defgv, aka *_ should be taken care of elsewhere */
440 /* clean up after study() */
441 SvREFCNT_dec(PL_lastscream);
442 PL_lastscream = Nullsv;
443 Safefree(PL_screamfirst);
445 Safefree(PL_screamnext);
449 Safefree(PL_efloatbuf);
450 PL_efloatbuf = Nullch;
453 /* startup and shutdown function lists */
454 SvREFCNT_dec(PL_beginav);
455 SvREFCNT_dec(PL_endav);
456 SvREFCNT_dec(PL_stopav);
457 SvREFCNT_dec(PL_initav);
463 /* shortcuts just get cleared */
469 PL_argvoutgv = Nullgv;
471 PL_stderrgv = Nullgv;
472 PL_last_in_gv = Nullgv;
474 PL_debstash = Nullhv;
476 /* reset so print() ends up where we expect */
479 SvREFCNT_dec(PL_argvout_stack);
480 PL_argvout_stack = Nullav;
482 SvREFCNT_dec(PL_fdpid);
484 SvREFCNT_dec(PL_modglobal);
485 PL_modglobal = Nullhv;
486 SvREFCNT_dec(PL_preambleav);
487 PL_preambleav = Nullav;
488 SvREFCNT_dec(PL_subname);
490 SvREFCNT_dec(PL_linestr);
492 SvREFCNT_dec(PL_pidstatus);
493 PL_pidstatus = Nullhv;
494 SvREFCNT_dec(PL_toptarget);
495 PL_toptarget = Nullsv;
496 SvREFCNT_dec(PL_bodytarget);
497 PL_bodytarget = Nullsv;
498 PL_formtarget = Nullsv;
500 /* clear utf8 character classes */
501 SvREFCNT_dec(PL_utf8_alnum);
502 SvREFCNT_dec(PL_utf8_alnumc);
503 SvREFCNT_dec(PL_utf8_ascii);
504 SvREFCNT_dec(PL_utf8_alpha);
505 SvREFCNT_dec(PL_utf8_space);
506 SvREFCNT_dec(PL_utf8_cntrl);
507 SvREFCNT_dec(PL_utf8_graph);
508 SvREFCNT_dec(PL_utf8_digit);
509 SvREFCNT_dec(PL_utf8_upper);
510 SvREFCNT_dec(PL_utf8_lower);
511 SvREFCNT_dec(PL_utf8_print);
512 SvREFCNT_dec(PL_utf8_punct);
513 SvREFCNT_dec(PL_utf8_xdigit);
514 SvREFCNT_dec(PL_utf8_mark);
515 SvREFCNT_dec(PL_utf8_toupper);
516 SvREFCNT_dec(PL_utf8_tolower);
517 PL_utf8_alnum = Nullsv;
518 PL_utf8_alnumc = Nullsv;
519 PL_utf8_ascii = Nullsv;
520 PL_utf8_alpha = Nullsv;
521 PL_utf8_space = Nullsv;
522 PL_utf8_cntrl = Nullsv;
523 PL_utf8_graph = Nullsv;
524 PL_utf8_digit = Nullsv;
525 PL_utf8_upper = Nullsv;
526 PL_utf8_lower = Nullsv;
527 PL_utf8_print = Nullsv;
528 PL_utf8_punct = Nullsv;
529 PL_utf8_xdigit = Nullsv;
530 PL_utf8_mark = Nullsv;
531 PL_utf8_toupper = Nullsv;
532 PL_utf8_totitle = Nullsv;
533 PL_utf8_tolower = Nullsv;
535 if (!specialWARN(PL_compiling.cop_warnings))
536 SvREFCNT_dec(PL_compiling.cop_warnings);
537 PL_compiling.cop_warnings = Nullsv;
539 /* Prepare to destruct main symbol table. */
544 SvREFCNT_dec(PL_curstname);
545 PL_curstname = Nullsv;
547 /* clear queued errors */
548 SvREFCNT_dec(PL_errors);
552 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
553 if (PL_scopestack_ix != 0)
554 Perl_warner(aTHX_ WARN_INTERNAL,
555 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
556 (long)PL_scopestack_ix);
557 if (PL_savestack_ix != 0)
558 Perl_warner(aTHX_ WARN_INTERNAL,
559 "Unbalanced saves: %ld more saves than restores\n",
560 (long)PL_savestack_ix);
561 if (PL_tmps_floor != -1)
562 Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced tmps: %ld more allocs than frees\n",
563 (long)PL_tmps_floor + 1);
564 if (cxstack_ix != -1)
565 Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced context: %ld more PUSHes than POPs\n",
566 (long)cxstack_ix + 1);
569 /* Now absolutely destruct everything, somehow or other, loops or no. */
571 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
572 while (PL_sv_count != 0 && PL_sv_count != last_sv_count) {
573 last_sv_count = PL_sv_count;
576 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
577 SvFLAGS(PL_strtab) |= SVt_PVHV;
579 /* Destruct the global string table. */
581 /* Yell and reset the HeVAL() slots that are still holding refcounts,
582 * so that sv_free() won't fail on them.
590 max = HvMAX(PL_strtab);
591 array = HvARRAY(PL_strtab);
594 if (hent && ckWARN_d(WARN_INTERNAL)) {
595 Perl_warner(aTHX_ WARN_INTERNAL,
596 "Unbalanced string table refcount: (%d) for \"%s\"",
597 HeVAL(hent) - Nullsv, HeKEY(hent));
598 HeVAL(hent) = Nullsv;
608 SvREFCNT_dec(PL_strtab);
610 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
611 Perl_warner(aTHX_ WARN_INTERNAL,"Scalars leaked: %ld\n", (long)PL_sv_count);
615 /* No SVs have survived, need to clean out */
616 Safefree(PL_origfilename);
617 Safefree(PL_reg_start_tmp);
619 Safefree(PL_reg_curpm);
620 Safefree(PL_reg_poscache);
621 Safefree(HeKEY_hek(&PL_hv_fetch_ent_mh));
622 Safefree(PL_op_mask);
624 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
626 DEBUG_P(debprofdump());
628 MUTEX_DESTROY(&PL_strtab_mutex);
629 MUTEX_DESTROY(&PL_sv_mutex);
630 MUTEX_DESTROY(&PL_eval_mutex);
631 MUTEX_DESTROY(&PL_cred_mutex);
632 COND_DESTROY(&PL_eval_cond);
633 #ifdef EMULATE_ATOMIC_REFCOUNTS
634 MUTEX_DESTROY(&PL_svref_mutex);
635 #endif /* EMULATE_ATOMIC_REFCOUNTS */
637 /* As the penultimate thing, free the non-arena SV for thrsv */
638 Safefree(SvPVX(PL_thrsv));
639 Safefree(SvANY(PL_thrsv));
642 #endif /* USE_THREADS */
644 /* As the absolutely last thing, free the non-arena SV for mess() */
647 /* it could have accumulated taint magic */
648 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
651 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
652 moremagic = mg->mg_moremagic;
653 if (mg->mg_ptr && mg->mg_type != 'g' && mg->mg_len >= 0)
654 Safefree(mg->mg_ptr);
658 /* we know that type >= SVt_PV */
659 SvOOK_off(PL_mess_sv);
660 Safefree(SvPVX(PL_mess_sv));
661 Safefree(SvANY(PL_mess_sv));
662 Safefree(PL_mess_sv);
670 #if defined(PERL_OBJECT)
678 Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
680 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
681 PL_exitlist[PL_exitlistlen].fn = fn;
682 PL_exitlist[PL_exitlistlen].ptr = ptr;
687 perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
697 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
700 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
701 setuid perl scripts securely.\n");
705 #if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
706 _dyld_lookup_and_bind
707 ("__environ", (unsigned long *) &environ_pointer, NULL);
712 #ifndef VMS /* VMS doesn't have environ array */
713 PL_origenviron = environ;
718 /* Come here if running an undumped a.out. */
720 PL_origfilename = savepv(argv[0]);
721 PL_do_undump = FALSE;
722 cxstack_ix = -1; /* start label stack again */
724 init_postdump_symbols(argc,argv,env);
729 PL_curpad = AvARRAY(PL_comppad);
730 op_free(PL_main_root);
731 PL_main_root = Nullop;
733 PL_main_start = Nullop;
734 SvREFCNT_dec(PL_main_cv);
738 oldscope = PL_scopestack_ix;
739 PL_dowarn = G_WARN_OFF;
741 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_parse_body),
746 call_list(oldscope, PL_stopav);
752 /* my_exit() was called */
753 while (PL_scopestack_ix > oldscope)
756 PL_curstash = PL_defstash;
758 call_list(oldscope, PL_stopav);
759 return STATUS_NATIVE_EXPORT;
761 PerlIO_printf(Perl_error_log, "panic: top_env\n");
768 S_parse_body(pTHX_ va_list args)
771 int argc = PL_origargc;
772 char **argv = PL_origargv;
773 char **env = va_arg(args, char**);
774 char *scriptname = NULL;
776 VOL bool dosearch = FALSE;
781 char *cddir = Nullch;
783 XSINIT_t xsinit = va_arg(args, XSINIT_t);
785 sv_setpvn(PL_linestr,"",0);
786 sv = newSVpvn("",0); /* first used for -I flags */
790 for (argc--,argv++; argc > 0; argc--,argv++) {
791 if (argv[0][0] != '-' || !argv[0][1])
795 validarg = " PHOOEY ";
802 #ifndef PERL_STRICT_CR
826 if (s = moreswitches(s))
836 if (PL_euid != PL_uid || PL_egid != PL_gid)
837 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
839 PL_e_script = newSVpvn("",0);
840 filter_add(read_e_script, NULL);
843 sv_catpv(PL_e_script, s);
845 sv_catpv(PL_e_script, argv[1]);
849 Perl_croak(aTHX_ "No code specified for -e");
850 sv_catpv(PL_e_script, "\n");
853 case 'I': /* -I handled both here and in moreswitches() */
855 if (!*++s && (s=argv[1]) != Nullch) {
860 STRLEN len = strlen(s);
863 sv_catpvn(sv, "-I", 2);
864 sv_catpvn(sv, p, len);
865 sv_catpvn(sv, " ", 1);
869 Perl_croak(aTHX_ "No directory specified for -I");
873 PL_preprocess = TRUE;
883 PL_preambleav = newAV();
884 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
886 PL_Sv = newSVpv("print myconfig();",0);
888 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
890 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
892 sv_catpv(PL_Sv,"\" Compile-time options:");
894 sv_catpv(PL_Sv," DEBUGGING");
897 sv_catpv(PL_Sv," MULTIPLICITY");
900 sv_catpv(PL_Sv," USE_THREADS");
903 sv_catpv(PL_Sv," USE_ITHREADS");
906 sv_catpv(PL_Sv," USE_64_BITS");
908 # ifdef USE_LONG_DOUBLE
909 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
912 sv_catpv(PL_Sv," USE_SOCKS");
915 sv_catpv(PL_Sv," PERL_OBJECT");
917 # ifdef PERL_IMPLICIT_CONTEXT
918 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
920 # ifdef PERL_IMPLICIT_SYS
921 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
923 sv_catpv(PL_Sv,"\\n\",");
925 #if defined(LOCAL_PATCH_COUNT)
926 if (LOCAL_PATCH_COUNT > 0) {
928 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
929 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
930 if (PL_localpatches[i])
931 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
935 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
938 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
940 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
945 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; \
946 print \" \\%ENV:\\n @env\\n\" if @env; \
947 print \" \\@INC:\\n @INC\\n\";");
950 PL_Sv = newSVpv("config_vars(qw(",0);
951 sv_catpv(PL_Sv, ++s);
952 sv_catpv(PL_Sv, "))");
955 av_push(PL_preambleav, PL_Sv);
956 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
967 if (!*++s || isSPACE(*s)) {
971 /* catch use of gnu style long options */
972 if (strEQ(s, "version")) {
976 if (strEQ(s, "help")) {
983 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
989 #ifndef SECURE_INTERNAL_GETENV
992 (s = PerlEnv_getenv("PERL5OPT")))
996 if (*s == '-' && *(s+1) == 'T')
1009 if (!strchr("DIMUdmw", *s))
1010 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
1011 s = moreswitches(s);
1017 scriptname = argv[0];
1020 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1022 else if (scriptname == Nullch) {
1024 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1032 open_script(scriptname,dosearch,sv,&fdscript);
1034 validate_suid(validarg, scriptname,fdscript);
1036 #if defined(SIGCHLD) || defined(SIGCLD)
1039 # define SIGCHLD SIGCLD
1041 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1042 if (sigstate == SIG_IGN) {
1043 if (ckWARN(WARN_SIGNAL))
1044 Perl_warner(aTHX_ WARN_SIGNAL,
1045 "Can't ignore signal CHLD, forcing to default");
1046 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1053 if (cddir && PerlDir_chdir(cddir) < 0)
1054 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1058 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1059 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1060 CvUNIQUE_on(PL_compcv);
1062 PL_comppad = newAV();
1063 av_push(PL_comppad, Nullsv);
1064 PL_curpad = AvARRAY(PL_comppad);
1065 PL_comppad_name = newAV();
1066 PL_comppad_name_fill = 0;
1067 PL_min_intro_pending = 0;
1070 av_store(PL_comppad_name, 0, newSVpvn("@_", 2));
1071 PL_curpad[0] = (SV*)newAV();
1072 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
1073 CvOWNER(PL_compcv) = 0;
1074 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1075 MUTEX_INIT(CvMUTEXP(PL_compcv));
1076 #endif /* USE_THREADS */
1078 comppadlist = newAV();
1079 AvREAL_off(comppadlist);
1080 av_store(comppadlist, 0, (SV*)PL_comppad_name);
1081 av_store(comppadlist, 1, (SV*)PL_comppad);
1082 CvPADLIST(PL_compcv) = comppadlist;
1084 boot_core_UNIVERSAL();
1085 boot_core_xsutils();
1088 (*xsinit)(aTHXo); /* in case linked C routines want magical variables */
1089 #if defined(VMS) || defined(WIN32) || defined(DJGPP)
1097 init_predump_symbols();
1098 /* init_postdump_symbols not currently designed to be called */
1099 /* more than once (ENV isn't cleared first, for example) */
1100 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
1102 init_postdump_symbols(argc,argv,env);
1106 /* now parse the script */
1108 SETERRNO(0,SS$_NORMAL);
1110 if (yyparse() || PL_error_count) {
1112 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
1114 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1118 CopLINE_set(PL_curcop, 0);
1119 PL_curstash = PL_defstash;
1120 PL_preprocess = FALSE;
1122 SvREFCNT_dec(PL_e_script);
1123 PL_e_script = Nullsv;
1126 /* now that script is parsed, we can modify record separator */
1127 SvREFCNT_dec(PL_rs);
1128 PL_rs = SvREFCNT_inc(PL_nrs);
1129 sv_setsv(get_sv("/", TRUE), PL_rs);
1134 SAVECOPFILE(PL_curcop);
1135 SAVECOPLINE(PL_curcop);
1136 gv_check(PL_defstash);
1143 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1144 dump_mstats("after compilation:");
1163 oldscope = PL_scopestack_ix;
1166 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_run_body), oldscope);
1169 cxstack_ix = -1; /* start context stack again */
1171 case 0: /* normal completion */
1172 case 2: /* my_exit() */
1173 while (PL_scopestack_ix > oldscope)
1176 PL_curstash = PL_defstash;
1177 if (PL_endav && !PL_minus_c)
1178 call_list(oldscope, PL_endav);
1180 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1181 dump_mstats("after execution: ");
1183 return STATUS_NATIVE_EXPORT;
1186 POPSTACK_TO(PL_mainstack);
1189 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1199 S_run_body(pTHX_ va_list args)
1202 I32 oldscope = va_arg(args, I32);
1204 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
1205 PL_sawampersand ? "Enabling" : "Omitting"));
1207 if (!PL_restartop) {
1208 DEBUG_x(dump_all());
1209 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
1210 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1214 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
1217 if (PERLDB_SINGLE && PL_DBsingle)
1218 sv_setiv(PL_DBsingle, 1);
1220 call_list(oldscope, PL_initav);
1226 PL_op = PL_restartop;
1230 else if (PL_main_start) {
1231 CvDEPTH(PL_main_cv) = 1;
1232 PL_op = PL_main_start;
1242 Perl_get_sv(pTHX_ const char *name, I32 create)
1246 if (name[1] == '\0' && !isALPHA(name[0])) {
1247 PADOFFSET tmp = find_threadsv(name);
1248 if (tmp != NOT_IN_PAD) {
1250 return THREADSV(tmp);
1253 #endif /* USE_THREADS */
1254 gv = gv_fetchpv(name, create, SVt_PV);
1261 Perl_get_av(pTHX_ const char *name, I32 create)
1263 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1272 Perl_get_hv(pTHX_ const char *name, I32 create)
1274 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1283 Perl_get_cv(pTHX_ const char *name, I32 create)
1285 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
1286 /* XXX unsafe for threads if eval_owner isn't held */
1287 /* XXX this is probably not what they think they're getting.
1288 * It has the same effect as "sub name;", i.e. just a forward
1290 if (create && !GvCVu(gv))
1291 return newSUB(start_subparse(FALSE, 0),
1292 newSVOP(OP_CONST, 0, newSVpv(name,0)),
1300 /* Be sure to refetch the stack pointer after calling these routines. */
1303 Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
1305 /* See G_* flags in cop.h */
1306 /* null terminated arg list */
1313 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
1318 return call_pv(sub_name, flags);
1322 Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
1323 /* name of the subroutine */
1324 /* See G_* flags in cop.h */
1326 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
1330 Perl_call_method(pTHX_ const char *methname, I32 flags)
1331 /* name of the subroutine */
1332 /* See G_* flags in cop.h */
1338 XPUSHs(sv_2mortal(newSVpv(methname,0)));
1343 return call_sv(*PL_stack_sp--, flags);
1346 /* May be called with any of a CV, a GV, or an SV containing the name. */
1348 Perl_call_sv(pTHX_ SV *sv, I32 flags)
1350 /* See G_* flags in cop.h */
1353 LOGOP myop; /* fake syntax tree node */
1357 bool oldcatch = CATCH_GET;
1362 if (flags & G_DISCARD) {
1367 Zero(&myop, 1, LOGOP);
1368 myop.op_next = Nullop;
1369 if (!(flags & G_NOARGS))
1370 myop.op_flags |= OPf_STACKED;
1371 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1372 (flags & G_ARRAY) ? OPf_WANT_LIST :
1377 EXTEND(PL_stack_sp, 1);
1378 *++PL_stack_sp = sv;
1380 oldscope = PL_scopestack_ix;
1382 if (PERLDB_SUB && PL_curstash != PL_debstash
1383 /* Handle first BEGIN of -d. */
1384 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
1385 /* Try harder, since this may have been a sighandler, thus
1386 * curstash may be meaningless. */
1387 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
1388 && !(flags & G_NODEBUG))
1389 PL_op->op_private |= OPpENTERSUB_DB;
1391 if (!(flags & G_EVAL)) {
1393 call_xbody((OP*)&myop, FALSE);
1394 retval = PL_stack_sp - (PL_stack_base + oldmark);
1395 CATCH_SET(oldcatch);
1398 cLOGOP->op_other = PL_op;
1400 /* we're trying to emulate pp_entertry() here */
1402 register PERL_CONTEXT *cx;
1403 I32 gimme = GIMME_V;
1408 push_return(PL_op->op_next);
1409 PUSHBLOCK(cx, CXt_EVAL, PL_stack_sp);
1411 PL_eval_root = PL_op; /* Only needed so that goto works right. */
1413 PL_in_eval = EVAL_INEVAL;
1414 if (flags & G_KEEPERR)
1415 PL_in_eval |= EVAL_KEEPERR;
1422 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_call_body),
1426 retval = PL_stack_sp - (PL_stack_base + oldmark);
1427 if (!(flags & G_KEEPERR))
1434 /* my_exit() was called */
1435 PL_curstash = PL_defstash;
1437 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
1438 Perl_croak(aTHX_ "Callback called exit");
1443 PL_op = PL_restartop;
1447 PL_stack_sp = PL_stack_base + oldmark;
1448 if (flags & G_ARRAY)
1452 *++PL_stack_sp = &PL_sv_undef;
1457 if (PL_scopestack_ix > oldscope) {
1461 register PERL_CONTEXT *cx;
1472 if (flags & G_DISCARD) {
1473 PL_stack_sp = PL_stack_base + oldmark;
1483 S_call_body(pTHX_ va_list args)
1485 OP *myop = va_arg(args, OP*);
1486 int is_eval = va_arg(args, int);
1488 call_xbody(myop, is_eval);
1493 S_call_xbody(pTHX_ OP *myop, int is_eval)
1497 if (PL_op == myop) {
1499 PL_op = Perl_pp_entereval(aTHX);
1501 PL_op = Perl_pp_entersub(aTHX);
1507 /* Eval a string. The G_EVAL flag is always assumed. */
1510 Perl_eval_sv(pTHX_ SV *sv, I32 flags)
1512 /* See G_* flags in cop.h */
1515 UNOP myop; /* fake syntax tree node */
1516 I32 oldmark = SP - PL_stack_base;
1523 if (flags & G_DISCARD) {
1530 Zero(PL_op, 1, UNOP);
1531 EXTEND(PL_stack_sp, 1);
1532 *++PL_stack_sp = sv;
1533 oldscope = PL_scopestack_ix;
1535 if (!(flags & G_NOARGS))
1536 myop.op_flags = OPf_STACKED;
1537 myop.op_next = Nullop;
1538 myop.op_type = OP_ENTEREVAL;
1539 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1540 (flags & G_ARRAY) ? OPf_WANT_LIST :
1542 if (flags & G_KEEPERR)
1543 myop.op_flags |= OPf_SPECIAL;
1546 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_call_body),
1550 retval = PL_stack_sp - (PL_stack_base + oldmark);
1551 if (!(flags & G_KEEPERR))
1558 /* my_exit() was called */
1559 PL_curstash = PL_defstash;
1561 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
1562 Perl_croak(aTHX_ "Callback called exit");
1567 PL_op = PL_restartop;
1571 PL_stack_sp = PL_stack_base + oldmark;
1572 if (flags & G_ARRAY)
1576 *++PL_stack_sp = &PL_sv_undef;
1581 if (flags & G_DISCARD) {
1582 PL_stack_sp = PL_stack_base + oldmark;
1592 Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
1595 SV* sv = newSVpv(p, 0);
1598 eval_sv(sv, G_SCALAR);
1605 if (croak_on_error && SvTRUE(ERRSV)) {
1607 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
1613 /* Require a module. */
1616 Perl_require_pv(pTHX_ const char *pv)
1620 PUSHSTACKi(PERLSI_REQUIRE);
1622 sv = sv_newmortal();
1623 sv_setpv(sv, "require '");
1626 eval_sv(sv, G_DISCARD);
1632 Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
1636 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
1637 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1641 S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
1643 /* This message really ought to be max 23 lines.
1644 * Removed -h because the user already knows that opton. Others? */
1646 static char *usage_msg[] = {
1647 "-0[octal] specify record separator (\\0, if no argument)",
1648 "-a autosplit mode with -n or -p (splits $_ into @F)",
1649 "-c check syntax only (runs BEGIN and END blocks)",
1650 "-d[:debugger] run program under debugger",
1651 "-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
1652 "-e 'command' one line of program (several -e's allowed, omit programfile)",
1653 "-F/pattern/ split() pattern for -a switch (//'s are optional)",
1654 "-i[extension] edit <> files in place (makes backup if extension supplied)",
1655 "-Idirectory specify @INC/#include directory (several -I's allowed)",
1656 "-l[octal] enable line ending processing, specifies line terminator",
1657 "-[mM][-]module execute `use/no module...' before executing program",
1658 "-n assume 'while (<>) { ... }' loop around program",
1659 "-p assume loop like -n but print line also, like sed",
1660 "-P run program through C preprocessor before compilation",
1661 "-s enable rudimentary parsing for switches after programfile",
1662 "-S look for programfile using PATH environment variable",
1663 "-T enable tainting checks",
1664 "-u dump core after parsing program",
1665 "-U allow unsafe operations",
1666 "-v print version, subversion (includes VERY IMPORTANT perl info)",
1667 "-V[:variable] print configuration summary (or a single Config.pm variable)",
1668 "-w enable many useful warnings (RECOMMENDED)",
1669 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
1673 char **p = usage_msg;
1675 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
1677 printf("\n %s", *p++);
1680 /* This routine handles any switches that can be given during run */
1683 Perl_moreswitches(pTHX_ char *s)
1692 rschar = (U32)scan_oct(s, 4, &numlen);
1693 SvREFCNT_dec(PL_nrs);
1694 if (rschar & ~((U8)~0))
1695 PL_nrs = &PL_sv_undef;
1696 else if (!rschar && numlen >= 2)
1697 PL_nrs = newSVpvn("", 0);
1700 PL_nrs = newSVpvn(&ch, 1);
1706 PL_splitstr = savepv(s + 1);
1720 if (*s == ':' || *s == '=') {
1721 my_setenv("PERL5DB", Perl_form(aTHX_ "use Devel::%s;", ++s));
1725 PL_perldb = PERLDB_ALL;
1733 if (isALPHA(s[1])) {
1734 static char debopts[] = "psltocPmfrxuLHXDS";
1737 for (s++; *s && (d = strchr(debopts,*s)); s++)
1738 PL_debug |= 1 << (d - debopts);
1741 PL_debug = atoi(s+1);
1742 for (s++; isDIGIT(*s); s++) ;
1744 PL_debug |= 0x80000000;
1747 if (ckWARN_d(WARN_DEBUGGING))
1748 Perl_warner(aTHX_ WARN_DEBUGGING,
1749 "Recompile perl with -DDEBUGGING to use -D switch\n");
1750 for (s++; isALNUM(*s); s++) ;
1756 usage(PL_origargv[0]);
1760 Safefree(PL_inplace);
1761 PL_inplace = savepv(s+1);
1763 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
1766 if (*s == '-') /* Additional switches on #! line. */
1770 case 'I': /* -I handled both here and in parse_perl() */
1773 while (*s && isSPACE(*s))
1778 /* ignore trailing spaces (possibly followed by other switches) */
1780 for (e = p; *e && !isSPACE(*e); e++) ;
1784 } while (*p && *p != '-');
1785 e = savepvn(s, e-s);
1793 Perl_croak(aTHX_ "No directory specified for -I");
1801 PL_ors = savepv("\n");
1803 *PL_ors = (char)scan_oct(s, 3 + (*s == '0'), &numlen);
1808 if (RsPARA(PL_nrs)) {
1813 PL_ors = SvPV(PL_nrs, PL_orslen);
1814 PL_ors = savepvn(PL_ors, PL_orslen);
1818 forbid_setid("-M"); /* XXX ? */
1821 forbid_setid("-m"); /* XXX ? */
1826 /* -M-foo == 'no foo' */
1827 if (*s == '-') { use = "no "; ++s; }
1828 sv = newSVpv(use,0);
1830 /* We allow -M'Module qw(Foo Bar)' */
1831 while(isALNUM(*s) || *s==':') ++s;
1833 sv_catpv(sv, start);
1834 if (*(start-1) == 'm') {
1836 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
1837 sv_catpv( sv, " ()");
1840 sv_catpvn(sv, start, s-start);
1841 sv_catpv(sv, " split(/,/,q{");
1847 PL_preambleav = newAV();
1848 av_push(PL_preambleav, sv);
1851 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
1863 PL_doswitches = TRUE;
1868 Perl_croak(aTHX_ "Too late for \"-T\" option");
1872 PL_do_undump = TRUE;
1880 printf("\nThis is perl, v%"UVuf".%"UVuf".%"UVuf" built for %s",
1881 (UV)PERL_REVISION, (UV)PERL_VERSION, (UV)PERL_SUBVERSION, ARCHNAME);
1882 #if defined(LOCAL_PATCH_COUNT)
1883 if (LOCAL_PATCH_COUNT > 0)
1884 printf("\n(with %d registered patch%s, see perl -V for more detail)",
1885 (int)LOCAL_PATCH_COUNT, (LOCAL_PATCH_COUNT!=1) ? "es" : "");
1888 printf("\n\nCopyright 1987-1999, Larry Wall\n");
1890 printf("\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
1893 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
1894 printf("djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
1897 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
1898 "Version 5 port Copyright (c) 1994-1999, Andreas Kaiser, Ilya Zakharevich\n");
1901 printf("atariST series port, ++jrb bammi@cadence.com\n");
1904 printf("BeOS port Copyright Tom Spindler, 1997-1999\n");
1907 printf("MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-1999\n");
1910 printf("MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
1913 printf("Stratus VOS port by Paul_Green@stratus.com, 1997-1999\n");
1916 printf("VM/ESA port by Neale Ferguson, 1998-1999\n");
1919 printf("BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
1922 printf("MiNT port by Guido Flohr, 1997-1999\n");
1924 #ifdef BINARY_BUILD_NOTICE
1925 BINARY_BUILD_NOTICE;
1928 Perl may be copied only under the terms of either the Artistic License or the\n\
1929 GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n\
1930 Complete documentation for Perl, including FAQ lists, should be found on\n\
1931 this system using `man perl' or `perldoc perl'. If you have access to the\n\
1932 Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
1935 if (! (PL_dowarn & G_WARN_ALL_MASK))
1936 PL_dowarn |= G_WARN_ON;
1940 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
1941 PL_compiling.cop_warnings = WARN_ALL ;
1945 PL_dowarn = G_WARN_ALL_OFF;
1946 PL_compiling.cop_warnings = WARN_NONE ;
1951 if (s[1] == '-') /* Additional switches on #! line. */
1956 #if defined(WIN32) || !defined(PERL_STRICT_CR)
1962 #ifdef ALTERNATE_SHEBANG
1963 case 'S': /* OS/2 needs -S on "extproc" line. */
1971 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
1976 /* compliments of Tom Christiansen */
1978 /* unexec() can be found in the Gnu emacs distribution */
1979 /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
1982 Perl_my_unexec(pTHX)
1990 prog = newSVpv(BIN_EXP, 0);
1991 sv_catpv(prog, "/perl");
1992 file = newSVpv(PL_origfilename, 0);
1993 sv_catpv(file, ".perldump");
1995 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
1996 /* unexec prints msg to stderr in case of failure */
1997 PerlProc_exit(status);
2000 # include <lib$routines.h>
2001 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
2003 ABORT(); /* for use with undump */
2008 /* initialize curinterp */
2013 #ifdef PERL_OBJECT /* XXX kludge */
2016 PL_chopset = " \n-"; \
2017 PL_copline = NOLINE; \
2018 PL_curcop = &PL_compiling;\
2019 PL_curcopdb = NULL; \
2021 PL_dumpindent = 4; \
2022 PL_laststatval = -1; \
2023 PL_laststype = OP_STAT; \
2024 PL_maxscream = -1; \
2025 PL_maxsysfd = MAXSYSFD; \
2026 PL_statname = Nullsv; \
2027 PL_tmps_floor = -1; \
2029 PL_op_mask = NULL; \
2030 PL_laststatval = -1; \
2031 PL_laststype = OP_STAT; \
2032 PL_mess_sv = Nullsv; \
2033 PL_splitstr = " "; \
2034 PL_generation = 100; \
2035 PL_exitlist = NULL; \
2036 PL_exitlistlen = 0; \
2038 PL_in_clean_objs = FALSE; \
2039 PL_in_clean_all = FALSE; \
2040 PL_profiledata = NULL; \
2042 PL_rsfp_filters = Nullav; \
2047 # ifdef MULTIPLICITY
2048 # define PERLVAR(var,type)
2049 # define PERLVARA(var,n,type)
2050 # if defined(PERL_IMPLICIT_CONTEXT)
2051 # if defined(USE_THREADS)
2052 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2053 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2054 # else /* !USE_THREADS */
2055 # define PERLVARI(var,type,init) aTHX->var = init;
2056 # define PERLVARIC(var,type,init) aTHX->var = init;
2057 # endif /* USE_THREADS */
2059 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2060 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2062 # include "intrpvar.h"
2063 # ifndef USE_THREADS
2064 # include "thrdvar.h"
2071 # define PERLVAR(var,type)
2072 # define PERLVARA(var,n,type)
2073 # define PERLVARI(var,type,init) PL_##var = init;
2074 # define PERLVARIC(var,type,init) PL_##var = init;
2075 # include "intrpvar.h"
2076 # ifndef USE_THREADS
2077 # include "thrdvar.h"
2089 S_init_main_stash(pTHX)
2094 /* Note that strtab is a rather special HV. Assumptions are made
2095 about not iterating on it, and not adding tie magic to it.
2096 It is properly deallocated in perl_destruct() */
2097 PL_strtab = newHV();
2099 MUTEX_INIT(&PL_strtab_mutex);
2101 HvSHAREKEYS_off(PL_strtab); /* mandatory */
2102 hv_ksplit(PL_strtab, 512);
2104 PL_curstash = PL_defstash = newHV();
2105 PL_curstname = newSVpvn("main",4);
2106 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2107 SvREFCNT_dec(GvHV(gv));
2108 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
2110 HvNAME(PL_defstash) = savepv("main");
2111 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2112 GvMULTI_on(PL_incgv);
2113 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2114 GvMULTI_on(PL_hintgv);
2115 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2116 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2117 GvMULTI_on(PL_errgv);
2118 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2119 GvMULTI_on(PL_replgv);
2120 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
2121 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2122 sv_setpvn(ERRSV, "", 0);
2123 PL_curstash = PL_defstash;
2124 CopSTASH_set(&PL_compiling, PL_defstash);
2125 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
2126 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
2127 /* We must init $/ before switches are processed. */
2128 sv_setpvn(get_sv("/", TRUE), "\n", 1);
2132 S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
2140 PL_origfilename = savepv("-e");
2143 /* if find_script() returns, it returns a malloc()-ed value */
2144 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
2146 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2147 char *s = scriptname + 8;
2148 *fdscript = atoi(s);
2152 scriptname = savepv(s + 1);
2153 Safefree(PL_origfilename);
2154 PL_origfilename = scriptname;
2159 CopFILE_set(PL_curcop, PL_origfilename);
2160 if (strEQ(PL_origfilename,"-"))
2162 if (*fdscript >= 0) {
2163 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
2164 #if defined(HAS_FCNTL) && defined(F_SETFD)
2166 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1); /* ensure close-on-exec */
2169 else if (PL_preprocess) {
2170 char *cpp_cfg = CPPSTDIN;
2171 SV *cpp = newSVpvn("",0);
2172 SV *cmd = NEWSV(0,0);
2174 if (strEQ(cpp_cfg, "cppstdin"))
2175 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
2176 sv_catpv(cpp, cpp_cfg);
2178 sv_catpvn(sv, "-I", 2);
2179 sv_catpv(sv,PRIVLIB_EXP);
2182 Perl_sv_setpvf(aTHX_ cmd, "\
2183 sed %s -e \"/^[^#]/b\" \
2184 -e \"/^#[ ]*include[ ]/b\" \
2185 -e \"/^#[ ]*define[ ]/b\" \
2186 -e \"/^#[ ]*if[ ]/b\" \
2187 -e \"/^#[ ]*ifdef[ ]/b\" \
2188 -e \"/^#[ ]*ifndef[ ]/b\" \
2189 -e \"/^#[ ]*else/b\" \
2190 -e \"/^#[ ]*elif[ ]/b\" \
2191 -e \"/^#[ ]*undef[ ]/b\" \
2192 -e \"/^#[ ]*endif/b\" \
2195 (PL_doextract ? "-e \"1,/^#/d\n\"" : ""),
2198 Perl_sv_setpvf(aTHX_ cmd, "\
2199 %s %s -e '/^[^#]/b' \
2200 -e '/^#[ ]*include[ ]/b' \
2201 -e '/^#[ ]*define[ ]/b' \
2202 -e '/^#[ ]*if[ ]/b' \
2203 -e '/^#[ ]*ifdef[ ]/b' \
2204 -e '/^#[ ]*ifndef[ ]/b' \
2205 -e '/^#[ ]*else/b' \
2206 -e '/^#[ ]*elif[ ]/b' \
2207 -e '/^#[ ]*undef[ ]/b' \
2208 -e '/^#[ ]*endif/b' \
2212 Perl_sv_setpvf(aTHX_ cmd, "\
2213 %s %s -e '/^[^#]/b' \
2214 -e '/^#[ ]*include[ ]/b' \
2215 -e '/^#[ ]*define[ ]/b' \
2216 -e '/^#[ ]*if[ ]/b' \
2217 -e '/^#[ ]*ifdef[ ]/b' \
2218 -e '/^#[ ]*ifndef[ ]/b' \
2219 -e '/^#[ ]*else/b' \
2220 -e '/^#[ ]*elif[ ]/b' \
2221 -e '/^#[ ]*undef[ ]/b' \
2222 -e '/^#[ ]*endif/b' \
2231 (PL_doextract ? "-e '1,/^#/d\n'" : ""),
2233 scriptname, cpp, sv, CPPMINUS);
2234 PL_doextract = FALSE;
2235 #ifdef IAMSUID /* actually, this is caught earlier */
2236 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2238 (void)seteuid(PL_uid); /* musn't stay setuid root */
2241 (void)setreuid((Uid_t)-1, PL_uid);
2243 #ifdef HAS_SETRESUID
2244 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2246 PerlProc_setuid(PL_uid);
2250 if (PerlProc_geteuid() != PL_uid)
2251 Perl_croak(aTHX_ "Can't do seteuid!\n");
2253 #endif /* IAMSUID */
2254 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
2258 else if (!*scriptname) {
2259 forbid_setid("program input from stdin");
2260 PL_rsfp = PerlIO_stdin();
2263 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
2264 #if defined(HAS_FCNTL) && defined(F_SETFD)
2266 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1); /* ensure close-on-exec */
2271 #ifndef IAMSUID /* in case script is not readable before setuid */
2273 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2274 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2277 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
2278 (int)PERL_REVISION, (int)PERL_VERSION,
2279 (int)PERL_SUBVERSION), PL_origargv);
2280 Perl_croak(aTHX_ "Can't do setuid\n");
2284 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
2285 CopFILE(PL_curcop), Strerror(errno));
2290 * I_SYSSTATVFS HAS_FSTATVFS
2292 * I_STATFS HAS_FSTATFS
2293 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
2294 * here so that metaconfig picks them up. */
2298 S_fd_on_nosuid_fs(pTHX_ int fd)
2300 int check_okay = 0; /* able to do all the required sys/libcalls */
2301 int on_nosuid = 0; /* the fd is on a nosuid fs */
2303 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
2304 * fstatvfs() is UNIX98.
2305 * fstatfs() is 4.3 BSD.
2306 * ustat()+getmnt() is pre-4.3 BSD.
2307 * getmntent() is O(number-of-mounted-filesystems) and can hang on
2308 * an irrelevant filesystem while trying to reach the right one.
2311 # ifdef HAS_FSTATVFS
2312 struct statvfs stfs;
2313 check_okay = fstatvfs(fd, &stfs) == 0;
2314 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
2316 # ifdef PERL_MOUNT_NOSUID
2317 # if defined(HAS_FSTATFS) && \
2318 defined(HAS_STRUCT_STATFS) && \
2319 defined(HAS_STRUCT_STATFS_F_FLAGS)
2321 check_okay = fstatfs(fd, &stfs) == 0;
2322 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
2324 # if defined(HAS_FSTAT) && \
2325 defined(HAS_USTAT) && \
2326 defined(HAS_GETMNT) && \
2327 defined(HAS_STRUCT_FS_DATA) && \
2330 if (fstat(fd, &fdst) == 0) {
2332 if (ustat(fdst.st_dev, &us) == 0) {
2334 /* NOSTAT_ONE here because we're not examining fields which
2335 * vary between that case and STAT_ONE. */
2336 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
2337 size_t cmplen = sizeof(us.f_fname);
2338 if (sizeof(fsd.fd_req.path) < cmplen)
2339 cmplen = sizeof(fsd.fd_req.path);
2340 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
2341 fdst.st_dev == fsd.fd_req.dev) {
2343 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
2349 # endif /* fstat+ustat+getmnt */
2350 # endif /* fstatfs */
2352 # if defined(HAS_GETMNTENT) && \
2353 defined(HAS_HASMNTOPT) && \
2354 defined(MNTOPT_NOSUID)
2355 FILE *mtab = fopen("/etc/mtab", "r");
2356 struct mntent *entry;
2357 struct stat stb, fsb;
2359 if (mtab && (fstat(fd, &stb) == 0)) {
2360 while (entry = getmntent(mtab)) {
2361 if (stat(entry->mnt_dir, &fsb) == 0
2362 && fsb.st_dev == stb.st_dev)
2364 /* found the filesystem */
2366 if (hasmntopt(entry, MNTOPT_NOSUID))
2369 } /* A single fs may well fail its stat(). */
2374 # endif /* getmntent+hasmntopt */
2375 # endif /* PERL_MOUNT_NOSUID: fstatfs or fstat+ustat+statfs */
2376 # endif /* statvfs */
2379 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
2382 #endif /* IAMSUID */
2385 S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
2389 /* do we need to emulate setuid on scripts? */
2391 /* This code is for those BSD systems that have setuid #! scripts disabled
2392 * in the kernel because of a security problem. Merely defining DOSUID
2393 * in perl will not fix that problem, but if you have disabled setuid
2394 * scripts in the kernel, this will attempt to emulate setuid and setgid
2395 * on scripts that have those now-otherwise-useless bits set. The setuid
2396 * root version must be called suidperl or sperlN.NNN. If regular perl
2397 * discovers that it has opened a setuid script, it calls suidperl with
2398 * the same argv that it had. If suidperl finds that the script it has
2399 * just opened is NOT setuid root, it sets the effective uid back to the
2400 * uid. We don't just make perl setuid root because that loses the
2401 * effective uid we had before invoking perl, if it was different from the
2404 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
2405 * be defined in suidperl only. suidperl must be setuid root. The
2406 * Configure script will set this up for you if you want it.
2413 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
2414 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
2415 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
2420 #ifndef HAS_SETREUID
2421 /* On this access check to make sure the directories are readable,
2422 * there is actually a small window that the user could use to make
2423 * filename point to an accessible directory. So there is a faint
2424 * chance that someone could execute a setuid script down in a
2425 * non-accessible directory. I don't know what to do about that.
2426 * But I don't think it's too important. The manual lies when
2427 * it says access() is useful in setuid programs.
2429 if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/
2430 Perl_croak(aTHX_ "Permission denied");
2432 /* If we can swap euid and uid, then we can determine access rights
2433 * with a simple stat of the file, and then compare device and
2434 * inode to make sure we did stat() on the same file we opened.
2435 * Then we just have to make sure he or she can execute it.
2438 struct stat tmpstatbuf;
2442 setreuid(PL_euid,PL_uid) < 0
2445 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
2448 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
2449 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
2450 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0)
2451 Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */
2452 #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
2453 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp)))
2454 Perl_croak(aTHX_ "Permission denied");
2456 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
2457 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
2458 (void)PerlIO_close(PL_rsfp);
2459 if (PL_rsfp = PerlProc_popen("/bin/mail root","w")) { /* heh, heh */
2460 PerlIO_printf(PL_rsfp,
2461 "User %"Uid_t_f" tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
2462 (Filename of set-id script was %s, uid %"Uid_t_f" gid %"Gid_t_f".)\n\nSincerely,\nperl\n",
2463 PL_uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
2464 (long)PL_statbuf.st_dev, (long)PL_statbuf.st_ino,
2466 PL_statbuf.st_uid, PL_statbuf.st_gid);
2467 (void)PerlProc_pclose(PL_rsfp);
2469 Perl_croak(aTHX_ "Permission denied\n");
2473 setreuid(PL_uid,PL_euid) < 0
2475 # if defined(HAS_SETRESUID)
2476 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
2479 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
2480 Perl_croak(aTHX_ "Can't reswap uid and euid");
2481 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
2482 Perl_croak(aTHX_ "Permission denied\n");
2484 #endif /* HAS_SETREUID */
2485 #endif /* IAMSUID */
2487 if (!S_ISREG(PL_statbuf.st_mode))
2488 Perl_croak(aTHX_ "Permission denied");
2489 if (PL_statbuf.st_mode & S_IWOTH)
2490 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
2491 PL_doswitches = FALSE; /* -s is insecure in suid */
2492 CopLINE_inc(PL_curcop);
2493 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
2494 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
2495 Perl_croak(aTHX_ "No #! line");
2496 s = SvPV(PL_linestr,n_a)+2;
2498 while (!isSPACE(*s)) s++;
2499 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
2500 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
2501 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
2502 Perl_croak(aTHX_ "Not a perl script");
2503 while (*s == ' ' || *s == '\t') s++;
2505 * #! arg must be what we saw above. They can invoke it by
2506 * mentioning suidperl explicitly, but they may not add any strange
2507 * arguments beyond what #! says if they do invoke suidperl that way.
2509 len = strlen(validarg);
2510 if (strEQ(validarg," PHOOEY ") ||
2511 strnNE(s,validarg,len) || !isSPACE(s[len]))
2512 Perl_croak(aTHX_ "Args must match #! line");
2515 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
2516 PL_euid == PL_statbuf.st_uid)
2518 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
2519 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2520 #endif /* IAMSUID */
2522 if (PL_euid) { /* oops, we're not the setuid root perl */
2523 (void)PerlIO_close(PL_rsfp);
2526 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
2527 (int)PERL_REVISION, (int)PERL_VERSION,
2528 (int)PERL_SUBVERSION), PL_origargv);
2530 Perl_croak(aTHX_ "Can't do setuid\n");
2533 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
2535 (void)setegid(PL_statbuf.st_gid);
2538 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
2540 #ifdef HAS_SETRESGID
2541 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
2543 PerlProc_setgid(PL_statbuf.st_gid);
2547 if (PerlProc_getegid() != PL_statbuf.st_gid)
2548 Perl_croak(aTHX_ "Can't do setegid!\n");
2550 if (PL_statbuf.st_mode & S_ISUID) {
2551 if (PL_statbuf.st_uid != PL_euid)
2553 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
2556 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
2558 #ifdef HAS_SETRESUID
2559 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
2561 PerlProc_setuid(PL_statbuf.st_uid);
2565 if (PerlProc_geteuid() != PL_statbuf.st_uid)
2566 Perl_croak(aTHX_ "Can't do seteuid!\n");
2568 else if (PL_uid) { /* oops, mustn't run as root */
2570 (void)seteuid((Uid_t)PL_uid);
2573 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
2575 #ifdef HAS_SETRESUID
2576 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
2578 PerlProc_setuid((Uid_t)PL_uid);
2582 if (PerlProc_geteuid() != PL_uid)
2583 Perl_croak(aTHX_ "Can't do seteuid!\n");
2586 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
2587 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
2590 else if (PL_preprocess)
2591 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
2592 else if (fdscript >= 0)
2593 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
2595 Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n");
2597 /* We absolutely must clear out any saved ids here, so we */
2598 /* exec the real perl, substituting fd script for scriptname. */
2599 /* (We pass script name as "subdir" of fd, which perl will grok.) */
2600 PerlIO_rewind(PL_rsfp);
2601 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
2602 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
2603 if (!PL_origargv[which])
2604 Perl_croak(aTHX_ "Permission denied");
2605 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
2606 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
2607 #if defined(HAS_FCNTL) && defined(F_SETFD)
2608 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
2610 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
2611 (int)PERL_REVISION, (int)PERL_VERSION,
2612 (int)PERL_SUBVERSION), PL_origargv);/* try again */
2613 Perl_croak(aTHX_ "Can't do setuid\n");
2614 #endif /* IAMSUID */
2616 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
2617 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
2619 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
2620 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
2622 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
2625 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
2626 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2627 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
2628 /* not set-id, must be wrapped */
2634 S_find_beginning(pTHX)
2636 register char *s, *s2;
2638 /* skip forward in input to the real script? */
2641 while (PL_doextract) {
2642 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
2643 Perl_croak(aTHX_ "No Perl script found in input\n");
2644 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
2645 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
2646 PL_doextract = FALSE;
2647 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2649 while (*s == ' ' || *s == '\t') s++;
2651 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2652 if (strnEQ(s2-4,"perl",4))
2654 while (s = moreswitches(s)) ;
2664 PL_uid = PerlProc_getuid();
2665 PL_euid = PerlProc_geteuid();
2666 PL_gid = PerlProc_getgid();
2667 PL_egid = PerlProc_getegid();
2669 PL_uid |= PL_gid << 16;
2670 PL_euid |= PL_egid << 16;
2672 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2676 S_forbid_setid(pTHX_ char *s)
2678 if (PL_euid != PL_uid)
2679 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
2680 if (PL_egid != PL_gid)
2681 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
2685 Perl_init_debugger(pTHX)
2688 HV *ostash = PL_curstash;
2690 PL_curstash = PL_debstash;
2691 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
2692 AvREAL_off(PL_dbargs);
2693 PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2694 PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
2695 PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2696 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
2697 PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
2698 sv_setiv(PL_DBsingle, 0);
2699 PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
2700 sv_setiv(PL_DBtrace, 0);
2701 PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
2702 sv_setiv(PL_DBsignal, 0);
2703 PL_curstash = ostash;
2706 #ifndef STRESS_REALLOC
2707 #define REASONABLE(size) (size)
2709 #define REASONABLE(size) (1) /* unreasonable */
2713 Perl_init_stacks(pTHX)
2715 /* start with 128-item stack and 8K cxstack */
2716 PL_curstackinfo = new_stackinfo(REASONABLE(128),
2717 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
2718 PL_curstackinfo->si_type = PERLSI_MAIN;
2719 PL_curstack = PL_curstackinfo->si_stack;
2720 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
2722 PL_stack_base = AvARRAY(PL_curstack);
2723 PL_stack_sp = PL_stack_base;
2724 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
2726 New(50,PL_tmps_stack,REASONABLE(128),SV*);
2729 PL_tmps_max = REASONABLE(128);
2731 New(54,PL_markstack,REASONABLE(32),I32);
2732 PL_markstack_ptr = PL_markstack;
2733 PL_markstack_max = PL_markstack + REASONABLE(32);
2737 New(54,PL_scopestack,REASONABLE(32),I32);
2738 PL_scopestack_ix = 0;
2739 PL_scopestack_max = REASONABLE(32);
2741 New(54,PL_savestack,REASONABLE(128),ANY);
2742 PL_savestack_ix = 0;
2743 PL_savestack_max = REASONABLE(128);
2745 New(54,PL_retstack,REASONABLE(16),OP*);
2747 PL_retstack_max = REASONABLE(16);
2756 while (PL_curstackinfo->si_next)
2757 PL_curstackinfo = PL_curstackinfo->si_next;
2758 while (PL_curstackinfo) {
2759 PERL_SI *p = PL_curstackinfo->si_prev;
2760 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
2761 Safefree(PL_curstackinfo->si_cxstack);
2762 Safefree(PL_curstackinfo);
2763 PL_curstackinfo = p;
2765 Safefree(PL_tmps_stack);
2766 Safefree(PL_markstack);
2767 Safefree(PL_scopestack);
2768 Safefree(PL_savestack);
2769 Safefree(PL_retstack);
2773 static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
2784 lex_start(PL_linestr);
2786 PL_subname = newSVpvn("main",4);
2790 S_init_predump_symbols(pTHX)
2797 sv_setpvn(get_sv("\"", TRUE), " ", 1);
2798 PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
2799 GvMULTI_on(PL_stdingv);
2800 io = GvIOp(PL_stdingv);
2801 IoIFP(io) = PerlIO_stdin();
2802 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
2804 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
2806 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
2809 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
2811 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
2813 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
2815 PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
2816 GvMULTI_on(PL_stderrgv);
2817 io = GvIOp(PL_stderrgv);
2818 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
2819 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
2821 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
2823 PL_statname = NEWSV(66,0); /* last filename we did stat on */
2826 PL_osname = savepv(OSNAME);
2830 S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env)
2837 argc--,argv++; /* skip name of script */
2838 if (PL_doswitches) {
2839 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2842 if (argv[0][1] == '-' && !argv[0][2]) {
2846 if (s = strchr(argv[0], '=')) {
2848 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
2851 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
2854 PL_toptarget = NEWSV(0,0);
2855 sv_upgrade(PL_toptarget, SVt_PVFM);
2856 sv_setpvn(PL_toptarget, "", 0);
2857 PL_bodytarget = NEWSV(0,0);
2858 sv_upgrade(PL_bodytarget, SVt_PVFM);
2859 sv_setpvn(PL_bodytarget, "", 0);
2860 PL_formtarget = PL_bodytarget;
2863 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
2864 sv_setpv(GvSV(tmpgv),PL_origfilename);
2865 magicname("0", "0", 1);
2867 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
2869 sv_setpv(GvSV(tmpgv), os2_execname());
2871 sv_setpv(GvSV(tmpgv),PL_origargv[0]);
2873 if (PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
2874 GvMULTI_on(PL_argvgv);
2875 (void)gv_AVadd(PL_argvgv);
2876 av_clear(GvAVn(PL_argvgv));
2877 for (; argc > 0; argc--,argv++) {
2878 av_push(GvAVn(PL_argvgv),newSVpv(argv[0],0));
2881 if (PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
2883 GvMULTI_on(PL_envgv);
2884 hv = GvHVn(PL_envgv);
2885 hv_magic(hv, PL_envgv, 'E');
2886 #if !defined( VMS) && !defined(EPOC) /* VMS doesn't have environ array */
2887 /* Note that if the supplied env parameter is actually a copy
2888 of the global environ then it may now point to free'd memory
2889 if the environment has been modified since. To avoid this
2890 problem we treat env==NULL as meaning 'use the default'
2895 environ[0] = Nullch;
2896 for (; *env; env++) {
2897 if (!(s = strchr(*env,'=')))
2903 sv = newSVpv(s--,0);
2904 (void)hv_store(hv, *env, s - *env, sv, 0);
2906 #if defined(__BORLANDC__) && defined(USE_WIN32_RTL_ENV)
2907 /* Sins of the RTL. See note in my_setenv(). */
2908 (void)PerlEnv_putenv(savepv(*env));
2912 #ifdef DYNAMIC_ENV_FETCH
2913 HvNAME(hv) = savepv(ENV_HV_NAME);
2917 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2918 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
2922 S_init_perllib(pTHX)
2927 s = PerlEnv_getenv("PERL5LIB");
2931 incpush(PerlEnv_getenv("PERLLIB"), FALSE);
2933 /* Treat PERL5?LIB as a possible search list logical name -- the
2934 * "natural" VMS idiom for a Unix path string. We allow each
2935 * element to be a set of |-separated directories for compatibility.
2939 if (my_trnlnm("PERL5LIB",buf,0))
2940 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
2942 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
2946 /* Use the ~-expanded versions of APPLLIB (undocumented),
2947 ARCHLIB PRIVLIB SITEARCH and SITELIB
2950 incpush(APPLLIB_EXP, TRUE);
2954 incpush(ARCHLIB_EXP, FALSE);
2957 #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
2960 incpush(PRIVLIB_EXP, TRUE);
2962 incpush(PRIVLIB_EXP, FALSE);
2966 incpush(SITEARCH_EXP, FALSE);
2970 incpush(SITELIB_EXP, TRUE);
2972 incpush(SITELIB_EXP, FALSE);
2975 #if defined(PERL_VENDORLIB_EXP)
2977 incpush(PERL_VENDORLIB_EXP, TRUE);
2979 incpush(PERL_VENDORLIB_EXP, FALSE);
2983 incpush(".", FALSE);
2987 # define PERLLIB_SEP ';'
2990 # define PERLLIB_SEP '|'
2992 # define PERLLIB_SEP ':'
2995 #ifndef PERLLIB_MANGLE
2996 # define PERLLIB_MANGLE(s,n) (s)
3000 S_incpush(pTHX_ char *p, int addsubdirs)
3002 SV *subdir = Nullsv;
3008 subdir = sv_newmortal();
3011 /* Break at all separators */
3013 SV *libdir = NEWSV(55,0);
3016 /* skip any consecutive separators */
3017 while ( *p == PERLLIB_SEP ) {
3018 /* Uncomment the next line for PATH semantics */
3019 /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
3023 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
3024 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
3029 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
3030 p = Nullch; /* break out */
3034 * BEFORE pushing libdir onto @INC we may first push version- and
3035 * archname-specific sub-directories.
3038 struct stat tmpstatbuf;
3043 if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) {
3045 while (unix[len-1] == '/') len--; /* Cosmetic */
3046 sv_usepvn(libdir,unix,len);
3049 PerlIO_printf(Perl_error_log,
3050 "Failed to unixify @INC element \"%s\"\n",
3053 /* .../archname/version if -d .../archname/version/auto */
3054 Perl_sv_setpvf(aTHX_ subdir, "%_/%s/"PERL_FS_VER_FMT"/auto", libdir,
3055 ARCHNAME, (int)PERL_REVISION,
3056 (int)PERL_VERSION, (int)PERL_SUBVERSION);
3057 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3058 S_ISDIR(tmpstatbuf.st_mode))
3059 av_push(GvAVn(PL_incgv),
3060 newSVpvn(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
3062 /* .../archname if -d .../archname/auto */
3063 Perl_sv_setpvf(aTHX_ subdir, "%_/%s/auto", libdir, ARCHNAME);
3064 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3065 S_ISDIR(tmpstatbuf.st_mode))
3066 av_push(GvAVn(PL_incgv),
3067 newSVpvn(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
3070 /* finally push this lib directory on the end of @INC */
3071 av_push(GvAVn(PL_incgv), libdir);
3076 STATIC struct perl_thread *
3077 S_init_main_thread(pTHX)
3079 #if !defined(PERL_IMPLICIT_CONTEXT)
3080 struct perl_thread *thr;
3084 Newz(53, thr, 1, struct perl_thread);
3085 PL_curcop = &PL_compiling;
3086 thr->interp = PERL_GET_INTERP;
3087 thr->cvcache = newHV();
3088 thr->threadsv = newAV();
3089 /* thr->threadsvp is set when find_threadsv is called */
3090 thr->specific = newAV();
3091 thr->flags = THRf_R_JOINABLE;
3092 MUTEX_INIT(&thr->mutex);
3093 /* Handcraft thrsv similarly to mess_sv */
3094 New(53, PL_thrsv, 1, SV);
3095 Newz(53, xpv, 1, XPV);
3096 SvFLAGS(PL_thrsv) = SVt_PV;
3097 SvANY(PL_thrsv) = (void*)xpv;
3098 SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */
3099 SvPVX(PL_thrsv) = (char*)thr;
3100 SvCUR_set(PL_thrsv, sizeof(thr));
3101 SvLEN_set(PL_thrsv, sizeof(thr));
3102 *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */
3103 thr->oursv = PL_thrsv;
3104 PL_chopset = " \n-";
3107 MUTEX_LOCK(&PL_threads_mutex);
3112 MUTEX_UNLOCK(&PL_threads_mutex);
3114 #ifdef HAVE_THREAD_INTERN
3115 Perl_init_thread_intern(thr);
3118 #ifdef SET_THREAD_SELF
3119 SET_THREAD_SELF(thr);
3121 thr->self = pthread_self();
3122 #endif /* SET_THREAD_SELF */
3126 * These must come after the SET_THR because sv_setpvn does
3127 * SvTAINT and the taint fields require dTHR.
3129 PL_toptarget = NEWSV(0,0);
3130 sv_upgrade(PL_toptarget, SVt_PVFM);
3131 sv_setpvn(PL_toptarget, "", 0);
3132 PL_bodytarget = NEWSV(0,0);
3133 sv_upgrade(PL_bodytarget, SVt_PVFM);
3134 sv_setpvn(PL_bodytarget, "", 0);
3135 PL_formtarget = PL_bodytarget;
3136 thr->errsv = newSVpvn("", 0);
3137 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
3140 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3141 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3142 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3143 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3144 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
3146 PL_reginterp_cnt = 0;
3150 #endif /* USE_THREADS */
3153 Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
3157 line_t oldline = CopLINE(PL_curcop);
3163 while (AvFILL(paramList) >= 0) {
3164 cv = (CV*)av_shift(paramList);
3166 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_call_list_body), cv);
3170 (void)SvPV(atsv, len);
3173 PL_curcop = &PL_compiling;
3174 CopLINE_set(PL_curcop, oldline);
3175 if (paramList == PL_beginav)
3176 sv_catpv(atsv, "BEGIN failed--compilation aborted");
3178 Perl_sv_catpvf(aTHX_ atsv,
3179 "%s failed--call queue aborted",
3180 paramList == PL_stopav ? "STOP"
3181 : paramList == PL_initav ? "INIT"
3183 while (PL_scopestack_ix > oldscope)
3185 Perl_croak(aTHX_ "%s", SvPVx(atsv, n_a));
3192 /* my_exit() was called */
3193 while (PL_scopestack_ix > oldscope)
3196 PL_curstash = PL_defstash;
3197 PL_curcop = &PL_compiling;
3198 CopLINE_set(PL_curcop, oldline);
3199 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) {
3200 if (paramList == PL_beginav)
3201 Perl_croak(aTHX_ "BEGIN failed--compilation aborted");
3203 Perl_croak(aTHX_ "%s failed--call queue aborted",
3204 paramList == PL_stopav ? "STOP"
3205 : paramList == PL_initav ? "INIT"
3212 PL_curcop = &PL_compiling;
3213 CopLINE_set(PL_curcop, oldline);
3216 PerlIO_printf(Perl_error_log, "panic: restartop\n");
3224 S_call_list_body(pTHX_ va_list args)
3227 CV *cv = va_arg(args, CV*);
3229 PUSHMARK(PL_stack_sp);
3230 call_sv((SV*)cv, G_EVAL|G_DISCARD);
3235 Perl_my_exit(pTHX_ U32 status)
3239 DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
3240 thr, (unsigned long) status));
3249 STATUS_NATIVE_SET(status);
3256 Perl_my_failure_exit(pTHX)
3259 if (vaxc$errno & 1) {
3260 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
3261 STATUS_NATIVE_SET(44);
3264 if (!vaxc$errno && errno) /* unlikely */
3265 STATUS_NATIVE_SET(44);
3267 STATUS_NATIVE_SET(vaxc$errno);
3272 STATUS_POSIX_SET(errno);
3274 exitstatus = STATUS_POSIX >> 8;
3275 if (exitstatus & 255)
3276 STATUS_POSIX_SET(exitstatus);
3278 STATUS_POSIX_SET(255);
3285 S_my_exit_jump(pTHX)
3288 register PERL_CONTEXT *cx;
3293 SvREFCNT_dec(PL_e_script);
3294 PL_e_script = Nullsv;
3297 POPSTACK_TO(PL_mainstack);
3298 if (cxstack_ix >= 0) {
3301 POPBLOCK(cx,PL_curpm);
3313 read_e_script(pTHXo_ int idx, SV *buf_sv, int maxlen)
3316 p = SvPVX(PL_e_script);
3317 nl = strchr(p, '\n');
3318 nl = (nl) ? nl+1 : SvEND(PL_e_script);
3320 filter_del(read_e_script);
3323 sv_catpvn(buf_sv, p, nl-p);
3324 sv_chop(PL_e_script, nl);