From dea478855f8de7fe0974f5cbb5fa3f5968c46302 Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Fri, 9 Oct 2009 15:40:49 +0200
Subject: hush: support $PPID

function                                             old     new   delta
get_local_var_value                                   33      72     +39
hush_main                                            951     983     +32
block_signals                                        152     155      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 74/0)               Total: 74 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 shell/hush.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/shell/hush.c b/shell/hush.c
index ec38023ff..3028d795c 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -484,6 +484,7 @@ struct globals {
 	line_input_t *line_input_state;
 #endif
 	pid_t root_pid;
+	pid_t root_ppid;
 	pid_t last_bg_pid;
 #if ENABLE_HUSH_JOB
 	int run_list_level;
@@ -1307,6 +1308,9 @@ static const char *get_local_var_value(const char *name)
 	struct variable **pp = get_ptr_to_local_var(name);
 	if (pp)
 		return strchr((*pp)->varstr, '=') + 1;
+	if (strcmp(name, "PPID") == 0)
+		return utoa(G.root_ppid);
+	// bash compat: UID? EUID?
 	return NULL;
 }
 
@@ -2687,8 +2691,9 @@ static void re_execute_shell(char ***to_free, const char *s,
 	if (pp) while (*pp++)
 		cnt++;
 
-	sprintf(param_buf, "-$%x:%x:%x:%x" IF_HUSH_LOOPS(":%x")
+	sprintf(param_buf, "-$%x:%x:%x:%x:%x" IF_HUSH_LOOPS(":%x")
 			, (unsigned) G.root_pid
+			, (unsigned) G.root_ppid
 			, (unsigned) G.last_bg_pid
 			, (unsigned) G.last_exitcode
 			, cnt
@@ -6510,7 +6515,7 @@ int hush_main(int argc, char **argv)
 	 * MACHTYPE=i386-pc-linux-gnu
 	 * OSTYPE=linux-gnu
 	 * HOSTNAME=<xxxxxxxxxx>
-	 * PPID=<NNNNN>
+	 * PPID=<NNNNN> - we also do it elsewhere
 	 * EUID=<NNNNN>
 	 * UID=<NNNNN>
 	 * GROUPS=()
@@ -6587,8 +6592,10 @@ int hush_main(int argc, char **argv)
 			 * Note: this form never happens:
 			 * sh ... -c 'builtin' [BARGV...] ""
 			 */
-			if (!G.root_pid)
+			if (!G.root_pid) {
 				G.root_pid = getpid();
+				G.root_ppid = getppid();
+			}
 			G.global_argv = argv + optind;
 			G.global_argc = argc - optind;
 			if (builtin_argc) {
@@ -6630,6 +6637,8 @@ int hush_main(int argc, char **argv)
 		case '$':
 			G.root_pid = bb_strtou(optarg, &optarg, 16);
 			optarg++;
+			G.root_ppid = bb_strtou(optarg, &optarg, 16);
+			optarg++;
 			G.last_bg_pid = bb_strtou(optarg, &optarg, 16);
 			optarg++;
 			G.last_exitcode = bb_strtou(optarg, &optarg, 16);
@@ -6670,8 +6679,10 @@ int hush_main(int argc, char **argv)
 		}
 	} /* option parsing loop */
 
-	if (!G.root_pid)
+	if (!G.root_pid) {
 		G.root_pid = getpid();
+		G.root_ppid = getppid();
+	}
 
 	/* If we are login shell... */
 	if (argv[0] && argv[0][0] == '-') {
-- 
cgit v1.2.3-55-g6feb