BillEvansAtMariposa

20191212-02

Dec 12th, 2019
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. set -e
  4. rm -rf work
  5. mkdir work
  6. cd work
  7. cat <<EOD > launcher.c; cc -Wall -Werror launcher.c -o launcher
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. int main(int argc,
  13. char **argv,
  14. char **arge
  15. )
  16. {
  17. int wait_status;
  18.  
  19. size_t program_index;
  20.  
  21. pid_t fork_result;
  22.  
  23. char * programs[]={"./py2-demo",
  24. "./py3-demo",
  25. "./sh-demo",
  26. "./bash-demo",
  27. "./perl-demo",
  28. NULL
  29. };
  30.  
  31. argv[0]="jabberwocky";
  32.  
  33. for(program_index=0;
  34. programs[program_index];
  35. program_index++
  36. )
  37. {
  38. fork_result=fork();
  39.  
  40. if(fork_result==-1)
  41. {
  42. perror("fork()");
  43.  
  44. return 1;
  45. }
  46.  
  47. if(fork_result==0)
  48. {
  49. printf("launching %s\n",programs[program_index]);
  50.  
  51. execve(programs[program_index],argv,arge);
  52.  
  53. perror("execve()");
  54.  
  55. return 1;
  56. }
  57.  
  58. wait(&wait_status);
  59. }
  60.  
  61. return 0;
  62. }
  63. EOD
  64. cat <<EOD > py2-demo.c; cc -Wall -Werror py2-demo.c -o py2-demo
  65. #include <sys/types.h>
  66. #include <sys/wait.h>
  67. #include <stdio.h>
  68. #include <unistd.h>
  69. int main(int argc,
  70. char **argv,
  71. char **arge
  72. )
  73. {
  74. int wait_status;
  75.  
  76. pid_t fork_result;
  77.  
  78. argv[0]="jabberwocky";
  79.  
  80. fork_result=fork();
  81.  
  82. if(fork_result==-1)
  83. {
  84. perror("fork()");
  85.  
  86. return 1;
  87. }
  88.  
  89. if(fork_result==0)
  90. {
  91. execve("./py2-demo.py",argv,arge);
  92.  
  93. perror("execve()");
  94.  
  95. return 1;
  96. }
  97.  
  98. wait(&wait_status);
  99.  
  100. return 0;
  101. }
  102. EOD
  103. cat <<EOD > py2-demo.py; chmod 700 py2-demo.py
  104. #!/usr/bin/env python2
  105.  
  106. import os
  107. import subprocess
  108.  
  109. def get_arg0():
  110. return (subprocess.Popen(["ps","-p","%s" % os.getppid(),"-o","args="],
  111. stdout=subprocess.PIPE).stdout.read()
  112. .decode(encoding='latin1')
  113. .split(" ")[0])
  114. print get_arg0()
  115. EOD
  116. cat <<EOD > py3-demo.c; cc -Wall -Werror py3-demo.c -o py3-demo
  117. #include <sys/types.h>
  118. #include <sys/wait.h>
  119. #include <stdio.h>
  120. #include <unistd.h>
  121. int main(int argc,
  122. char **argv,
  123. char **arge
  124. )
  125. {
  126. int wait_status;
  127.  
  128. pid_t fork_result;
  129.  
  130. argv[0]="jabberwocky";
  131.  
  132. fork_result=fork();
  133.  
  134. if(fork_result==-1)
  135. {
  136. perror("fork()");
  137.  
  138. return 1;
  139. }
  140.  
  141. if(fork_result==0)
  142. {
  143. execve("./py3-demo.py",argv,arge);
  144.  
  145. perror("execve()");
  146.  
  147. return 1;
  148. }
  149.  
  150. wait(&wait_status);
  151.  
  152. return 0;
  153. }
  154. EOD
  155. cat <<EOD > py3-demo.py; chmod 700 py3-demo.py
  156. #!/usr/bin/env python3
  157.  
  158. import os
  159. import subprocess
  160.  
  161. def get_arg0():
  162. return subprocess.run("ps -p %s -o 'args='" % os.getppid(),
  163. shell=True,
  164. stdout=subprocess.PIPE,
  165. stderr=subprocess.PIPE
  166. ).stdout.decode(encoding='latin1').split(sep=" ")[0]
  167.  
  168. print(get_arg0())
  169. EOD
  170. cat <<EOD > sh-demo.c; cc -Wall -Werror sh-demo.c -o sh-demo
  171. #include <sys/types.h>
  172. #include <sys/wait.h>
  173. #include <stdio.h>
  174. #include <unistd.h>
  175. int main(int argc,
  176. char **argv,
  177. char **arge
  178. )
  179. {
  180. int wait_status;
  181.  
  182. pid_t fork_result;
  183.  
  184. argv[0]="jabberwocky";
  185.  
  186. fork_result=fork();
  187.  
  188. if(fork_result==-1)
  189. {
  190. perror("fork()");
  191.  
  192. return 1;
  193. }
  194.  
  195. if(fork_result==0)
  196. {
  197. execve("./sh-demo.sh",argv,arge);
  198.  
  199. perror("execve()");
  200.  
  201. return 1;
  202. }
  203.  
  204. wait(&wait_status);
  205.  
  206. return 0;
  207. }
  208. EOD
  209. cat <<EOD > sh-demo.sh; chmod 700 sh-demo.sh
  210. #!/bin/sh
  211.  
  212. get_arg0()
  213. {
  214. echo \$(ps -p \$PPID -o "args=" | awk '{print \$1}')
  215. }
  216.  
  217. echo \$(get_arg0)
  218. EOD
  219. cat <<EOD > bash-demo.c; cc -Wall -Werror bash-demo.c -o bash-demo
  220. #include <sys/types.h>
  221. #include <sys/wait.h>
  222. #include <stdio.h>
  223. #include <unistd.h>
  224. int main(int argc,
  225. char **argv,
  226. char **arge
  227. )
  228. {
  229. int wait_status;
  230.  
  231. pid_t fork_result;
  232.  
  233. argv[0]="jabberwocky";
  234.  
  235. fork_result=fork();
  236.  
  237. if(fork_result==-1)
  238. {
  239. perror("fork()");
  240.  
  241. return 1;
  242. }
  243.  
  244. if(fork_result==0)
  245. {
  246. execve("./bash-demo.sh",argv,arge);
  247.  
  248. perror("execve()");
  249.  
  250. return 1;
  251. }
  252.  
  253. wait(&wait_status);
  254.  
  255. return 0;
  256. }
  257. EOD
  258. cat <<EOD > bash-demo.sh; chmod 700 bash-demo.sh
  259. #!/bin/sh
  260.  
  261. get_arg0()
  262. {
  263. echo \$(ps -p \$PPID -o "args=" | awk '{print \$1}')
  264. }
  265.  
  266. echo \$(get_arg0)
  267. EOD
  268. cat <<EOD > perl-demo.c; cc -Wall -Werror perl-demo.c -o perl-demo
  269. #include <sys/types.h>
  270. #include <sys/wait.h>
  271. #include <stdio.h>
  272. #include <unistd.h>
  273. int main(int argc,
  274. char **argv,
  275. char **arge
  276. )
  277. {
  278. int wait_status;
  279.  
  280. pid_t fork_result;
  281.  
  282. argv[0]="jabberwocky";
  283.  
  284. fork_result=fork();
  285.  
  286. if(fork_result==-1)
  287. {
  288. perror("fork()");
  289.  
  290. return 1;
  291. }
  292.  
  293. if(fork_result==0)
  294. {
  295. execve("./perl-demo.pl",argv,arge);
  296.  
  297. perror("execve()");
  298.  
  299. return 1;
  300. }
  301.  
  302. wait(&wait_status);
  303.  
  304. return 0;
  305. }
  306. EOD
  307. cat <<EOD > perl-demo.pl; chmod 700 perl-demo.pl
  308. #!/usr/bin/env perl
  309.  
  310. use strict;
  311. use warnings FATAL=>"all";
  312.  
  313. sub get_arg0
  314. {
  315. my \$result;
  316. my \$parent=getppid();
  317. \$result=\`ps -p \$parent -o "args=" | awk '{print \\\$1}'\`;
  318.  
  319. chomp(\$result);
  320. return \$result;
  321. }
  322.  
  323. print(get_arg0,"\n");
  324. EOD
  325. ./launcher
Advertisement
Add Comment
Please, Sign In to add comment