SPUG: assigning to $0; different Linuces

David Dyck david.dyck at fluke.com
Thu Aug 28 21:10:39 CDT 2003


On Thu, 28 Aug 2003 at 18:21 -0700, Ben Reser <ben at reser.org> wrote:

> On Thu, Aug 28, 2003 at 06:03:20PM -0700, Jeremy G Kahn wrote:
> > Has anybody else discovered that the ability to assign with $0 varies
> > across even Linux versions?

no :-)

> [snip]
>
> IIRC there are two different procps distributions.  Likely the
> difference is that Debian is using a different one than RedHat.

I agree with Ben.

> You might also compare the content of the various "files" in /proc/$$/
> Where $$ = your pid

Here's a quick example that uses the linux /proc filesystem
 (try may 5 proc)
that should work on both your linux systems

$ cmdline aaa bbb
 before:
cmdline=/usr/local/bin/perl\0-w\0/usr0/dcd/bin/cmdline\0aaa\0bbb\0
stat=cmdline
 after:
cmdline=foo
stat=cmdline

$ cat /usr0/dcd/bin/cmdline
#!/usr/local/bin/perl -w

sub getfile($)
{
    my $fname = shift;
    open FILE,"<$fname" || die "can't open $fname:$!\n";
    local $/;
    my $file=<FILE> || die "can't read $fname:$!\n";
    close FILE || die "can't close $fname:$!\n";
    # print "$fname=$file\n";
    return $file;
}

sub proc_cmdline($)
{
    my $pid = shift;
    my $cmdline=getfile("/proc/$pid/cmdline");
    $cmdline =~ s/\0/\\0/g;
    print "cmdline=$cmdline\n";
}

sub proc_stat($)
{
    my $pid = shift;
    my $stat=getfile("/proc/$pid/stat");
    if ($stat =~ m/\(([^)]*)\)/) {
	print "stat=$1\n";
    } else {
	print "stat: no match in $stat\n";
    }
}

sub show($)
{
    print " ",shift,":\n";
    proc_cmdline($$);
    proc_stat($$);
}

show "before";

$0="foo";

show "after";

__END__

man 5 proc

/dev/proc
            cmdline
                     This holds the complete command line for the
                     process,  unless  the whole process has been
                     swapped out, or unless the process is a zom­
                     bie.   In either of these later cases, there
                     is nothing in this file: i.e. a read on this
                     file  will return 0 characters.  The command
                     line arguments appear in this file as a  set
                     of  null-separated  strings,  with a further
                     null byte after the last string.

              stat   Status  information about the process.  This
                     is  used  by  ps(1).   It  is   defined   in
                     /usr/src/linux/fs/proc/array.c.

                     The  fields,  in  order,  with  their proper
                     scanf(3) format specifiers, are:

                      pid %d The process id.

                     comm %s
                             The filename of the  executable,  in
                             parentheses.     This   is   visible
                             whether or  not  the  executable  is
                             swapped out.







More information about the spug-list mailing list