When embedding perl it makes sense to be able to disable the behaviour
that $0 assignments tries to write to the memory of argv[0] and call
setproctitle(). The 'persistent' example in perlembed will for
instance segfault if you make it eval a file that assigns to $0. For
mod_perl you don't really want $0 assignments to hide apache from 'ps'.
I propose that we make the PL_origalen value 1 magical in that it
disables the magic behaviour of $0. PL_origalen is otherwise used by
perl to hold the amount of memory it thinks it can scribble in argv[0]
when $0 is assigned to. Objections?
The following patch implements this.
--Gisle
==== //depot/perl/mg.c#398 - /home/gisle/perl/blead/mg.c ====
Index: perl/mg.c
--- perl/mg.c.~1~ Tue Jan 10 10:44:47 2006
+++ perl/mg.c Tue Jan 10 10:44:47 2006
@@ -2540,7 +2540,7 @@
/* The BSDs don't show the argv[] in ps(1) output, they
* show a string from the process struct and provide
* the setproctitle() routine to manipulate that. */
- {
+ if (PL_origalen != 1) {
s = SvPV_const(sv, len);
# if __FreeBSD_version > 410001
/* The leading "-" removes the "perl: " prefix,
@@ -2561,7 +2561,7 @@
}
#endif
#if defined(__hpux) && defined(PSTAT_SETCMD)
- {
+ if (PL_origalen != 1) {
union pstun un;
s = SvPV_const(sv, len);
un.pst_command = (char *)s;
==== //depot/perl/perl.c#702 - /home/gisle/perl/blead/perl.c ====
Index: perl/perl.c
--- perl/perl.c.~1~ Tue Jan 10 10:44:47 2006
+++ perl/perl.c Tue Jan 10 10:44:47 2006
@@ -1439,7 +1439,10 @@
PL_origargc = argc;
PL_origargv = argv;
- {
+ if (PL_origalen != 0) {
+ PL_origalen = 1; /* don't use old PL_origalen is perl_parse() is called again */
+ }
+ else {
/* Set PL_origalen be the sum of the contiguous argv[]
* elements plus the size of the env in case that it is
* contiguous with the argv[]. This is used in mg.c:Perl_magic_set()
==== //depot/perl/pod/perlembed.pod#37 - /home/gisle/perl/blead/pod/perlembed.pod ====
Index: perl/pod/perlembed.pod
--- perl/pod/perlembed.pod.~1~ Tue Jan 10 10:44:47 2006
+++ perl/pod/perlembed.pod Tue Jan 10 10:44:47 2006
@@ -793,6 +793,7 @@
}
perl_construct(my_perl);
+ PL_origalen = 1; /* don't let $0 assignment update the proctitle or embedding[0] */
exitstatus = perl_parse(my_perl, NULL, 2, embedding, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
if(!exitstatus) {
@@ -852,6 +853,21 @@
to get the new behaviour. This also enables the running of END blocks if
the perl_parse fails and C<perl_destruct> will return the exit value.
+=head2 $0 assignments
+
+When a perl script assigns a value to $0 then the perl runtime will
+try to make this value show up as the program name reported by "ps" by
+updating the memory pointed to by the argv passed to perl_parse() and
+also calling API functions like setproctitle() where available. This
+behaviour might not be appropriate when embedding perl and can be
+disabled by assigning the value C<1> to the variable C<PL_origalen>
+before perl_parse() is called.
+
+The F<persistent.c> example above is for instance likely to segfault
+when $0 is assigned to if the C<PL_origalen = 1;> assignment is
+removed. This because perl will try to write to the read only memory
+of the C<embedding[]> strings.
+
=head2 Maintaining multiple interpreter instances
Some rare applications will need to create more than one interpreter
End of Patch.