xref: /linux/arch/um/os-Linux/helper.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <sched.h>
11 #include <limits.h>
12 #include <sys/signal.h>
13 #include <sys/wait.h>
14 #include "user.h"
15 #include "kern_util.h"
16 #include "user_util.h"
17 #include "os.h"
18 #include "um_malloc.h"
19 
20 struct helper_data {
21 	void (*pre_exec)(void*);
22 	void *pre_data;
23 	char **argv;
24 	int fd;
25 	char *buf;
26 };
27 
28 /* Debugging aid, changed only from gdb */
29 int helper_pause = 0;
30 
31 static void helper_hup(int sig)
32 {
33 }
34 
35 static int helper_child(void *arg)
36 {
37 	struct helper_data *data = arg;
38 	char **argv = data->argv;
39 	int errval;
40 
41 	if (helper_pause){
42 		signal(SIGHUP, helper_hup);
43 		pause();
44 	}
45 	if (data->pre_exec != NULL)
46 		(*data->pre_exec)(data->pre_data);
47 	errval = execvp_noalloc(data->buf, argv[0], argv);
48 	printk("helper_child - execvp of '%s' failed - errno = %d\n", argv[0], -errval);
49 	os_write_file(data->fd, &errval, sizeof(errval));
50 	kill(os_getpid(), SIGKILL);
51 	return 0;
52 }
53 
54 /* Returns either the pid of the child process we run or -E* on failure.
55  * XXX The alloc_stack here breaks if this is called in the tracing thread, so
56  * we need to receive a preallocated stack (a local buffer is ok). */
57 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
58 	       unsigned long *stack_out)
59 {
60 	struct helper_data data;
61 	unsigned long stack, sp;
62 	int pid, fds[2], ret, n;
63 
64 	if ((stack_out != NULL) && (*stack_out != 0))
65 		stack = *stack_out;
66 	else
67 		stack = alloc_stack(0, __cant_sleep());
68 	if (stack == 0)
69 		return -ENOMEM;
70 
71 	ret = os_pipe(fds, 1, 0);
72 	if (ret < 0) {
73 		printk("run_helper : pipe failed, ret = %d\n", -ret);
74 		goto out_free;
75 	}
76 
77 	ret = os_set_exec_close(fds[1], 1);
78 	if (ret < 0) {
79 		printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
80 		       -ret);
81 		goto out_close;
82 	}
83 
84 	sp = stack + page_size() - sizeof(void *);
85 	data.pre_exec = pre_exec;
86 	data.pre_data = pre_data;
87 	data.argv = argv;
88 	data.fd = fds[1];
89 	data.buf = __cant_sleep() ? um_kmalloc_atomic(PATH_MAX) :
90 					um_kmalloc(PATH_MAX);
91 	pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
92 	if (pid < 0) {
93 		ret = -errno;
94 		printk("run_helper : clone failed, errno = %d\n", errno);
95 		goto out_free2;
96 	}
97 
98 	close(fds[1]);
99 	fds[1] = -1;
100 
101 	/* Read the errno value from the child, if the exec failed, or get 0 if
102 	 * the exec succeeded because the pipe fd was set as close-on-exec. */
103 	n = os_read_file(fds[0], &ret, sizeof(ret));
104 	if (n == 0) {
105 		ret = pid;
106 	} else {
107 		if (n < 0) {
108 			printk("run_helper : read on pipe failed, ret = %d\n",
109 			       -n);
110 			ret = n;
111 			kill(pid, SIGKILL);
112 		}
113 		CATCH_EINTR(waitpid(pid, NULL, 0));
114 	}
115 
116 out_free2:
117 	kfree(data.buf);
118 out_close:
119 	if (fds[1] != -1)
120 		close(fds[1]);
121 	close(fds[0]);
122 out_free:
123 	if ((stack_out == NULL) || (*stack_out == 0))
124 		free_stack(stack, 0);
125 	return ret;
126 }
127 
128 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
129 		      unsigned long *stack_out, int stack_order)
130 {
131 	unsigned long stack, sp;
132 	int pid, status, err;
133 
134 	stack = alloc_stack(stack_order, __cant_sleep());
135 	if (stack == 0)
136 		return -ENOMEM;
137 
138 	sp = stack + (page_size() << stack_order) - sizeof(void *);
139 	pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
140 	if (pid < 0) {
141 		err = -errno;
142 		printk("run_helper_thread : clone failed, errno = %d\n",
143 		       errno);
144 		return err;
145 	}
146 	if (stack_out == NULL) {
147 		CATCH_EINTR(pid = waitpid(pid, &status, 0));
148 		if (pid < 0) {
149 			err = -errno;
150 			printk("run_helper_thread - wait failed, errno = %d\n",
151 			       errno);
152 			pid = err;
153 		}
154 		if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
155 			printk("run_helper_thread - thread returned status "
156 			       "0x%x\n", status);
157 		free_stack(stack, stack_order);
158 	} else
159 		*stack_out = stack;
160 	return pid;
161 }
162 
163 int helper_wait(int pid)
164 {
165 	int ret;
166 
167 	CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
168 	if (ret < 0) {
169 		ret = -errno;
170 		printk("helper_wait : waitpid failed, errno = %d\n", errno);
171 	}
172 	return ret;
173 }
174