Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- set -e
- rm -rf work
- mkdir work
- cd work
- cat <<EOD > launcher.c; cc -Wall -Werror launcher.c -o launcher
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- size_t program_index;
- pid_t fork_result;
- char * programs[]={"./py2-demo",
- "./py3-demo",
- "./sh-demo",
- "./bash-demo",
- "./perl-demo",
- NULL
- };
- argv[0]="jabberwocky";
- for(program_index=0;
- programs[program_index];
- program_index++
- )
- {
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- printf("launching %s\n",programs[program_index]);
- execve(programs[program_index],argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- }
- return 0;
- }
- EOD
- cat <<EOD > py2-demo.c; cc -Wall -Werror py2-demo.c -o py2-demo
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- pid_t fork_result;
- argv[0]="jabberwocky";
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- execve("./py2-demo.py",argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- return 0;
- }
- EOD
- cat <<EOD > py2-demo.py; chmod 700 py2-demo.py
- #!/usr/bin/env python2
- import os
- import subprocess
- def get_arg0():
- return (subprocess.Popen(["ps","-p","%s" % os.getppid(),"-o","args="],
- stdout=subprocess.PIPE).stdout.read()
- .decode(encoding='latin1')
- .split(" ")[0])
- print get_arg0()
- EOD
- cat <<EOD > py3-demo.c; cc -Wall -Werror py3-demo.c -o py3-demo
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- pid_t fork_result;
- argv[0]="jabberwocky";
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- execve("./py3-demo.py",argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- return 0;
- }
- EOD
- cat <<EOD > py3-demo.py; chmod 700 py3-demo.py
- #!/usr/bin/env python3
- import os
- import subprocess
- def get_arg0():
- return subprocess.run("ps -p %s -o 'args='" % os.getppid(),
- shell=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE
- ).stdout.decode(encoding='latin1').split(sep=" ")[0]
- print(get_arg0())
- EOD
- cat <<EOD > sh-demo.c; cc -Wall -Werror sh-demo.c -o sh-demo
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- pid_t fork_result;
- argv[0]="jabberwocky";
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- execve("./sh-demo.sh",argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- return 0;
- }
- EOD
- cat <<EOD > sh-demo.sh; chmod 700 sh-demo.sh
- #!/bin/sh
- get_arg0()
- {
- echo \$(ps -p \$PPID -o "args=" | awk '{print \$1}')
- }
- echo \$(get_arg0)
- EOD
- cat <<EOD > bash-demo.c; cc -Wall -Werror bash-demo.c -o bash-demo
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- pid_t fork_result;
- argv[0]="jabberwocky";
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- execve("./bash-demo.sh",argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- return 0;
- }
- EOD
- cat <<EOD > bash-demo.sh; chmod 700 bash-demo.sh
- #!/bin/sh
- get_arg0()
- {
- echo \$(ps -p \$PPID -o "args=" | awk '{print \$1}')
- }
- echo \$(get_arg0)
- EOD
- cat <<EOD > perl-demo.c; cc -Wall -Werror perl-demo.c -o perl-demo
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc,
- char **argv,
- char **arge
- )
- {
- int wait_status;
- pid_t fork_result;
- argv[0]="jabberwocky";
- fork_result=fork();
- if(fork_result==-1)
- {
- perror("fork()");
- return 1;
- }
- if(fork_result==0)
- {
- execve("./perl-demo.pl",argv,arge);
- perror("execve()");
- return 1;
- }
- wait(&wait_status);
- return 0;
- }
- EOD
- cat <<EOD > perl-demo.pl; chmod 700 perl-demo.pl
- #!/usr/bin/env perl
- use strict;
- use warnings FATAL=>"all";
- sub get_arg0
- {
- my \$result;
- my \$parent=getppid();
- \$result=\`ps -p \$parent -o "args=" | awk '{print \\\$1}'\`;
- chomp(\$result);
- return \$result;
- }
- print(get_arg0,"\n");
- EOD
- ./launcher
Advertisement
Add Comment
Please, Sign In to add comment