summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_bridge.c
blob: e187f11bb96a07a1733ba8dbe0324a8324362027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/***********************************************************/
/*  ntux: native translation und extension                 */
/*  Copyright (C) 2016--2022  SysDeer Technologies, LLC    */
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
/***********************************************************/

#include <ntapi/nt_termios.h>

#include <psxabi/sys_sysapi.h>
#include <psxabi/sys_fcntl.h>
#include <psxabi/sys_errno.h>
#include <psxabi/sys_process.h>
#include <psxxfi/xfi_framework.h>

#include <ntux/ntux.h>
#include "ntux_driver_impl.h"
#include "ntux_nolibc_impl.h"
#include "ntux_errinfo_impl.h"

int ntux_cmd_bridge(const struct ntux_driver_ctx * dctx)
{
	int		ret;
	int		val;
	pid_t		pid;
	int		argc;
	const char **	argv;
	const char **	envp;
	unsigned char * program;
	unsigned char * logfile;
	ssize_t		rbytes;
	ssize_t		wbytes;
	char *		ch;
	char *		script;
	char *		interp;
	const char *	usrscript;
	char **		sargv;
	const char **	pargv;
	const char **	aargv;
	char		buf[2048];
	int		fdlog[2];
	int32_t		status;

	struct ntux_fd_ctx fdctx;

	/* init */
	ntux_driver_set_ectx(
		dctx,0,
		dctx->cctx->sargv[0]);

	if (ntux_get_driver_fdctx(dctx,&fdctx) < 0)
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

	if (dctx->cctx->interp) {
		ret = __xfi_framework_get_int32_slot_value(
			PSX_RTDATA_UDAT32_ARGV1_IS_OPTARG,
			&val);

		if (ret < 0)
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_CUSTOM_ERROR(
					dctx,
					NTUX_ERR_FLOW_ERROR);

		for (sargv=dctx->cctx->sargv; *sargv; sargv++)
			(void)0;

		argc = sargv - dctx->cctx->sargv;

		if (!(aargv = ntux_calloc(argc + 3,sizeof(char *))))
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

		if (!(script = ntux_calloc(1,NTUX_MAX_PATH)))
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

		if (!(interp = ntux_calloc(1,NTUX_MAX_PATH)))
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

		usrscript = val ? dctx->cctx->sargv[2] : dctx->cctx->sargv[1];

		if (__sys_fs_npath(fdctx.fdcwd,usrscript,0,script,NTUX_MAX_PATH) < 0)
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

		if (__sys_fs_npath(fdctx.fdcwd,dctx->cctx->interp,0,interp,NTUX_MAX_PATH) < 0)
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);

		if (val) {
			aargv[0] = interp;
			aargv[1] = dctx->cctx->sargv[1];
			aargv[2] = script;

			sargv    = &dctx->cctx->sargv[3];
			pargv    = &aargv[3];
		} else {
			aargv[0] = interp;
			aargv[1] = script;

			sargv    = &dctx->cctx->sargv[2];
			pargv    = &aargv[2];
		}

		for (; *sargv; )
			*pargv++ = *sargv++;

		argv    = aargv;
		program = (unsigned char *)interp;
	} else {
		argv    = (const char **)dctx->cctx->sargv;
		program = (unsigned char *)dctx->cctx->sargv[0];
	}

	envp    = (const char **)dctx->cctx->senvp;
	logfile = (unsigned char *)dctx->cctx->logfile;

	/* fdlog */
	if (logfile) {
		if ((fdlog[1] = __sys_openat(
				ntux_driver_fdcwd(dctx),
				logfile,O_CREAT|O_TRUNC|O_WRONLY,0)) < 0)
			if (ntux_errno_set(dctx,fdlog[1]))
				return NTUX_SYSTEM_ERROR(dctx);
	} else {
		if ((ret = __sys_pipe(fdlog)) < 0)
			if (ntux_errno_set(dctx,ret))
				return NTUX_SYSTEM_ERROR(dctx);
	}

	/* spawn */
	pid = __sys_vfork();

	/* failed? */
	if (pid < 0)
		if (ntux_errno_set(dctx,pid))
			return NTUX_SYSTEM_ERROR(dctx);

	/* child */
	if (pid == 0)
		if ((status = __sys_execve(program,argv,envp)))
			if (ntux_errno_set(dctx,status))
				if (NTUX_SYSTEM_ERROR(dctx))
					__sys_exit(0);

	/* parent */
	__sys_close(fdlog[1]);

	if (dctx->cctx->logfile) {
		__sys_wait4(
			pid,&status,
			0,0);

		return 0;
	}

	/* piped bridge output */
	rbytes = __sys_read(fdlog[0],buf,sizeof(buf));

	while (rbytes == -EINTR)
		rbytes = __sys_read(fdlog[0],buf,sizeof(buf));

	while (rbytes > 0) {
		for (ch=buf; rbytes; ch += wbytes) {
			wbytes = __sys_write(2,ch,rbytes);

			while (wbytes == -EINTR)
				wbytes = __sys_write(2,buf,rbytes);

			if (wbytes < 0) {
				__sys_close(fdlog[0]);
				ntux_errno_set(dctx,wbytes);
				return NTUX_SYSTEM_ERROR(dctx);
			}

			rbytes -= wbytes;
		}

		rbytes = __sys_read(fdlog[0],buf,sizeof(buf));

		while (rbytes == -EINTR)
			rbytes = __sys_read(fdlog[0],buf,sizeof(buf));
	}

	__sys_close(fdlog[0]);

	if (rbytes < 0)
		if (ntux_errno_set(dctx,rbytes))
			return NTUX_SYSTEM_ERROR(dctx);

	/* wait */
	struct ntux_driver_ctx_impl * ictx = ntux_get_driver_ictx(dctx);

	__sys_wait4(
		pid,
		&ictx->cctx.status,
		0,0);

	return 0;
}