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