xref: /openbmc/linux/arch/um/os-Linux/helper.c (revision f42b3800)
1 /*
2  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sched.h>
10 #include <sys/socket.h>
11 #include <sys/wait.h>
12 #include "kern_constants.h"
13 #include "kern_util.h"
14 #include "os.h"
15 #include "um_malloc.h"
16 #include "user.h"
17 
18 struct helper_data {
19 	void (*pre_exec)(void*);
20 	void *pre_data;
21 	char **argv;
22 	int fd;
23 	char *buf;
24 };
25 
26 static int helper_child(void *arg)
27 {
28 	struct helper_data *data = arg;
29 	char **argv = data->argv;
30 	int err;
31 
32 	if (data->pre_exec != NULL)
33 		(*data->pre_exec)(data->pre_data);
34 	err = execvp_noalloc(data->buf, argv[0], argv);
35 
36 	/* If the exec succeeds, we don't get here */
37 	write(data->fd, &err, sizeof(err));
38 
39 	return 0;
40 }
41 
42 /* Returns either the pid of the child process we run or -E* on failure. */
43 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
44 {
45 	struct helper_data data;
46 	unsigned long stack, sp;
47 	int pid, fds[2], ret, n;
48 
49 	stack = alloc_stack(0, __cant_sleep());
50 	if (stack == 0)
51 		return -ENOMEM;
52 
53 	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
54 	if (ret < 0) {
55 		ret = -errno;
56 		printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
57 		       errno);
58 		goto out_free;
59 	}
60 
61 	ret = os_set_exec_close(fds[1]);
62 	if (ret < 0) {
63 		printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
64 		       "ret = %d\n", -ret);
65 		goto out_close;
66 	}
67 
68 	sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
69 	data.pre_exec = pre_exec;
70 	data.pre_data = pre_data;
71 	data.argv = argv;
72 	data.fd = fds[1];
73 	data.buf = __cant_sleep() ? kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
74 					kmalloc(PATH_MAX, UM_GFP_KERNEL);
75 	pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
76 	if (pid < 0) {
77 		ret = -errno;
78 		printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
79 		       errno);
80 		goto out_free2;
81 	}
82 
83 	close(fds[1]);
84 	fds[1] = -1;
85 
86 	/*
87 	 * Read the errno value from the child, if the exec failed, or get 0 if
88 	 * the exec succeeded because the pipe fd was set as close-on-exec.
89 	 */
90 	n = read(fds[0], &ret, sizeof(ret));
91 	if (n == 0) {
92 		ret = pid;
93 	} else {
94 		if (n < 0) {
95 			n = -errno;
96 			printk(UM_KERN_ERR "run_helper : read on pipe failed, "
97 			       "ret = %d\n", -n);
98 			ret = n;
99 		}
100 		CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
101 	}
102 
103 out_free2:
104 	kfree(data.buf);
105 out_close:
106 	if (fds[1] != -1)
107 		close(fds[1]);
108 	close(fds[0]);
109 out_free:
110 	free_stack(stack, 0);
111 	return ret;
112 }
113 
114 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
115 		      unsigned long *stack_out)
116 {
117 	unsigned long stack, sp;
118 	int pid, status, err;
119 
120 	stack = alloc_stack(0, __cant_sleep());
121 	if (stack == 0)
122 		return -ENOMEM;
123 
124 	sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
125 	pid = clone(proc, (void *) sp, flags, arg);
126 	if (pid < 0) {
127 		err = -errno;
128 		printk(UM_KERN_ERR "run_helper_thread : clone failed, "
129 		       "errno = %d\n", errno);
130 		return err;
131 	}
132 	if (stack_out == NULL) {
133 		CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
134 		if (pid < 0) {
135 			err = -errno;
136 			printk(UM_KERN_ERR "run_helper_thread - wait failed, "
137 			       "errno = %d\n", errno);
138 			pid = err;
139 		}
140 		if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
141 			printk(UM_KERN_ERR "run_helper_thread - thread "
142 			       "returned status 0x%x\n", status);
143 		free_stack(stack, 0);
144 	} else
145 		*stack_out = stack;
146 	return pid;
147 }
148 
149 int helper_wait(int pid)
150 {
151 	int ret, status;
152 	int wflags = __WCLONE;
153 
154 	CATCH_EINTR(ret = waitpid(pid, &status, wflags));
155 	if (ret < 0) {
156 		printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
157 		       "errno = %d\n", pid, errno);
158 		return -errno;
159 	} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
160 		printk(UM_KERN_ERR "helper_wait : process %d exited with "
161 		       "status 0x%x\n", pid, status);
162 		return -ECHILD;
163 	} else
164 		return 0;
165 }
166