1 | /* $Id: TestExecService.cpp 84924 2020-06-24 09:40:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TestExecServ - Basic Remote Execution Service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_DEFAULT
|
---|
32 | #include <iprt/alloca.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/buildconfig.h>
|
---|
36 | #include <iprt/cdrom.h>
|
---|
37 | #include <iprt/critsect.h>
|
---|
38 | #include <iprt/crc.h>
|
---|
39 | #include <iprt/ctype.h>
|
---|
40 | #include <iprt/dir.h>
|
---|
41 | #include <iprt/env.h>
|
---|
42 | #include <iprt/err.h>
|
---|
43 | #include <iprt/file.h>
|
---|
44 | #include <iprt/getopt.h>
|
---|
45 | #include <iprt/handle.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/log.h>
|
---|
48 | #include <iprt/mem.h>
|
---|
49 | #include <iprt/message.h>
|
---|
50 | #include <iprt/param.h>
|
---|
51 | #include <iprt/path.h>
|
---|
52 | #include <iprt/pipe.h>
|
---|
53 | #include <iprt/poll.h>
|
---|
54 | #include <iprt/process.h>
|
---|
55 | #include <iprt/stream.h>
|
---|
56 | #include <iprt/string.h>
|
---|
57 | #include <iprt/system.h>
|
---|
58 | #include <iprt/thread.h>
|
---|
59 | #include <iprt/time.h>
|
---|
60 | #include <iprt/uuid.h>
|
---|
61 | #include <iprt/zip.h>
|
---|
62 |
|
---|
63 | #include "product-generated.h"
|
---|
64 | #include "TestExecServiceInternal.h"
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | /*********************************************************************************************************************************
|
---|
69 | * Structures and Typedefs *
|
---|
70 | *********************************************************************************************************************************/
|
---|
71 | /**
|
---|
72 | * Handle IDs used by txsDoExec for the poll set.
|
---|
73 | */
|
---|
74 | typedef enum TXSEXECHNDID
|
---|
75 | {
|
---|
76 | TXSEXECHNDID_STDIN = 0,
|
---|
77 | TXSEXECHNDID_STDOUT,
|
---|
78 | TXSEXECHNDID_STDERR,
|
---|
79 | TXSEXECHNDID_TESTPIPE,
|
---|
80 | TXSEXECHNDID_STDIN_WRITABLE,
|
---|
81 | TXSEXECHNDID_TRANSPORT,
|
---|
82 | TXSEXECHNDID_THREAD
|
---|
83 | } TXSEXECHNDID;
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * For buffering process input supplied by the client.
|
---|
88 | */
|
---|
89 | typedef struct TXSEXECSTDINBUF
|
---|
90 | {
|
---|
91 | /** The mount of buffered data. */
|
---|
92 | size_t cb;
|
---|
93 | /** The current data offset. */
|
---|
94 | size_t off;
|
---|
95 | /** The data buffer. */
|
---|
96 | char *pch;
|
---|
97 | /** The amount of allocated buffer space. */
|
---|
98 | size_t cbAllocated;
|
---|
99 | /** Send further input into the bit bucket (stdin is dead). */
|
---|
100 | bool fBitBucket;
|
---|
101 | /** The CRC-32 for standard input (received part). */
|
---|
102 | uint32_t uCrc32;
|
---|
103 | } TXSEXECSTDINBUF;
|
---|
104 | /** Pointer to a standard input buffer. */
|
---|
105 | typedef TXSEXECSTDINBUF *PTXSEXECSTDINBUF;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * TXS child process info.
|
---|
109 | */
|
---|
110 | typedef struct TXSEXEC
|
---|
111 | {
|
---|
112 | PCTXSPKTHDR pPktHdr;
|
---|
113 | RTMSINTERVAL cMsTimeout;
|
---|
114 | int rcReplySend;
|
---|
115 |
|
---|
116 | RTPOLLSET hPollSet;
|
---|
117 | RTPIPE hStdInW;
|
---|
118 | RTPIPE hStdOutR;
|
---|
119 | RTPIPE hStdErrR;
|
---|
120 | RTPIPE hTestPipeR;
|
---|
121 | RTPIPE hWakeUpPipeR;
|
---|
122 | RTTHREAD hThreadWaiter;
|
---|
123 |
|
---|
124 | /** @name For the setup phase
|
---|
125 | * @{ */
|
---|
126 | struct StdPipe
|
---|
127 | {
|
---|
128 | RTHANDLE hChild;
|
---|
129 | PRTHANDLE phChild;
|
---|
130 | } StdIn,
|
---|
131 | StdOut,
|
---|
132 | StdErr;
|
---|
133 | RTPIPE hTestPipeW;
|
---|
134 | RTENV hEnv;
|
---|
135 | /** @} */
|
---|
136 |
|
---|
137 | /** For serializating some access. */
|
---|
138 | RTCRITSECT CritSect;
|
---|
139 | /** @name Members protected by the critical section.
|
---|
140 | * @{ */
|
---|
141 | RTPROCESS hProcess;
|
---|
142 | /** The process status. Only valid when fProcessAlive is cleared. */
|
---|
143 | RTPROCSTATUS ProcessStatus;
|
---|
144 | /** Set when the process is alive, clear when dead. */
|
---|
145 | bool volatile fProcessAlive;
|
---|
146 | /** The end of the pipe that hThreadWaiter writes to. */
|
---|
147 | RTPIPE hWakeUpPipeW;
|
---|
148 | /** @} */
|
---|
149 | } TXSEXEC;
|
---|
150 | /** Pointer to a the TXS child process info. */
|
---|
151 | typedef TXSEXEC *PTXSEXEC;
|
---|
152 |
|
---|
153 |
|
---|
154 | /*********************************************************************************************************************************
|
---|
155 | * Global Variables *
|
---|
156 | *********************************************************************************************************************************/
|
---|
157 | /**
|
---|
158 | * Transport layers.
|
---|
159 | */
|
---|
160 | static const PCTXSTRANSPORT g_apTransports[] =
|
---|
161 | {
|
---|
162 | &g_TcpTransport,
|
---|
163 | #ifndef RT_OS_OS2
|
---|
164 | &g_SerialTransport,
|
---|
165 | #endif
|
---|
166 | //&g_FileSysTransport,
|
---|
167 | //&g_GuestPropTransport,
|
---|
168 | //&g_TestDevTransport,
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** The select transport layer. */
|
---|
172 | static PCTXSTRANSPORT g_pTransport;
|
---|
173 | /** The scratch path. */
|
---|
174 | static char g_szScratchPath[RTPATH_MAX];
|
---|
175 | /** The default scratch path. */
|
---|
176 | static char g_szDefScratchPath[RTPATH_MAX];
|
---|
177 | /** The CD/DVD-ROM path. */
|
---|
178 | static char g_szCdRomPath[RTPATH_MAX];
|
---|
179 | /** The default CD/DVD-ROM path. */
|
---|
180 | static char g_szDefCdRomPath[RTPATH_MAX];
|
---|
181 | /** The directory containing the TXS executable. */
|
---|
182 | static char g_szTxsDir[RTPATH_MAX];
|
---|
183 | /** The current working directory for TXS (doesn't change). */
|
---|
184 | static char g_szCwd[RTPATH_MAX];
|
---|
185 | /** The operating system short name. */
|
---|
186 | static char g_szOsShortName[16];
|
---|
187 | /** The CPU architecture short name. */
|
---|
188 | static char g_szArchShortName[16];
|
---|
189 | /** The combined "OS.arch" name. */
|
---|
190 | static char g_szOsDotArchShortName[32];
|
---|
191 | /** The combined "OS/arch" name. */
|
---|
192 | static char g_szOsSlashArchShortName[32];
|
---|
193 | /** The executable suffix. */
|
---|
194 | static char g_szExeSuff[8];
|
---|
195 | /** The shell script suffix. */
|
---|
196 | static char g_szScriptSuff[8];
|
---|
197 | /** UUID identifying this TXS instance. This can be used to see if TXS
|
---|
198 | * has been restarted or not. */
|
---|
199 | static RTUUID g_InstanceUuid;
|
---|
200 | /** Whether to display the output of the child process or not. */
|
---|
201 | static bool g_fDisplayOutput = true;
|
---|
202 | /** Whether to terminate or not.
|
---|
203 | * @todo implement signals and stuff. */
|
---|
204 | static bool volatile g_fTerminate = false;
|
---|
205 | /** Verbosity level. */
|
---|
206 | uint32_t g_cVerbose = 1;
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Calculates the checksum value, zero any padding space and send the packet.
|
---|
211 | *
|
---|
212 | * @returns IPRT status code.
|
---|
213 | * @param pPkt The packet to send. Must point to a correctly
|
---|
214 | * aligned buffer.
|
---|
215 | */
|
---|
216 | static int txsSendPkt(PTXSPKTHDR pPkt)
|
---|
217 | {
|
---|
218 | Assert(pPkt->cb >= sizeof(*pPkt));
|
---|
219 | pPkt->uCrc32 = RTCrc32(pPkt->achOpcode, pPkt->cb - RT_UOFFSETOF(TXSPKTHDR, achOpcode));
|
---|
220 | if (pPkt->cb != RT_ALIGN_32(pPkt->cb, TXSPKT_ALIGNMENT))
|
---|
221 | memset((uint8_t *)pPkt + pPkt->cb, '\0', RT_ALIGN_32(pPkt->cb, TXSPKT_ALIGNMENT) - pPkt->cb);
|
---|
222 |
|
---|
223 | Log(("txsSendPkt: cb=%#x opcode=%.8s\n", pPkt->cb, pPkt->achOpcode));
|
---|
224 | Log2(("%.*Rhxd\n", RT_MIN(pPkt->cb, 256), pPkt));
|
---|
225 | int rc = g_pTransport->pfnSendPkt(pPkt);
|
---|
226 | while (RT_UNLIKELY(rc == VERR_INTERRUPTED) && !g_fTerminate)
|
---|
227 | rc = g_pTransport->pfnSendPkt(pPkt);
|
---|
228 | if (RT_FAILURE(rc))
|
---|
229 | Log(("txsSendPkt: rc=%Rrc\n", rc));
|
---|
230 |
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Sends a babble reply and disconnects the client (if applicable).
|
---|
236 | *
|
---|
237 | * @param pszOpcode The BABBLE opcode.
|
---|
238 | */
|
---|
239 | static void txsReplyBabble(const char *pszOpcode)
|
---|
240 | {
|
---|
241 | TXSPKTHDR Reply;
|
---|
242 | Reply.cb = sizeof(Reply);
|
---|
243 | Reply.uCrc32 = 0;
|
---|
244 | memcpy(Reply.achOpcode, pszOpcode, sizeof(Reply.achOpcode));
|
---|
245 |
|
---|
246 | g_pTransport->pfnBabble(&Reply, 20*1000);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Receive and validate a packet.
|
---|
251 | *
|
---|
252 | * Will send bable responses to malformed packets that results in a error status
|
---|
253 | * code.
|
---|
254 | *
|
---|
255 | * @returns IPRT status code.
|
---|
256 | * @param ppPktHdr Where to return the packet on success. Free
|
---|
257 | * with RTMemFree.
|
---|
258 | * @param fAutoRetryOnFailure Whether to retry on error.
|
---|
259 | */
|
---|
260 | static int txsRecvPkt(PPTXSPKTHDR ppPktHdr, bool fAutoRetryOnFailure)
|
---|
261 | {
|
---|
262 | for (;;)
|
---|
263 | {
|
---|
264 | PTXSPKTHDR pPktHdr;
|
---|
265 | int rc = g_pTransport->pfnRecvPkt(&pPktHdr);
|
---|
266 | if (RT_SUCCESS(rc))
|
---|
267 | {
|
---|
268 | /* validate the packet. */
|
---|
269 | if ( pPktHdr->cb >= sizeof(TXSPKTHDR)
|
---|
270 | && pPktHdr->cb < TXSPKT_MAX_SIZE)
|
---|
271 | {
|
---|
272 | Log2(("txsRecvPkt: pPktHdr=%p cb=%#x crc32=%#x opcode=%.8s\n"
|
---|
273 | "%.*Rhxd\n",
|
---|
274 | pPktHdr, pPktHdr->cb, pPktHdr->uCrc32, pPktHdr->achOpcode, RT_MIN(pPktHdr->cb, 256), pPktHdr));
|
---|
275 | uint32_t uCrc32Calc = pPktHdr->uCrc32 != 0
|
---|
276 | ? RTCrc32(&pPktHdr->achOpcode[0], pPktHdr->cb - RT_UOFFSETOF(TXSPKTHDR, achOpcode))
|
---|
277 | : 0;
|
---|
278 | if (pPktHdr->uCrc32 == uCrc32Calc)
|
---|
279 | {
|
---|
280 | AssertCompileMemberSize(TXSPKTHDR, achOpcode, 8);
|
---|
281 | if ( RT_C_IS_UPPER(pPktHdr->achOpcode[0])
|
---|
282 | && RT_C_IS_UPPER(pPktHdr->achOpcode[1])
|
---|
283 | && (RT_C_IS_UPPER(pPktHdr->achOpcode[2]) || pPktHdr->achOpcode[2] == ' ')
|
---|
284 | && (RT_C_IS_PRINT(pPktHdr->achOpcode[3]) || pPktHdr->achOpcode[3] == ' ')
|
---|
285 | && (RT_C_IS_PRINT(pPktHdr->achOpcode[4]) || pPktHdr->achOpcode[4] == ' ')
|
---|
286 | && (RT_C_IS_PRINT(pPktHdr->achOpcode[5]) || pPktHdr->achOpcode[5] == ' ')
|
---|
287 | && (RT_C_IS_PRINT(pPktHdr->achOpcode[6]) || pPktHdr->achOpcode[6] == ' ')
|
---|
288 | && (RT_C_IS_PRINT(pPktHdr->achOpcode[7]) || pPktHdr->achOpcode[7] == ' ')
|
---|
289 | )
|
---|
290 | {
|
---|
291 | Log(("txsRecvPkt: cb=%#x opcode=%.8s\n", pPktHdr->cb, pPktHdr->achOpcode));
|
---|
292 | *ppPktHdr = pPktHdr;
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 |
|
---|
296 | rc = VERR_IO_BAD_COMMAND;
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | Log(("txsRecvPkt: cb=%#x opcode=%.8s crc32=%#x actual=%#x\n",
|
---|
301 | pPktHdr->cb, pPktHdr->achOpcode, pPktHdr->uCrc32, uCrc32Calc));
|
---|
302 | rc = VERR_IO_CRC;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else
|
---|
306 | rc = VERR_IO_BAD_LENGTH;
|
---|
307 |
|
---|
308 | /* Send babble reply and disconnect the client if the transport is
|
---|
309 | connection oriented. */
|
---|
310 | if (rc == VERR_IO_BAD_LENGTH)
|
---|
311 | txsReplyBabble("BABBLE L");
|
---|
312 | else if (rc == VERR_IO_CRC)
|
---|
313 | txsReplyBabble("BABBLE C");
|
---|
314 | else if (rc == VERR_IO_BAD_COMMAND)
|
---|
315 | txsReplyBabble("BABBLE O");
|
---|
316 | else
|
---|
317 | txsReplyBabble("BABBLE ");
|
---|
318 | RTMemFree(pPktHdr);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* Try again or return failure? */
|
---|
322 | if ( g_fTerminate
|
---|
323 | || rc != VERR_INTERRUPTED
|
---|
324 | || !fAutoRetryOnFailure
|
---|
325 | )
|
---|
326 | {
|
---|
327 | Log(("txsRecvPkt: rc=%Rrc\n", rc));
|
---|
328 | return rc;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Make a simple reply, only status opcode.
|
---|
335 | *
|
---|
336 | * @returns IPRT status code of the send.
|
---|
337 | * @param pReply The reply packet.
|
---|
338 | * @param pszOpcode The status opcode. Exactly 8 chars long, padd
|
---|
339 | * with space.
|
---|
340 | * @param cbExtra Bytes in addition to the header.
|
---|
341 | */
|
---|
342 | static int txsReplyInternal(PTXSPKTHDR pReply, const char *pszOpcode, size_t cbExtra)
|
---|
343 | {
|
---|
344 | /* copy the opcode, don't be too strict in case of a padding screw up. */
|
---|
345 | size_t cchOpcode = strlen(pszOpcode);
|
---|
346 | if (RT_LIKELY(cchOpcode == sizeof(pReply->achOpcode)))
|
---|
347 | memcpy(pReply->achOpcode, pszOpcode, sizeof(pReply->achOpcode));
|
---|
348 | else
|
---|
349 | {
|
---|
350 | Assert(cchOpcode == sizeof(pReply->achOpcode));
|
---|
351 | while (cchOpcode > 0 && pszOpcode[cchOpcode - 1] == ' ')
|
---|
352 | cchOpcode--;
|
---|
353 | AssertMsgReturn(cchOpcode < sizeof(pReply->achOpcode), ("%d/'%.8s'\n", cchOpcode, pszOpcode), VERR_INTERNAL_ERROR_4);
|
---|
354 | memcpy(pReply->achOpcode, pszOpcode, cchOpcode);
|
---|
355 | memset(&pReply->achOpcode[cchOpcode], ' ', sizeof(pReply->achOpcode) - cchOpcode);
|
---|
356 | }
|
---|
357 |
|
---|
358 | pReply->cb = (uint32_t)sizeof(TXSPKTHDR) + (uint32_t)cbExtra;
|
---|
359 | pReply->uCrc32 = 0; /* (txsSendPkt sets it) */
|
---|
360 |
|
---|
361 | return txsSendPkt(pReply);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Make a simple reply, only status opcode.
|
---|
366 | *
|
---|
367 | * @returns IPRT status code of the send.
|
---|
368 | * @param pPktHdr The original packet (for future use).
|
---|
369 | * @param pszOpcode The status opcode. Exactly 8 chars long, padd
|
---|
370 | * with space.
|
---|
371 | */
|
---|
372 | static int txsReplySimple(PCTXSPKTHDR pPktHdr, const char *pszOpcode)
|
---|
373 | {
|
---|
374 | TXSPKTHDR Pkt;
|
---|
375 | NOREF(pPktHdr);
|
---|
376 | return txsReplyInternal(&Pkt, pszOpcode, 0);
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Acknowledges a packet with success.
|
---|
381 | *
|
---|
382 | * @returns IPRT status code of the send.
|
---|
383 | * @param pPktHdr The original packet (for future use).
|
---|
384 | */
|
---|
385 | static int txsReplyAck(PCTXSPKTHDR pPktHdr)
|
---|
386 | {
|
---|
387 | return txsReplySimple(pPktHdr, "ACK ");
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Replies with a failure.
|
---|
392 | *
|
---|
393 | * @returns IPRT status code of the send.
|
---|
394 | * @param pPktHdr The original packet (for future use).
|
---|
395 | * @param pszOpcode The status opcode. Exactly 8 chars long, padd
|
---|
396 | * with space.
|
---|
397 | * @param pszDetailFmt Longer description of the problem (format
|
---|
398 | * string).
|
---|
399 | * @param va Format arguments.
|
---|
400 | */
|
---|
401 | static int txsReplyFailureV(PCTXSPKTHDR pPktHdr, const char *pszOpcode, const char *pszDetailFmt, va_list va)
|
---|
402 | {
|
---|
403 | NOREF(pPktHdr);
|
---|
404 | union
|
---|
405 | {
|
---|
406 | TXSPKTHDR Hdr;
|
---|
407 | char ach[256];
|
---|
408 | } uPkt;
|
---|
409 |
|
---|
410 | size_t cchDetail = RTStrPrintfV(&uPkt.ach[sizeof(TXSPKTHDR)],
|
---|
411 | sizeof(uPkt) - sizeof(TXSPKTHDR),
|
---|
412 | pszDetailFmt, va);
|
---|
413 | return txsReplyInternal(&uPkt.Hdr, pszOpcode, cchDetail + 1);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Replies with a failure.
|
---|
418 | *
|
---|
419 | * @returns IPRT status code of the send.
|
---|
420 | * @param pPktHdr The original packet (for future use).
|
---|
421 | * @param pszOpcode The status opcode. Exactly 8 chars long, padd
|
---|
422 | * with space.
|
---|
423 | * @param pszDetailFmt Longer description of the problem (format
|
---|
424 | * string).
|
---|
425 | * @param ... Format arguments.
|
---|
426 | */
|
---|
427 | static int txsReplyFailure(PCTXSPKTHDR pPktHdr, const char *pszOpcode, const char *pszDetailFmt, ...)
|
---|
428 | {
|
---|
429 | va_list va;
|
---|
430 | va_start(va, pszDetailFmt);
|
---|
431 | int rc = txsReplyFailureV(pPktHdr, pszOpcode, pszDetailFmt, va);
|
---|
432 | va_end(va);
|
---|
433 | return rc;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Replies according to the return code.
|
---|
438 | *
|
---|
439 | * @returns IPRT status code of the send.
|
---|
440 | * @param pPktHdr The packet to reply to.
|
---|
441 | * @param rcOperation The status code to report.
|
---|
442 | * @param pszOperationFmt The operation that failed. Typically giving the
|
---|
443 | * function call with important arguments.
|
---|
444 | * @param ... Arguments to the format string.
|
---|
445 | */
|
---|
446 | static int txsReplyRC(PCTXSPKTHDR pPktHdr, int rcOperation, const char *pszOperationFmt, ...)
|
---|
447 | {
|
---|
448 | if (RT_SUCCESS(rcOperation))
|
---|
449 | return txsReplyAck(pPktHdr);
|
---|
450 |
|
---|
451 | char szOperation[128];
|
---|
452 | va_list va;
|
---|
453 | va_start(va, pszOperationFmt);
|
---|
454 | RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va);
|
---|
455 | va_end(va);
|
---|
456 |
|
---|
457 | return txsReplyFailure(pPktHdr, "FAILED ", "%s failed with rc=%Rrc (opcode '%.8s')",
|
---|
458 | szOperation, rcOperation, pPktHdr->achOpcode);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Signal a bad packet minum size.
|
---|
463 | *
|
---|
464 | * @returns IPRT status code of the send.
|
---|
465 | * @param pPktHdr The packet to reply to.
|
---|
466 | * @param cbMin The minimum size.
|
---|
467 | */
|
---|
468 | static int txsReplyBadMinSize(PCTXSPKTHDR pPktHdr, size_t cbMin)
|
---|
469 | {
|
---|
470 | return txsReplyFailure(pPktHdr, "BAD SIZE", "Expected at least %zu bytes, got %u (opcode '%.8s')",
|
---|
471 | cbMin, pPktHdr->cb, pPktHdr->achOpcode);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Signal a bad packet exact size.
|
---|
476 | *
|
---|
477 | * @returns IPRT status code of the send.
|
---|
478 | * @param pPktHdr The packet to reply to.
|
---|
479 | * @param cb The wanted size.
|
---|
480 | */
|
---|
481 | static int txsReplyBadSize(PCTXSPKTHDR pPktHdr, size_t cb)
|
---|
482 | {
|
---|
483 | return txsReplyFailure(pPktHdr, "BAD SIZE", "Expected at %zu bytes, got %u (opcode '%.8s')",
|
---|
484 | cb, pPktHdr->cb, pPktHdr->achOpcode);
|
---|
485 | }
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * Deals with a command that isn't implemented yet.
|
---|
489 | * @returns IPRT status code of the send.
|
---|
490 | * @param pPktHdr The packet which opcode isn't implemented.
|
---|
491 | */
|
---|
492 | static int txsReplyNotImplemented(PCTXSPKTHDR pPktHdr)
|
---|
493 | {
|
---|
494 | return txsReplyFailure(pPktHdr, "NOT IMPL", "Opcode '%.8s' is not implemented", pPktHdr->achOpcode);
|
---|
495 | }
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Deals with a unknown command.
|
---|
499 | * @returns IPRT status code of the send.
|
---|
500 | * @param pPktHdr The packet to reply to.
|
---|
501 | */
|
---|
502 | static int txsReplyUnknown(PCTXSPKTHDR pPktHdr)
|
---|
503 | {
|
---|
504 | return txsReplyFailure(pPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known", pPktHdr->achOpcode);
|
---|
505 | }
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Replaces a variable with its value.
|
---|
509 | *
|
---|
510 | * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
|
---|
511 | * @param ppszNew In/Out.
|
---|
512 | * @param pcchNew In/Out. (Messed up on failure.)
|
---|
513 | * @param offVar Variable offset.
|
---|
514 | * @param cchVar Variable length.
|
---|
515 | * @param pszValue The value.
|
---|
516 | * @param cchValue Value length.
|
---|
517 | */
|
---|
518 | static int txsReplaceStringVariable(char **ppszNew, size_t *pcchNew, size_t offVar, size_t cchVar,
|
---|
519 | const char *pszValue, size_t cchValue)
|
---|
520 | {
|
---|
521 | size_t const cchAfter = *pcchNew - offVar - cchVar;
|
---|
522 | if (cchVar < cchValue)
|
---|
523 | {
|
---|
524 | *pcchNew += cchValue - cchVar;
|
---|
525 | int rc = RTStrRealloc(ppszNew, *pcchNew + 1);
|
---|
526 | if (RT_FAILURE(rc))
|
---|
527 | return rc;
|
---|
528 | }
|
---|
529 |
|
---|
530 | char *pszNew = *ppszNew;
|
---|
531 | memmove(&pszNew[offVar + cchValue], &pszNew[offVar + cchVar], cchAfter + 1);
|
---|
532 | memcpy(&pszNew[offVar], pszValue, cchValue);
|
---|
533 | return VINF_SUCCESS;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Replace the variables found in the source string, returning a new string that
|
---|
538 | * lives on the string heap.
|
---|
539 | *
|
---|
540 | * @returns Boolean success indicator. Will reply to the client with all the
|
---|
541 | * gory detail on failure.
|
---|
542 | * @param pPktHdr The packet the string relates to. For replying
|
---|
543 | * on error.
|
---|
544 | * @param pszSrc The source string.
|
---|
545 | * @param ppszNew Where to return the new string.
|
---|
546 | * @param prcSend Where to return the status code of the send on
|
---|
547 | * failure.
|
---|
548 | */
|
---|
549 | static int txsReplaceStringVariables(PCTXSPKTHDR pPktHdr, const char *pszSrc, char **ppszNew, int *prcSend)
|
---|
550 | {
|
---|
551 | /* Lazy approach that employs memmove. */
|
---|
552 | size_t cchNew = strlen(pszSrc);
|
---|
553 | char *pszNew = RTStrDup(pszSrc);
|
---|
554 | char *pszDollar = pszNew;
|
---|
555 | while (pszDollar && (pszDollar = strchr(pszDollar, '$')) != NULL)
|
---|
556 | {
|
---|
557 | if (pszDollar[1] == '{')
|
---|
558 | {
|
---|
559 | char *pszEnd = strchr(&pszDollar[2], '}');
|
---|
560 | if (pszEnd)
|
---|
561 | {
|
---|
562 | #define IF_VARIABLE_DO(pszDollar, szVarExpr, pszValue) \
|
---|
563 | if ( cchVar == sizeof(szVarExpr) - 1 \
|
---|
564 | && !memcmp(pszDollar, szVarExpr, sizeof(szVarExpr) - 1) ) \
|
---|
565 | { \
|
---|
566 | size_t const cchValue = strlen(pszValue); \
|
---|
567 | rc = txsReplaceStringVariable(&pszNew, &cchNew, offDollar, \
|
---|
568 | sizeof(szVarExpr) - 1, pszValue, cchValue); \
|
---|
569 | offDollar += cchValue; \
|
---|
570 | }
|
---|
571 | int rc;
|
---|
572 | size_t const cchVar = pszEnd - pszDollar + 1; /* includes "${}" */
|
---|
573 | size_t offDollar = pszDollar - pszNew;
|
---|
574 | IF_VARIABLE_DO(pszDollar, "${CDROM}", g_szCdRomPath)
|
---|
575 | else IF_VARIABLE_DO(pszDollar, "${SCRATCH}", g_szScratchPath)
|
---|
576 | else IF_VARIABLE_DO(pszDollar, "${ARCH}", g_szArchShortName)
|
---|
577 | else IF_VARIABLE_DO(pszDollar, "${OS}", g_szOsShortName)
|
---|
578 | else IF_VARIABLE_DO(pszDollar, "${OS.ARCH}", g_szOsDotArchShortName)
|
---|
579 | else IF_VARIABLE_DO(pszDollar, "${OS/ARCH}", g_szOsSlashArchShortName)
|
---|
580 | else IF_VARIABLE_DO(pszDollar, "${EXESUFF}", g_szExeSuff)
|
---|
581 | else IF_VARIABLE_DO(pszDollar, "${SCRIPTSUFF}", g_szScriptSuff)
|
---|
582 | else IF_VARIABLE_DO(pszDollar, "${TXSDIR}", g_szTxsDir)
|
---|
583 | else IF_VARIABLE_DO(pszDollar, "${CWD}", g_szCwd)
|
---|
584 | else if ( cchVar >= sizeof("${env.") + 1
|
---|
585 | && memcmp(pszDollar, RT_STR_TUPLE("${env.")) == 0)
|
---|
586 | {
|
---|
587 | const char *pszEnvVar = pszDollar + 6;
|
---|
588 | size_t cchValue = 0;
|
---|
589 | char szValue[RTPATH_MAX];
|
---|
590 | *pszEnd = '\0';
|
---|
591 | rc = RTEnvGetEx(RTENV_DEFAULT, pszEnvVar, szValue, sizeof(szValue), &cchValue);
|
---|
592 | if (RT_SUCCESS(rc))
|
---|
593 | {
|
---|
594 | *pszEnd = '}';
|
---|
595 | rc = txsReplaceStringVariable(&pszNew, &cchNew, offDollar, cchVar, szValue, cchValue);
|
---|
596 | offDollar += cchValue;
|
---|
597 | }
|
---|
598 | else
|
---|
599 | {
|
---|
600 | if (rc == VERR_ENV_VAR_NOT_FOUND)
|
---|
601 | *prcSend = txsReplyFailure(pPktHdr, "UNKN VAR", "Environment variable '%s' encountered in '%s'",
|
---|
602 | pszEnvVar, pszSrc);
|
---|
603 | else
|
---|
604 | *prcSend = txsReplyFailure(pPktHdr, "FAILDENV",
|
---|
605 | "RTEnvGetEx(,'%s',,,) failed with %Rrc (opcode '%.8s')",
|
---|
606 | pszEnvVar, rc, pPktHdr->achOpcode);
|
---|
607 | RTStrFree(pszNew);
|
---|
608 | *ppszNew = NULL;
|
---|
609 | return false;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | else
|
---|
613 | {
|
---|
614 | RTStrFree(pszNew);
|
---|
615 | *prcSend = txsReplyFailure(pPktHdr, "UNKN VAR", "Unknown variable '%.*s' encountered in '%s'",
|
---|
616 | cchVar, pszDollar, pszSrc);
|
---|
617 | *ppszNew = NULL;
|
---|
618 | return false;
|
---|
619 | }
|
---|
620 | pszDollar = &pszNew[offDollar];
|
---|
621 |
|
---|
622 | if (RT_FAILURE(rc))
|
---|
623 | {
|
---|
624 | RTStrFree(pszNew);
|
---|
625 | *prcSend = txsReplyRC(pPktHdr, rc, "RTStrRealloc");
|
---|
626 | *ppszNew = NULL;
|
---|
627 | return false;
|
---|
628 | }
|
---|
629 | #undef IF_VARIABLE_DO
|
---|
630 | }
|
---|
631 | }
|
---|
632 | /* Undo dollar escape sequences: $$ -> $ */
|
---|
633 | else if (pszDollar[1] == '$')
|
---|
634 | {
|
---|
635 | size_t cchLeft = cchNew - (&pszDollar[1] - pszNew);
|
---|
636 | memmove(pszDollar, &pszDollar[1], cchLeft);
|
---|
637 | pszDollar[cchLeft] = '\0';
|
---|
638 | cchNew -= 1;
|
---|
639 | }
|
---|
640 | else /* No match, move to next char to avoid endless looping. */
|
---|
641 | pszDollar++;
|
---|
642 | }
|
---|
643 |
|
---|
644 | *ppszNew = pszNew;
|
---|
645 | *prcSend = VINF_SUCCESS;
|
---|
646 | return true;
|
---|
647 | }
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Checks if the string is valid and returns the expanded version.
|
---|
651 | *
|
---|
652 | * @returns true if valid, false if invalid.
|
---|
653 | * @param pPktHdr The packet being unpacked.
|
---|
654 | * @param pszArgName The argument name.
|
---|
655 | * @param psz Pointer to the string within pPktHdr.
|
---|
656 | * @param ppszExp Where to return the expanded string. Must be
|
---|
657 | * freed by calling RTStrFree().
|
---|
658 | * @param ppszNext Where to return the pointer to the next field.
|
---|
659 | * If NULL, then we assume this string is at the
|
---|
660 | * end of the packet and will make sure it has the
|
---|
661 | * advertised length.
|
---|
662 | * @param prcSend Where to return the status code of the send on
|
---|
663 | * failure.
|
---|
664 | */
|
---|
665 | static bool txsIsStringValid(PCTXSPKTHDR pPktHdr, const char *pszArgName, const char *psz,
|
---|
666 | char **ppszExp, const char **ppszNext, int *prcSend)
|
---|
667 | {
|
---|
668 | *ppszExp = NULL;
|
---|
669 | if (ppszNext)
|
---|
670 | *ppszNext = NULL;
|
---|
671 |
|
---|
672 | size_t const off = psz - (const char *)pPktHdr;
|
---|
673 | if (pPktHdr->cb <= off)
|
---|
674 | {
|
---|
675 | *prcSend = txsReplyFailure(pPktHdr, "STR MISS", "Missing string argument '%s' in '%.8s'",
|
---|
676 | pszArgName, pPktHdr->achOpcode);
|
---|
677 | return false;
|
---|
678 | }
|
---|
679 |
|
---|
680 | size_t const cchMax = pPktHdr->cb - off;
|
---|
681 | const char *pszEnd = RTStrEnd(psz, cchMax);
|
---|
682 | if (!pszEnd)
|
---|
683 | {
|
---|
684 | *prcSend = txsReplyFailure(pPktHdr, "STR TERM", "The string argument '%s' in '%.8s' is unterminated",
|
---|
685 | pszArgName, pPktHdr->achOpcode);
|
---|
686 | return false;
|
---|
687 | }
|
---|
688 |
|
---|
689 | if (!ppszNext && (size_t)(pszEnd - psz) != cchMax - 1)
|
---|
690 | {
|
---|
691 | *prcSend = txsReplyFailure(pPktHdr, "STR SHRT", "The string argument '%s' in '%.8s' is shorter than advertised",
|
---|
692 | pszArgName, pPktHdr->achOpcode);
|
---|
693 | return false;
|
---|
694 | }
|
---|
695 |
|
---|
696 | if (!txsReplaceStringVariables(pPktHdr, psz, ppszExp, prcSend))
|
---|
697 | return false;
|
---|
698 | if (ppszNext)
|
---|
699 | *ppszNext = pszEnd + 1;
|
---|
700 | return true;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Validates a packet with a single string after the header.
|
---|
705 | *
|
---|
706 | * @returns true if valid, false if invalid.
|
---|
707 | * @param pPktHdr The packet.
|
---|
708 | * @param pszArgName The argument name.
|
---|
709 | * @param ppszExp Where to return the string pointer. Variables
|
---|
710 | * will be replaced and it must therefore be freed
|
---|
711 | * by calling RTStrFree().
|
---|
712 | * @param prcSend Where to return the status code of the send on
|
---|
713 | * failure.
|
---|
714 | */
|
---|
715 | static bool txsIsStringPktValid(PCTXSPKTHDR pPktHdr, const char *pszArgName, char **ppszExp, int *prcSend)
|
---|
716 | {
|
---|
717 | if (pPktHdr->cb < sizeof(TXSPKTHDR) + 2)
|
---|
718 | {
|
---|
719 | *ppszExp = NULL;
|
---|
720 | *prcSend = txsReplyBadMinSize(pPktHdr, sizeof(TXSPKTHDR) + 2);
|
---|
721 | return false;
|
---|
722 | }
|
---|
723 |
|
---|
724 | return txsIsStringValid(pPktHdr, pszArgName, (const char *)(pPktHdr + 1), ppszExp, NULL, prcSend);
|
---|
725 | }
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Checks if the two opcodes match.
|
---|
729 | *
|
---|
730 | * @returns true on match, false on mismatch.
|
---|
731 | * @param pPktHdr The packet header.
|
---|
732 | * @param pszOpcode2 The opcode we're comparing with. Does not have
|
---|
733 | * to be the whole 8 chars long.
|
---|
734 | */
|
---|
735 | DECLINLINE(bool) txsIsSameOpcode(PCTXSPKTHDR pPktHdr, const char *pszOpcode2)
|
---|
736 | {
|
---|
737 | if (pPktHdr->achOpcode[0] != pszOpcode2[0])
|
---|
738 | return false;
|
---|
739 | if (pPktHdr->achOpcode[1] != pszOpcode2[1])
|
---|
740 | return false;
|
---|
741 |
|
---|
742 | unsigned i = 2;
|
---|
743 | while ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode)
|
---|
744 | && pszOpcode2[i] != '\0')
|
---|
745 | {
|
---|
746 | if (pPktHdr->achOpcode[i] != pszOpcode2[i])
|
---|
747 | break;
|
---|
748 | i++;
|
---|
749 | }
|
---|
750 |
|
---|
751 | if ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode)
|
---|
752 | && pszOpcode2[i] == '\0')
|
---|
753 | {
|
---|
754 | while ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode)
|
---|
755 | && pPktHdr->achOpcode[i] == ' ')
|
---|
756 | i++;
|
---|
757 | }
|
---|
758 |
|
---|
759 | return i == RT_SIZEOFMEMB(TXSPKTHDR, achOpcode);
|
---|
760 | }
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Used by txsDoGetFile to wait for a reply ACK from the client.
|
---|
764 | *
|
---|
765 | * @returns VINF_SUCCESS on ACK, VERR_GENERAL_FAILURE on NACK,
|
---|
766 | * VERR_NET_NOT_CONNECTED on unknown response (sending a bable reply),
|
---|
767 | * or whatever txsRecvPkt returns.
|
---|
768 | * @param pPktHdr The original packet (for future use).
|
---|
769 | */
|
---|
770 | static int txsWaitForAck(PCTXSPKTHDR pPktHdr)
|
---|
771 | {
|
---|
772 | NOREF(pPktHdr);
|
---|
773 | /** @todo timeout? */
|
---|
774 | PTXSPKTHDR pReply;
|
---|
775 | int rc = txsRecvPkt(&pReply, false /*fAutoRetryOnFailure*/);
|
---|
776 | if (RT_SUCCESS(rc))
|
---|
777 | {
|
---|
778 | if (txsIsSameOpcode(pReply, "ACK"))
|
---|
779 | rc = VINF_SUCCESS;
|
---|
780 | else if (txsIsSameOpcode(pReply, "NACK"))
|
---|
781 | rc = VERR_GENERAL_FAILURE;
|
---|
782 | else
|
---|
783 | {
|
---|
784 | txsReplyBabble("BABBLE ");
|
---|
785 | rc = VERR_NET_NOT_CONNECTED;
|
---|
786 | }
|
---|
787 | RTMemFree(pReply);
|
---|
788 | }
|
---|
789 | return rc;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Expands the variables in the string and sends it back to the host.
|
---|
794 | *
|
---|
795 | * @returns IPRT status code from send.
|
---|
796 | * @param pPktHdr The expand string packet.
|
---|
797 | */
|
---|
798 | static int txsDoExpandString(PCTXSPKTHDR pPktHdr)
|
---|
799 | {
|
---|
800 | int rc;
|
---|
801 | char *pszExpanded;
|
---|
802 | if (!txsIsStringPktValid(pPktHdr, "string", &pszExpanded, &rc))
|
---|
803 | return rc;
|
---|
804 |
|
---|
805 | struct
|
---|
806 | {
|
---|
807 | TXSPKTHDR Hdr;
|
---|
808 | char szString[_64K];
|
---|
809 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
810 | } Pkt;
|
---|
811 |
|
---|
812 | size_t const cbExpanded = strlen(pszExpanded) + 1;
|
---|
813 | if (cbExpanded <= sizeof(Pkt.szString))
|
---|
814 | {
|
---|
815 | memcpy(Pkt.szString, pszExpanded, cbExpanded);
|
---|
816 | rc = txsReplyInternal(&Pkt.Hdr, "STRING ", cbExpanded);
|
---|
817 | }
|
---|
818 | else
|
---|
819 | {
|
---|
820 | memcpy(Pkt.szString, pszExpanded, sizeof(Pkt.szString));
|
---|
821 | Pkt.szString[0] = '\0';
|
---|
822 | rc = txsReplyInternal(&Pkt.Hdr, "SHORTSTR", sizeof(Pkt.szString));
|
---|
823 | }
|
---|
824 |
|
---|
825 | RTStrFree(pszExpanded);
|
---|
826 | return rc;
|
---|
827 | }
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Packs a tar file / directory.
|
---|
831 | *
|
---|
832 | * @returns IPRT status code from send.
|
---|
833 | * @param pPktHdr The pack file packet.
|
---|
834 | */
|
---|
835 | static int txsDoPackFile(PCTXSPKTHDR pPktHdr)
|
---|
836 | {
|
---|
837 | int rc;
|
---|
838 | char *pszFile = NULL;
|
---|
839 | char *pszSource = NULL;
|
---|
840 |
|
---|
841 | /* Packet cursor. */
|
---|
842 | const char *pch = (const char *)(pPktHdr + 1);
|
---|
843 |
|
---|
844 | if (txsIsStringValid(pPktHdr, "file", pch, &pszFile, &pch, &rc))
|
---|
845 | {
|
---|
846 | if (txsIsStringValid(pPktHdr, "source", pch, &pszSource, &pch, &rc))
|
---|
847 | {
|
---|
848 | char *pszSuff = RTPathSuffix(pszFile);
|
---|
849 |
|
---|
850 | const char *apszArgs[7];
|
---|
851 | unsigned cArgs = 0;
|
---|
852 |
|
---|
853 | apszArgs[cArgs++] = "RTTar";
|
---|
854 | apszArgs[cArgs++] = "--create";
|
---|
855 |
|
---|
856 | apszArgs[cArgs++] = "--file";
|
---|
857 | apszArgs[cArgs++] = pszFile;
|
---|
858 |
|
---|
859 | if ( pszSuff
|
---|
860 | && ( !RTStrICmp(pszSuff, ".gz")
|
---|
861 | || !RTStrICmp(pszSuff, ".tgz")))
|
---|
862 | apszArgs[cArgs++] = "--gzip";
|
---|
863 |
|
---|
864 | apszArgs[cArgs++] = pszSource;
|
---|
865 |
|
---|
866 | RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
|
---|
867 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
868 | rc = VERR_GENERAL_FAILURE; /** @todo proper return code. */
|
---|
869 | else
|
---|
870 | rc = VINF_SUCCESS;
|
---|
871 |
|
---|
872 | rc = txsReplyRC(pPktHdr, rc, "RTZipTarCmd(\"%s\",\"%s\")",
|
---|
873 | pszFile, pszSource);
|
---|
874 |
|
---|
875 | RTStrFree(pszSource);
|
---|
876 | }
|
---|
877 | RTStrFree(pszFile);
|
---|
878 | }
|
---|
879 |
|
---|
880 | return rc;
|
---|
881 | }
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Unpacks a tar file.
|
---|
885 | *
|
---|
886 | * @returns IPRT status code from send.
|
---|
887 | * @param pPktHdr The unpack file packet.
|
---|
888 | */
|
---|
889 | static int txsDoUnpackFile(PCTXSPKTHDR pPktHdr)
|
---|
890 | {
|
---|
891 | int rc;
|
---|
892 | char *pszFile = NULL;
|
---|
893 | char *pszDirectory = NULL;
|
---|
894 |
|
---|
895 | /* Packet cursor. */
|
---|
896 | const char *pch = (const char *)(pPktHdr + 1);
|
---|
897 |
|
---|
898 | if (txsIsStringValid(pPktHdr, "file", pch, &pszFile, &pch, &rc))
|
---|
899 | {
|
---|
900 | if (txsIsStringValid(pPktHdr, "directory", pch, &pszDirectory, &pch, &rc))
|
---|
901 | {
|
---|
902 | char *pszSuff = RTPathSuffix(pszFile);
|
---|
903 |
|
---|
904 | const char *apszArgs[7];
|
---|
905 | unsigned cArgs = 0;
|
---|
906 |
|
---|
907 | apszArgs[cArgs++] = "RTTar";
|
---|
908 | apszArgs[cArgs++] = "--extract";
|
---|
909 |
|
---|
910 | apszArgs[cArgs++] = "--file";
|
---|
911 | apszArgs[cArgs++] = pszFile;
|
---|
912 |
|
---|
913 | apszArgs[cArgs++] = "--directory";
|
---|
914 | apszArgs[cArgs++] = pszDirectory;
|
---|
915 |
|
---|
916 | if ( pszSuff
|
---|
917 | && ( !RTStrICmp(pszSuff, ".gz")
|
---|
918 | || !RTStrICmp(pszSuff, ".tgz")))
|
---|
919 | apszArgs[cArgs++] = "--gunzip";
|
---|
920 |
|
---|
921 | RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
|
---|
922 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
923 | rc = VERR_GENERAL_FAILURE; /** @todo proper return code. */
|
---|
924 | else
|
---|
925 | rc = VINF_SUCCESS;
|
---|
926 |
|
---|
927 | rc = txsReplyRC(pPktHdr, rc, "RTZipTarCmd(\"%s\",\"%s\")",
|
---|
928 | pszFile, pszDirectory);
|
---|
929 |
|
---|
930 | RTStrFree(pszDirectory);
|
---|
931 | }
|
---|
932 | RTStrFree(pszFile);
|
---|
933 | }
|
---|
934 |
|
---|
935 | return rc;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Downloads a file to the client.
|
---|
940 | *
|
---|
941 | * The transfer sends a stream of DATA packets (0 or more) and ends it all with
|
---|
942 | * a ACK packet. If an error occurs, a FAILURE packet is sent and the transfer
|
---|
943 | * aborted.
|
---|
944 | *
|
---|
945 | * @returns IPRT status code from send.
|
---|
946 | * @param pPktHdr The get file packet.
|
---|
947 | */
|
---|
948 | static int txsDoGetFile(PCTXSPKTHDR pPktHdr)
|
---|
949 | {
|
---|
950 | int rc;
|
---|
951 | char *pszPath;
|
---|
952 | if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc))
|
---|
953 | return rc;
|
---|
954 |
|
---|
955 | RTFILE hFile;
|
---|
956 | rc = RTFileOpen(&hFile, pszPath, RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
|
---|
957 | if (RT_SUCCESS(rc))
|
---|
958 | {
|
---|
959 | uint32_t uMyCrc32 = RTCrc32Start();
|
---|
960 | for (;;)
|
---|
961 | {
|
---|
962 | struct
|
---|
963 | {
|
---|
964 | TXSPKTHDR Hdr;
|
---|
965 | uint32_t uCrc32;
|
---|
966 | char ab[_64K];
|
---|
967 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
968 | } Pkt;
|
---|
969 | size_t cbRead;
|
---|
970 | rc = RTFileRead(hFile, &Pkt.ab[0], _64K, &cbRead);
|
---|
971 | if (RT_FAILURE(rc) || cbRead == 0)
|
---|
972 | {
|
---|
973 | if (rc == VERR_EOF || (RT_SUCCESS(rc) && cbRead == 0))
|
---|
974 | {
|
---|
975 | Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
|
---|
976 | rc = txsReplyInternal(&Pkt.Hdr, "DATA EOF", sizeof(uint32_t));
|
---|
977 | if (RT_SUCCESS(rc))
|
---|
978 | rc = txsWaitForAck(&Pkt.Hdr);
|
---|
979 | }
|
---|
980 | else
|
---|
981 | rc = txsReplyRC(pPktHdr, rc, "RTFileRead");
|
---|
982 | break;
|
---|
983 | }
|
---|
984 |
|
---|
985 | uMyCrc32 = RTCrc32Process(uMyCrc32, &Pkt.ab[0], cbRead);
|
---|
986 | Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
|
---|
987 | rc = txsReplyInternal(&Pkt.Hdr, "DATA ", cbRead + sizeof(uint32_t));
|
---|
988 | if (RT_FAILURE(rc))
|
---|
989 | break;
|
---|
990 | rc = txsWaitForAck(&Pkt.Hdr);
|
---|
991 | if (RT_FAILURE(rc))
|
---|
992 | break;
|
---|
993 | }
|
---|
994 |
|
---|
995 | RTFileClose(hFile);
|
---|
996 | }
|
---|
997 | else
|
---|
998 | rc = txsReplyRC(pPktHdr, rc, "RTFileOpen(,\"%s\",)", pszPath);
|
---|
999 |
|
---|
1000 | RTStrFree(pszPath);
|
---|
1001 | return rc;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * Uploads a file from the client.
|
---|
1006 | *
|
---|
1007 | * The transfer sends a stream of DATA packets (0 or more) and ends it all with
|
---|
1008 | * a DATA EOF packet. We ACK each of these, so that if a write error occurs we
|
---|
1009 | * can abort the transfer straight away.
|
---|
1010 | *
|
---|
1011 | * @returns IPRT status code from send.
|
---|
1012 | * @param pPktHdr The put file packet.
|
---|
1013 | * @param fHasMode Set if the packet starts with a mode field.
|
---|
1014 | */
|
---|
1015 | static int txsDoPutFile(PCTXSPKTHDR pPktHdr, bool fHasMode)
|
---|
1016 | {
|
---|
1017 | int rc;
|
---|
1018 | RTFMODE fMode = 0;
|
---|
1019 | char *pszPath;
|
---|
1020 | if (!fHasMode)
|
---|
1021 | {
|
---|
1022 | if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc))
|
---|
1023 | return rc;
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | {
|
---|
1027 | /* After the packet header follows a mode mask and the remainder of
|
---|
1028 | the packet is the zero terminated file name. */
|
---|
1029 | size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2;
|
---|
1030 | if (pPktHdr->cb < cbMin)
|
---|
1031 | return txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
1032 | if (!txsIsStringValid(pPktHdr, "file", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc))
|
---|
1033 | return rc;
|
---|
1034 | fMode = *(RTFMODE const *)(pPktHdr + 1);
|
---|
1035 | fMode <<= RTFILE_O_CREATE_MODE_SHIFT;
|
---|
1036 | fMode &= RTFILE_O_CREATE_MODE_MASK;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | RTFILE hFile;
|
---|
1040 | rc = RTFileOpen(&hFile, pszPath, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE | fMode);
|
---|
1041 | if (RT_SUCCESS(rc))
|
---|
1042 | {
|
---|
1043 | bool fSuccess = false;
|
---|
1044 | rc = txsReplyAck(pPktHdr);
|
---|
1045 | if (RT_SUCCESS(rc))
|
---|
1046 | {
|
---|
1047 | if (fMode)
|
---|
1048 | RTFileSetMode(hFile, fMode);
|
---|
1049 |
|
---|
1050 | /*
|
---|
1051 | * Read client command packets and process them.
|
---|
1052 | */
|
---|
1053 | uint32_t uMyCrc32 = RTCrc32Start();
|
---|
1054 | for (;;)
|
---|
1055 | {
|
---|
1056 | PTXSPKTHDR pDataPktHdr;
|
---|
1057 | rc = txsRecvPkt(&pDataPktHdr, false /*fAutoRetryOnFailure*/);
|
---|
1058 | if (RT_FAILURE(rc))
|
---|
1059 | break;
|
---|
1060 |
|
---|
1061 | if (txsIsSameOpcode(pDataPktHdr, "DATA"))
|
---|
1062 | {
|
---|
1063 | size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(uint32_t);
|
---|
1064 | if (pDataPktHdr->cb >= cbMin)
|
---|
1065 | {
|
---|
1066 | size_t cbData = pDataPktHdr->cb - cbMin;
|
---|
1067 | const void *pvData = (const char *)pDataPktHdr + cbMin;
|
---|
1068 | uint32_t uCrc32 = *(uint32_t const *)(pDataPktHdr + 1);
|
---|
1069 |
|
---|
1070 | uMyCrc32 = RTCrc32Process(uMyCrc32, pvData, cbData);
|
---|
1071 | if (RTCrc32Finish(uMyCrc32) == uCrc32)
|
---|
1072 | {
|
---|
1073 | rc = RTFileWrite(hFile, pvData, cbData, NULL);
|
---|
1074 | if (RT_SUCCESS(rc))
|
---|
1075 | {
|
---|
1076 | rc = txsReplyAck(pDataPktHdr);
|
---|
1077 | RTMemFree(pDataPktHdr);
|
---|
1078 | continue;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | rc = txsReplyRC(pDataPktHdr, rc, "RTFileWrite");
|
---|
1082 | }
|
---|
1083 | else
|
---|
1084 | rc = txsReplyFailure(pDataPktHdr, "BAD DCRC", "mycrc=%#x your=%#x", uMyCrc32, uCrc32);
|
---|
1085 | }
|
---|
1086 | else
|
---|
1087 | rc = txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
1088 | }
|
---|
1089 | else if (txsIsSameOpcode(pDataPktHdr, "DATA EOF"))
|
---|
1090 | {
|
---|
1091 | if (pDataPktHdr->cb == sizeof(TXSPKTHDR) + sizeof(uint32_t))
|
---|
1092 | {
|
---|
1093 | uint32_t uCrc32 = *(uint32_t const *)(pDataPktHdr + 1);
|
---|
1094 | if (RTCrc32Finish(uMyCrc32) == uCrc32)
|
---|
1095 | {
|
---|
1096 | rc = txsReplyAck(pDataPktHdr);
|
---|
1097 | fSuccess = RT_SUCCESS(rc);
|
---|
1098 | }
|
---|
1099 | else
|
---|
1100 | rc = txsReplyFailure(pDataPktHdr, "BAD DCRC", "mycrc=%#x your=%#x", uMyCrc32, uCrc32);
|
---|
1101 | }
|
---|
1102 | else
|
---|
1103 | rc = txsReplyAck(pDataPktHdr);
|
---|
1104 | }
|
---|
1105 | else if (txsIsSameOpcode(pDataPktHdr, "ABORT"))
|
---|
1106 | rc = txsReplyAck(pDataPktHdr);
|
---|
1107 | else
|
---|
1108 | rc = txsReplyFailure(pDataPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known or not recognized during PUT FILE", pDataPktHdr->achOpcode);
|
---|
1109 | RTMemFree(pDataPktHdr);
|
---|
1110 | break;
|
---|
1111 | }
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | RTFileClose(hFile);
|
---|
1115 |
|
---|
1116 | /*
|
---|
1117 | * Delete the file on failure.
|
---|
1118 | */
|
---|
1119 | if (!fSuccess)
|
---|
1120 | RTFileDelete(pszPath);
|
---|
1121 | }
|
---|
1122 | else
|
---|
1123 | rc = txsReplyRC(pPktHdr, rc, "RTFileOpen(,\"%s\",)", pszPath);
|
---|
1124 |
|
---|
1125 | RTStrFree(pszPath);
|
---|
1126 | return rc;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | /**
|
---|
1130 | * List the entries in the specified directory.
|
---|
1131 | *
|
---|
1132 | * @returns IPRT status code from send.
|
---|
1133 | * @param pPktHdr The list packet.
|
---|
1134 | */
|
---|
1135 | static int txsDoList(PCTXSPKTHDR pPktHdr)
|
---|
1136 | {
|
---|
1137 | int rc;
|
---|
1138 | char *pszPath;
|
---|
1139 | if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc))
|
---|
1140 | return rc;
|
---|
1141 |
|
---|
1142 | rc = txsReplyNotImplemented(pPktHdr);
|
---|
1143 |
|
---|
1144 | RTStrFree(pszPath);
|
---|
1145 | return rc;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | /**
|
---|
1149 | * Worker for STAT and LSTAT for packing down the file info reply.
|
---|
1150 | *
|
---|
1151 | * @returns IPRT status code from send.
|
---|
1152 | * @param pInfo The info to pack down.
|
---|
1153 | */
|
---|
1154 | static int txsReplyObjInfo(PCRTFSOBJINFO pInfo)
|
---|
1155 | {
|
---|
1156 | struct
|
---|
1157 | {
|
---|
1158 | TXSPKTHDR Hdr;
|
---|
1159 | int64_t cbObject;
|
---|
1160 | int64_t cbAllocated;
|
---|
1161 | int64_t nsAccessTime;
|
---|
1162 | int64_t nsModificationTime;
|
---|
1163 | int64_t nsChangeTime;
|
---|
1164 | int64_t nsBirthTime;
|
---|
1165 | uint32_t fMode;
|
---|
1166 | uint32_t uid;
|
---|
1167 | uint32_t gid;
|
---|
1168 | uint32_t cHardLinks;
|
---|
1169 | uint64_t INodeIdDevice;
|
---|
1170 | uint64_t INodeId;
|
---|
1171 | uint64_t Device;
|
---|
1172 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
1173 | } Pkt;
|
---|
1174 |
|
---|
1175 | Pkt.cbObject = pInfo->cbObject;
|
---|
1176 | Pkt.cbAllocated = pInfo->cbAllocated;
|
---|
1177 | Pkt.nsAccessTime = RTTimeSpecGetNano(&pInfo->AccessTime);
|
---|
1178 | Pkt.nsModificationTime = RTTimeSpecGetNano(&pInfo->ModificationTime);
|
---|
1179 | Pkt.nsChangeTime = RTTimeSpecGetNano(&pInfo->ChangeTime);
|
---|
1180 | Pkt.nsBirthTime = RTTimeSpecGetNano(&pInfo->BirthTime);
|
---|
1181 | Pkt.fMode = pInfo->Attr.fMode;
|
---|
1182 | Pkt.uid = pInfo->Attr.u.Unix.uid;
|
---|
1183 | Pkt.gid = pInfo->Attr.u.Unix.gid;
|
---|
1184 | Pkt.cHardLinks = pInfo->Attr.u.Unix.cHardlinks;
|
---|
1185 | Pkt.INodeIdDevice = pInfo->Attr.u.Unix.INodeIdDevice;
|
---|
1186 | Pkt.INodeId = pInfo->Attr.u.Unix.INodeId;
|
---|
1187 | Pkt.Device = pInfo->Attr.u.Unix.Device;
|
---|
1188 |
|
---|
1189 | return txsReplyInternal(&Pkt.Hdr, "FILEINFO", sizeof(Pkt) - TXSPKT_ALIGNMENT - sizeof(TXSPKTHDR));
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /**
|
---|
1193 | * Get info about a file system object, following all but the symbolic links
|
---|
1194 | * except in the final path component.
|
---|
1195 | *
|
---|
1196 | * @returns IPRT status code from send.
|
---|
1197 | * @param pPktHdr The lstat packet.
|
---|
1198 | */
|
---|
1199 | static int txsDoLStat(PCTXSPKTHDR pPktHdr)
|
---|
1200 | {
|
---|
1201 | int rc;
|
---|
1202 | char *pszPath;
|
---|
1203 | if (!txsIsStringPktValid(pPktHdr, "path", &pszPath, &rc))
|
---|
1204 | return rc;
|
---|
1205 |
|
---|
1206 | RTFSOBJINFO Info;
|
---|
1207 | rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
1208 | if (RT_SUCCESS(rc))
|
---|
1209 | rc = txsReplyObjInfo(&Info);
|
---|
1210 | else
|
---|
1211 | rc = txsReplyRC(pPktHdr, rc, "RTPathQueryInfoEx(\"%s\",,UNIX,ON_LINK)", pszPath);
|
---|
1212 |
|
---|
1213 | RTStrFree(pszPath);
|
---|
1214 | return rc;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /**
|
---|
1218 | * Get info about a file system object, following all symbolic links.
|
---|
1219 | *
|
---|
1220 | * @returns IPRT status code from send.
|
---|
1221 | * @param pPktHdr The stat packet.
|
---|
1222 | */
|
---|
1223 | static int txsDoStat(PCTXSPKTHDR pPktHdr)
|
---|
1224 | {
|
---|
1225 | int rc;
|
---|
1226 | char *pszPath;
|
---|
1227 | if (!txsIsStringPktValid(pPktHdr, "path", &pszPath, &rc))
|
---|
1228 | return rc;
|
---|
1229 |
|
---|
1230 | RTFSOBJINFO Info;
|
---|
1231 | rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_UNIX, RTPATH_F_FOLLOW_LINK);
|
---|
1232 | if (RT_SUCCESS(rc))
|
---|
1233 | rc = txsReplyObjInfo(&Info);
|
---|
1234 | else
|
---|
1235 | rc = txsReplyRC(pPktHdr, rc, "RTPathQueryInfoEx(\"%s\",,UNIX,FOLLOW_LINK)", pszPath);
|
---|
1236 |
|
---|
1237 | RTStrFree(pszPath);
|
---|
1238 | return rc;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Checks if the specified path is a symbolic link.
|
---|
1243 | *
|
---|
1244 | * @returns IPRT status code from send.
|
---|
1245 | * @param pPktHdr The issymlnk packet.
|
---|
1246 | */
|
---|
1247 | static int txsDoIsSymlnk(PCTXSPKTHDR pPktHdr)
|
---|
1248 | {
|
---|
1249 | int rc;
|
---|
1250 | char *pszPath;
|
---|
1251 | if (!txsIsStringPktValid(pPktHdr, "symlink", &pszPath, &rc))
|
---|
1252 | return rc;
|
---|
1253 |
|
---|
1254 | RTFSOBJINFO Info;
|
---|
1255 | rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
1256 | if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(Info.Attr.fMode))
|
---|
1257 | rc = txsReplySimple(pPktHdr, "TRUE ");
|
---|
1258 | else
|
---|
1259 | rc = txsReplySimple(pPktHdr, "FALSE ");
|
---|
1260 |
|
---|
1261 | RTStrFree(pszPath);
|
---|
1262 | return rc;
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /**
|
---|
1266 | * Checks if the specified path is a file or not.
|
---|
1267 | *
|
---|
1268 | * If the final path element is a symbolic link to a file, we'll return
|
---|
1269 | * FALSE.
|
---|
1270 | *
|
---|
1271 | * @returns IPRT status code from send.
|
---|
1272 | * @param pPktHdr The isfile packet.
|
---|
1273 | */
|
---|
1274 | static int txsDoIsFile(PCTXSPKTHDR pPktHdr)
|
---|
1275 | {
|
---|
1276 | int rc;
|
---|
1277 | char *pszPath;
|
---|
1278 | if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc))
|
---|
1279 | return rc;
|
---|
1280 |
|
---|
1281 | RTFSOBJINFO Info;
|
---|
1282 | rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
1283 | if (RT_SUCCESS(rc) && RTFS_IS_FILE(Info.Attr.fMode))
|
---|
1284 | rc = txsReplySimple(pPktHdr, "TRUE ");
|
---|
1285 | else
|
---|
1286 | rc = txsReplySimple(pPktHdr, "FALSE ");
|
---|
1287 |
|
---|
1288 | RTStrFree(pszPath);
|
---|
1289 | return rc;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | /**
|
---|
1293 | * Checks if the specified path is a directory or not.
|
---|
1294 | *
|
---|
1295 | * If the final path element is a symbolic link to a directory, we'll return
|
---|
1296 | * FALSE.
|
---|
1297 | *
|
---|
1298 | * @returns IPRT status code from send.
|
---|
1299 | * @param pPktHdr The isdir packet.
|
---|
1300 | */
|
---|
1301 | static int txsDoIsDir(PCTXSPKTHDR pPktHdr)
|
---|
1302 | {
|
---|
1303 | int rc;
|
---|
1304 | char *pszPath;
|
---|
1305 | if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc))
|
---|
1306 | return rc;
|
---|
1307 |
|
---|
1308 | RTFSOBJINFO Info;
|
---|
1309 | rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
1310 | if (RT_SUCCESS(rc) && RTFS_IS_DIRECTORY(Info.Attr.fMode))
|
---|
1311 | rc = txsReplySimple(pPktHdr, "TRUE ");
|
---|
1312 | else
|
---|
1313 | rc = txsReplySimple(pPktHdr, "FALSE ");
|
---|
1314 |
|
---|
1315 | RTStrFree(pszPath);
|
---|
1316 | return rc;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | /**
|
---|
1320 | * Changes the owner of a file, directory or symbolic link.
|
---|
1321 | *
|
---|
1322 | * @returns IPRT status code from send.
|
---|
1323 | * @param pPktHdr The chmod packet.
|
---|
1324 | */
|
---|
1325 | static int txsDoChOwn(PCTXSPKTHDR pPktHdr)
|
---|
1326 | {
|
---|
1327 | #ifdef RT_OS_WINDOWS
|
---|
1328 | return txsReplyNotImplemented(pPktHdr);
|
---|
1329 | #else
|
---|
1330 | /* After the packet header follows a 32-bit UID and 32-bit GID, while the
|
---|
1331 | remainder of the packet is the zero terminated path. */
|
---|
1332 | size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2;
|
---|
1333 | if (pPktHdr->cb < cbMin)
|
---|
1334 | return txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
1335 |
|
---|
1336 | int rc;
|
---|
1337 | char *pszPath;
|
---|
1338 | if (!txsIsStringValid(pPktHdr, "path", (const char *)(pPktHdr + 1) + sizeof(uint32_t) * 2, &pszPath, NULL, &rc))
|
---|
1339 | return rc;
|
---|
1340 |
|
---|
1341 | uint32_t uid = ((uint32_t const *)(pPktHdr + 1))[0];
|
---|
1342 | uint32_t gid = ((uint32_t const *)(pPktHdr + 1))[1];
|
---|
1343 |
|
---|
1344 | rc = RTPathSetOwnerEx(pszPath, uid, gid, RTPATH_F_ON_LINK);
|
---|
1345 |
|
---|
1346 | rc = txsReplyRC(pPktHdr, rc, "RTPathSetOwnerEx(\"%s\", %u, %u)", pszPath, uid, gid);
|
---|
1347 | RTStrFree(pszPath);
|
---|
1348 | return rc;
|
---|
1349 | #endif
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /**
|
---|
1353 | * Changes the mode of a file or directory.
|
---|
1354 | *
|
---|
1355 | * @returns IPRT status code from send.
|
---|
1356 | * @param pPktHdr The chmod packet.
|
---|
1357 | */
|
---|
1358 | static int txsDoChMod(PCTXSPKTHDR pPktHdr)
|
---|
1359 | {
|
---|
1360 | /* After the packet header follows a mode mask and the remainder of
|
---|
1361 | the packet is the zero terminated file name. */
|
---|
1362 | size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2;
|
---|
1363 | if (pPktHdr->cb < cbMin)
|
---|
1364 | return txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
1365 |
|
---|
1366 | int rc;
|
---|
1367 | char *pszPath;
|
---|
1368 | if (!txsIsStringValid(pPktHdr, "path", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc))
|
---|
1369 | return rc;
|
---|
1370 |
|
---|
1371 | RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1);
|
---|
1372 |
|
---|
1373 | rc = RTPathSetMode(pszPath, fMode);
|
---|
1374 |
|
---|
1375 | rc = txsReplyRC(pPktHdr, rc, "RTPathSetMode(\"%s\", %o)", pszPath, fMode);
|
---|
1376 | RTStrFree(pszPath);
|
---|
1377 | return rc;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | /**
|
---|
1381 | * Removes a directory tree.
|
---|
1382 | *
|
---|
1383 | * @returns IPRT status code from send.
|
---|
1384 | * @param pPktHdr The rmtree packet.
|
---|
1385 | */
|
---|
1386 | static int txsDoRmTree(PCTXSPKTHDR pPktHdr)
|
---|
1387 | {
|
---|
1388 | int rc;
|
---|
1389 | char *pszPath;
|
---|
1390 | if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc))
|
---|
1391 | return rc;
|
---|
1392 |
|
---|
1393 | rc = RTDirRemoveRecursive(pszPath, 0 /*fFlags*/);
|
---|
1394 |
|
---|
1395 | rc = txsReplyRC(pPktHdr, rc, "RTDirRemoveRecusive(\"%s\",0)", pszPath);
|
---|
1396 | RTStrFree(pszPath);
|
---|
1397 | return rc;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * Removes a symbolic link.
|
---|
1402 | *
|
---|
1403 | * @returns IPRT status code from send.
|
---|
1404 | * @param pPktHdr The rmsymlink packet.
|
---|
1405 | */
|
---|
1406 | static int txsDoRmSymlnk(PCTXSPKTHDR pPktHdr)
|
---|
1407 | {
|
---|
1408 | int rc;
|
---|
1409 | char *pszPath;
|
---|
1410 | if (!txsIsStringPktValid(pPktHdr, "symlink", &pszPath, &rc))
|
---|
1411 | return rc;
|
---|
1412 |
|
---|
1413 | rc = RTSymlinkDelete(pszPath, 0);
|
---|
1414 |
|
---|
1415 | rc = txsReplyRC(pPktHdr, rc, "RTSymlinkDelete(\"%s\")", pszPath);
|
---|
1416 | RTStrFree(pszPath);
|
---|
1417 | return rc;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | /**
|
---|
1421 | * Removes a file.
|
---|
1422 | *
|
---|
1423 | * @returns IPRT status code from send.
|
---|
1424 | * @param pPktHdr The rmfile packet.
|
---|
1425 | */
|
---|
1426 | static int txsDoRmFile(PCTXSPKTHDR pPktHdr)
|
---|
1427 | {
|
---|
1428 | int rc;
|
---|
1429 | char *pszPath;
|
---|
1430 | if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc))
|
---|
1431 | return rc;
|
---|
1432 |
|
---|
1433 | rc = RTFileDelete(pszPath);
|
---|
1434 |
|
---|
1435 | rc = txsReplyRC(pPktHdr, rc, "RTFileDelete(\"%s\")", pszPath);
|
---|
1436 | RTStrFree(pszPath);
|
---|
1437 | return rc;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /**
|
---|
1441 | * Removes a directory.
|
---|
1442 | *
|
---|
1443 | * @returns IPRT status code from send.
|
---|
1444 | * @param pPktHdr The rmdir packet.
|
---|
1445 | */
|
---|
1446 | static int txsDoRmDir(PCTXSPKTHDR pPktHdr)
|
---|
1447 | {
|
---|
1448 | int rc;
|
---|
1449 | char *pszPath;
|
---|
1450 | if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc))
|
---|
1451 | return rc;
|
---|
1452 |
|
---|
1453 | rc = RTDirRemove(pszPath);
|
---|
1454 |
|
---|
1455 | rc = txsReplyRC(pPktHdr, rc, "RTDirRemove(\"%s\")", pszPath);
|
---|
1456 | RTStrFree(pszPath);
|
---|
1457 | return rc;
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | /**
|
---|
1461 | * Creates a symbolic link.
|
---|
1462 | *
|
---|
1463 | * @returns IPRT status code from send.
|
---|
1464 | * @param pPktHdr The mksymlnk packet.
|
---|
1465 | */
|
---|
1466 | static int txsDoMkSymlnk(PCTXSPKTHDR pPktHdr)
|
---|
1467 | {
|
---|
1468 | return txsReplyNotImplemented(pPktHdr);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | /**
|
---|
1472 | * Creates a directory and all its parents.
|
---|
1473 | *
|
---|
1474 | * @returns IPRT status code from send.
|
---|
1475 | * @param pPktHdr The mkdir -p packet.
|
---|
1476 | */
|
---|
1477 | static int txsDoMkDrPath(PCTXSPKTHDR pPktHdr)
|
---|
1478 | {
|
---|
1479 | /* The same format as the MKDIR command. */
|
---|
1480 | if (pPktHdr->cb < sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2)
|
---|
1481 | return txsReplyBadMinSize(pPktHdr, sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2);
|
---|
1482 |
|
---|
1483 | int rc;
|
---|
1484 | char *pszPath;
|
---|
1485 | if (!txsIsStringValid(pPktHdr, "dir", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc))
|
---|
1486 | return rc;
|
---|
1487 |
|
---|
1488 | RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1);
|
---|
1489 |
|
---|
1490 | rc = RTDirCreateFullPathEx(pszPath, fMode, RTDIRCREATE_FLAGS_IGNORE_UMASK);
|
---|
1491 |
|
---|
1492 | rc = txsReplyRC(pPktHdr, rc, "RTDirCreateFullPath(\"%s\", %#x)", pszPath, fMode);
|
---|
1493 | RTStrFree(pszPath);
|
---|
1494 | return rc;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | /**
|
---|
1498 | * Creates a directory.
|
---|
1499 | *
|
---|
1500 | * @returns IPRT status code from send.
|
---|
1501 | * @param pPktHdr The mkdir packet.
|
---|
1502 | */
|
---|
1503 | static int txsDoMkDir(PCTXSPKTHDR pPktHdr)
|
---|
1504 | {
|
---|
1505 | /* After the packet header follows a mode mask and the remainder of
|
---|
1506 | the packet is the zero terminated directory name. */
|
---|
1507 | size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2;
|
---|
1508 | if (pPktHdr->cb < cbMin)
|
---|
1509 | return txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
1510 |
|
---|
1511 | int rc;
|
---|
1512 | char *pszPath;
|
---|
1513 | if (!txsIsStringValid(pPktHdr, "dir", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc))
|
---|
1514 | return rc;
|
---|
1515 |
|
---|
1516 | RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1);
|
---|
1517 | rc = RTDirCreate(pszPath, fMode, RTDIRCREATE_FLAGS_IGNORE_UMASK);
|
---|
1518 |
|
---|
1519 | rc = txsReplyRC(pPktHdr, rc, "RTDirCreate(\"%s\", %#x)", pszPath, fMode);
|
---|
1520 | RTStrFree(pszPath);
|
---|
1521 | return rc;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | /**
|
---|
1525 | * Cleans up the scratch area.
|
---|
1526 | *
|
---|
1527 | * @returns IPRT status code from send.
|
---|
1528 | * @param pPktHdr The shutdown packet.
|
---|
1529 | */
|
---|
1530 | static int txsDoCleanup(PCTXSPKTHDR pPktHdr)
|
---|
1531 | {
|
---|
1532 | int rc = RTDirRemoveRecursive(g_szScratchPath, RTDIRRMREC_F_CONTENT_ONLY);
|
---|
1533 | return txsReplyRC(pPktHdr, rc, "RTDirRemoveRecursive(\"%s\", CONTENT_ONLY)", g_szScratchPath);
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | /**
|
---|
1537 | * Ejects the specified DVD/CD drive.
|
---|
1538 | *
|
---|
1539 | * @returns IPRT status code from send.
|
---|
1540 | * @param pPktHdr The eject packet.
|
---|
1541 | */
|
---|
1542 | static int txsDoCdEject(PCTXSPKTHDR pPktHdr)
|
---|
1543 | {
|
---|
1544 | /* After the packet header follows a uint32_t ordinal. */
|
---|
1545 | size_t const cbExpected = sizeof(TXSPKTHDR) + sizeof(uint32_t);
|
---|
1546 | if (pPktHdr->cb != cbExpected)
|
---|
1547 | return txsReplyBadSize(pPktHdr, cbExpected);
|
---|
1548 | uint32_t iOrdinal = *(uint32_t const *)(pPktHdr + 1);
|
---|
1549 |
|
---|
1550 | RTCDROM hCdrom;
|
---|
1551 | int rc = RTCdromOpenByOrdinal(iOrdinal, RTCDROM_O_CONTROL, &hCdrom);
|
---|
1552 | if (RT_FAILURE(rc))
|
---|
1553 | return txsReplyRC(pPktHdr, rc, "RTCdromOpenByOrdinal(%u, RTCDROM_O_CONTROL, )", iOrdinal);
|
---|
1554 | rc = RTCdromEject(hCdrom, true /*fForce*/);
|
---|
1555 | RTCdromRelease(hCdrom);
|
---|
1556 |
|
---|
1557 | return txsReplyRC(pPktHdr, rc, "RTCdromEject(ord=%u, fForce=true)", iOrdinal);
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | /**
|
---|
1561 | * Common worker for txsDoShutdown and txsDoReboot.
|
---|
1562 | *
|
---|
1563 | * @returns IPRT status code from send.
|
---|
1564 | * @param pPktHdr The reboot packet.
|
---|
1565 | * @param fAction Which action to take.
|
---|
1566 | */
|
---|
1567 | static int txsCommonShutdownReboot(PCTXSPKTHDR pPktHdr, uint32_t fAction)
|
---|
1568 | {
|
---|
1569 | /*
|
---|
1570 | * We ACK the reboot & shutdown before actually performing them, then we
|
---|
1571 | * terminate the transport layer.
|
---|
1572 | *
|
---|
1573 | * This is to make sure the client isn't stuck with a dead connection. The
|
---|
1574 | * transport layer termination also make sure we won't accept new
|
---|
1575 | * connections in case the client is too eager to reconnect to a rebooted
|
---|
1576 | * test victim. On the down side, we cannot easily report RTSystemShutdown
|
---|
1577 | * failures failures this way. But the client can kind of figure it out by
|
---|
1578 | * reconnecting and seeing that our UUID was unchanged.
|
---|
1579 | */
|
---|
1580 | int rc;
|
---|
1581 | if (pPktHdr->cb != sizeof(TXSPKTHDR))
|
---|
1582 | return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR));
|
---|
1583 | g_pTransport->pfnNotifyReboot();
|
---|
1584 | rc = txsReplyAck(pPktHdr);
|
---|
1585 | RTThreadSleep(2560); /* fudge factor */
|
---|
1586 | g_pTransport->pfnTerm();
|
---|
1587 |
|
---|
1588 | /*
|
---|
1589 | * Do the job, if it fails we'll restart the transport layer.
|
---|
1590 | */
|
---|
1591 | #if 0
|
---|
1592 | rc = VINF_SUCCESS;
|
---|
1593 | #else
|
---|
1594 | rc = RTSystemShutdown(0 /*cMsDelay*/,
|
---|
1595 | fAction | RTSYSTEM_SHUTDOWN_PLANNED | RTSYSTEM_SHUTDOWN_FORCE,
|
---|
1596 | "Test Execution Service");
|
---|
1597 | #endif
|
---|
1598 | if (RT_SUCCESS(rc))
|
---|
1599 | {
|
---|
1600 | RTMsgInfo(fAction == RTSYSTEM_SHUTDOWN_REBOOT ? "Rebooting...\n" : "Shutting down...\n");
|
---|
1601 | g_fTerminate = true;
|
---|
1602 | }
|
---|
1603 | else
|
---|
1604 | {
|
---|
1605 | RTMsgError("RTSystemShutdown w/ fAction=%#x failed: %Rrc", fAction, rc);
|
---|
1606 |
|
---|
1607 | int rc2 = g_pTransport->pfnInit();
|
---|
1608 | if (RT_FAILURE(rc2))
|
---|
1609 | {
|
---|
1610 | g_fTerminate = true;
|
---|
1611 | rc = rc2;
|
---|
1612 | }
|
---|
1613 | }
|
---|
1614 | return rc;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | /**
|
---|
1618 | * Shuts down the machine, powering it off if possible.
|
---|
1619 | *
|
---|
1620 | * @returns IPRT status code from send.
|
---|
1621 | * @param pPktHdr The shutdown packet.
|
---|
1622 | */
|
---|
1623 | static int txsDoShutdown(PCTXSPKTHDR pPktHdr)
|
---|
1624 | {
|
---|
1625 | return txsCommonShutdownReboot(pPktHdr, RTSYSTEM_SHUTDOWN_POWER_OFF_HALT);
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | /**
|
---|
1629 | * Reboots the machine.
|
---|
1630 | *
|
---|
1631 | * @returns IPRT status code from send.
|
---|
1632 | * @param pPktHdr The reboot packet.
|
---|
1633 | */
|
---|
1634 | static int txsDoReboot(PCTXSPKTHDR pPktHdr)
|
---|
1635 | {
|
---|
1636 | return txsCommonShutdownReboot(pPktHdr, RTSYSTEM_SHUTDOWN_REBOOT);
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 | /**
|
---|
1640 | * Verifies and acknowledges a "UUID" request.
|
---|
1641 | *
|
---|
1642 | * @returns IPRT status code.
|
---|
1643 | * @param pPktHdr The UUID packet.
|
---|
1644 | */
|
---|
1645 | static int txsDoUuid(PCTXSPKTHDR pPktHdr)
|
---|
1646 | {
|
---|
1647 | if (pPktHdr->cb != sizeof(TXSPKTHDR))
|
---|
1648 | return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR));
|
---|
1649 |
|
---|
1650 | struct
|
---|
1651 | {
|
---|
1652 | TXSPKTHDR Hdr;
|
---|
1653 | char szUuid[RTUUID_STR_LENGTH];
|
---|
1654 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
1655 | } Pkt;
|
---|
1656 |
|
---|
1657 | int rc = RTUuidToStr(&g_InstanceUuid, Pkt.szUuid, sizeof(Pkt.szUuid));
|
---|
1658 | if (RT_FAILURE(rc))
|
---|
1659 | return txsReplyRC(pPktHdr, rc, "RTUuidToStr");
|
---|
1660 | return txsReplyInternal(&Pkt.Hdr, "ACK UUID", strlen(Pkt.szUuid) + 1);
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | /**
|
---|
1664 | * Verifies and acknowledges a "BYE" request.
|
---|
1665 | *
|
---|
1666 | * @returns IPRT status code.
|
---|
1667 | * @param pPktHdr The bye packet.
|
---|
1668 | */
|
---|
1669 | static int txsDoBye(PCTXSPKTHDR pPktHdr)
|
---|
1670 | {
|
---|
1671 | int rc;
|
---|
1672 | if (pPktHdr->cb == sizeof(TXSPKTHDR))
|
---|
1673 | rc = txsReplyAck(pPktHdr);
|
---|
1674 | else
|
---|
1675 | rc = txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR));
|
---|
1676 | g_pTransport->pfnNotifyBye();
|
---|
1677 | return rc;
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | /**
|
---|
1681 | * Verifies and acknowledges a "VER" request.
|
---|
1682 | *
|
---|
1683 | * @returns IPRT status code.
|
---|
1684 | * @param pPktHdr The version packet.
|
---|
1685 | */
|
---|
1686 | static int txsDoVer(PCTXSPKTHDR pPktHdr)
|
---|
1687 | {
|
---|
1688 | if (pPktHdr->cb != sizeof(TXSPKTHDR))
|
---|
1689 | return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR));
|
---|
1690 |
|
---|
1691 | struct
|
---|
1692 | {
|
---|
1693 | TXSPKTHDR Hdr;
|
---|
1694 | char szVer[96];
|
---|
1695 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
1696 | } Pkt;
|
---|
1697 |
|
---|
1698 | if (RTStrPrintf2(Pkt.szVer, sizeof(Pkt.szVer), "%s r%s %s.%s (%s %s)",
|
---|
1699 | RTBldCfgVersion(), RTBldCfgRevisionStr(), KBUILD_TARGET, KBUILD_TARGET_ARCH, __DATE__, __TIME__) > 0)
|
---|
1700 | {
|
---|
1701 | return txsReplyInternal(&Pkt.Hdr, "ACK VER ", strlen(Pkt.szVer) + 1);
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | return txsReplyRC(pPktHdr, VERR_BUFFER_OVERFLOW, "RTStrPrintf2");
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | /**
|
---|
1708 | * Verifies and acknowledges a "HOWDY" request.
|
---|
1709 | *
|
---|
1710 | * @returns IPRT status code.
|
---|
1711 | * @param pPktHdr The howdy packet.
|
---|
1712 | */
|
---|
1713 | static int txsDoHowdy(PCTXSPKTHDR pPktHdr)
|
---|
1714 | {
|
---|
1715 | if (pPktHdr->cb != sizeof(TXSPKTHDR))
|
---|
1716 | return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR));
|
---|
1717 | int rc = txsReplyAck(pPktHdr);
|
---|
1718 | if (RT_SUCCESS(rc))
|
---|
1719 | {
|
---|
1720 | g_pTransport->pfnNotifyHowdy();
|
---|
1721 | RTDirRemoveRecursive(g_szScratchPath, RTDIRRMREC_F_CONTENT_ONLY);
|
---|
1722 | }
|
---|
1723 | return rc;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | /**
|
---|
1727 | * Replies according to the return code.
|
---|
1728 | *
|
---|
1729 | * @returns rcOperation and pTxsExec->rcReplySend.
|
---|
1730 | * @param pTxsExec The TXSEXEC instance.
|
---|
1731 | * @param rcOperation The status code to report.
|
---|
1732 | * @param pszOperationFmt The operation that failed. Typically giving the
|
---|
1733 | * function call with important arguments.
|
---|
1734 | * @param ... Arguments to the format string.
|
---|
1735 | */
|
---|
1736 | static int txsExecReplyRC(PTXSEXEC pTxsExec, int rcOperation, const char *pszOperationFmt, ...)
|
---|
1737 | {
|
---|
1738 | AssertStmt(RT_FAILURE_NP(rcOperation), rcOperation = VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
1739 |
|
---|
1740 | char szOperation[128];
|
---|
1741 | va_list va;
|
---|
1742 | va_start(va, pszOperationFmt);
|
---|
1743 | RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va);
|
---|
1744 | va_end(va);
|
---|
1745 |
|
---|
1746 | pTxsExec->rcReplySend = txsReplyFailure(pTxsExec->pPktHdr, "FAILED ",
|
---|
1747 | "%s failed with rc=%Rrc (opcode '%.8s')",
|
---|
1748 | szOperation, rcOperation, pTxsExec->pPktHdr->achOpcode);
|
---|
1749 | return rcOperation;
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 |
|
---|
1753 | /**
|
---|
1754 | * Sends the process exit status reply to the TXS client.
|
---|
1755 | *
|
---|
1756 | * @returns IPRT status code of the send.
|
---|
1757 | * @param pTxsExec The TXSEXEC instance.
|
---|
1758 | * @param fProcessAlive Whether the process is still alive (against our
|
---|
1759 | * will).
|
---|
1760 | * @param fProcessTimedOut Whether the process timed out.
|
---|
1761 | * @param MsProcessKilled When the process was killed, UINT64_MAX if not.
|
---|
1762 | */
|
---|
1763 | static int txsExecSendExitStatus(PTXSEXEC pTxsExec, bool fProcessAlive, bool fProcessTimedOut, uint64_t MsProcessKilled)
|
---|
1764 | {
|
---|
1765 | int rc;
|
---|
1766 | if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX)
|
---|
1767 | {
|
---|
1768 | rc = txsReplySimple(pTxsExec->pPktHdr, "PROC TOK");
|
---|
1769 | if (g_fDisplayOutput)
|
---|
1770 | RTPrintf("txs: Process timed out and was killed\n");
|
---|
1771 | }
|
---|
1772 | else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX)
|
---|
1773 | {
|
---|
1774 | rc = txsReplySimple(pTxsExec->pPktHdr, "PROC TOA");
|
---|
1775 | if (g_fDisplayOutput)
|
---|
1776 | RTPrintf("txs: Process timed out and was not killed successfully\n");
|
---|
1777 | }
|
---|
1778 | else if (g_fTerminate && (fProcessAlive || MsProcessKilled != UINT64_MAX))
|
---|
1779 | rc = txsReplySimple(pTxsExec->pPktHdr, "PROC DWN");
|
---|
1780 | else if (fProcessAlive)
|
---|
1781 | {
|
---|
1782 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "Doofus! process is alive when it should not");
|
---|
1783 | AssertFailed();
|
---|
1784 | }
|
---|
1785 | else if (MsProcessKilled != UINT64_MAX)
|
---|
1786 | {
|
---|
1787 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "Doofus! process has been killed when it should not");
|
---|
1788 | AssertFailed();
|
---|
1789 | }
|
---|
1790 | else if ( pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL
|
---|
1791 | && pTxsExec->ProcessStatus.iStatus == 0)
|
---|
1792 | {
|
---|
1793 | rc = txsReplySimple(pTxsExec->pPktHdr, "PROC OK ");
|
---|
1794 | if (g_fDisplayOutput)
|
---|
1795 | RTPrintf("txs: Process exited with status: 0\n");
|
---|
1796 | }
|
---|
1797 | else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL)
|
---|
1798 | {
|
---|
1799 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC NOK", "%d", pTxsExec->ProcessStatus.iStatus);
|
---|
1800 | if (g_fDisplayOutput)
|
---|
1801 | RTPrintf("txs: Process exited with status: %d\n", pTxsExec->ProcessStatus.iStatus);
|
---|
1802 | }
|
---|
1803 | else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL)
|
---|
1804 | {
|
---|
1805 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC SIG", "%d", pTxsExec->ProcessStatus.iStatus);
|
---|
1806 | if (g_fDisplayOutput)
|
---|
1807 | RTPrintf("txs: Process exited with status: signal %d\n", pTxsExec->ProcessStatus.iStatus);
|
---|
1808 | }
|
---|
1809 | else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_ABEND)
|
---|
1810 | {
|
---|
1811 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC ABD", "");
|
---|
1812 | if (g_fDisplayOutput)
|
---|
1813 | RTPrintf("txs: Process exited with status: abend\n");
|
---|
1814 | }
|
---|
1815 | else
|
---|
1816 | {
|
---|
1817 | rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "enmReason=%d iStatus=%d",
|
---|
1818 | pTxsExec->ProcessStatus.enmReason, pTxsExec->ProcessStatus.iStatus);
|
---|
1819 | AssertMsgFailed(("enmReason=%d iStatus=%d", pTxsExec->ProcessStatus.enmReason, pTxsExec->ProcessStatus.iStatus));
|
---|
1820 | }
|
---|
1821 | return rc;
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | /**
|
---|
1825 | * Handle pending output data or error on standard out, standard error or the
|
---|
1826 | * test pipe.
|
---|
1827 | *
|
---|
1828 | * @returns IPRT status code from client send.
|
---|
1829 | * @param hPollSet The polling set.
|
---|
1830 | * @param fPollEvt The event mask returned by RTPollNoResume.
|
---|
1831 | * @param phPipeR The pipe handle.
|
---|
1832 | * @param puCrc32 The current CRC-32 of the stream. (In/Out)
|
---|
1833 | * @param enmHndId The handle ID.
|
---|
1834 | * @param pszOpcode The opcode for the data upload.
|
---|
1835 | *
|
---|
1836 | * @todo Put the last 4 parameters into a struct!
|
---|
1837 | */
|
---|
1838 | static int txsDoExecHlpHandleOutputEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phPipeR,
|
---|
1839 | uint32_t *puCrc32, TXSEXECHNDID enmHndId, const char *pszOpcode)
|
---|
1840 | {
|
---|
1841 | Log(("txsDoExecHlpHandleOutputEvent: %s fPollEvt=%#x\n", pszOpcode, fPollEvt));
|
---|
1842 |
|
---|
1843 | /*
|
---|
1844 | * Try drain the pipe before acting on any errors.
|
---|
1845 | */
|
---|
1846 | int rc = VINF_SUCCESS;
|
---|
1847 | struct
|
---|
1848 | {
|
---|
1849 | TXSPKTHDR Hdr;
|
---|
1850 | uint32_t uCrc32;
|
---|
1851 | char abBuf[_64K];
|
---|
1852 | char abPadding[TXSPKT_ALIGNMENT];
|
---|
1853 | } Pkt;
|
---|
1854 | size_t cbRead;
|
---|
1855 | int rc2 = RTPipeRead(*phPipeR, Pkt.abBuf, sizeof(Pkt.abBuf), &cbRead);
|
---|
1856 | if (RT_SUCCESS(rc2) && cbRead)
|
---|
1857 | {
|
---|
1858 | Log(("Crc32=%#x ", *puCrc32));
|
---|
1859 | *puCrc32 = RTCrc32Process(*puCrc32, Pkt.abBuf, cbRead);
|
---|
1860 | Log(("cbRead=%#x Crc32=%#x \n", cbRead, *puCrc32));
|
---|
1861 | Pkt.uCrc32 = RTCrc32Finish(*puCrc32);
|
---|
1862 | if (g_fDisplayOutput)
|
---|
1863 | {
|
---|
1864 | if (enmHndId == TXSEXECHNDID_STDOUT)
|
---|
1865 | RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf);
|
---|
1866 | else if (enmHndId == TXSEXECHNDID_STDERR)
|
---|
1867 | RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf);
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | rc = txsReplyInternal(&Pkt.Hdr, pszOpcode, cbRead + sizeof(uint32_t));
|
---|
1871 |
|
---|
1872 | /* Make sure we go another poll round in case there was too much data
|
---|
1873 | for the buffer to hold. */
|
---|
1874 | fPollEvt &= RTPOLL_EVT_ERROR;
|
---|
1875 | }
|
---|
1876 | else if (RT_FAILURE(rc2))
|
---|
1877 | {
|
---|
1878 | fPollEvt |= RTPOLL_EVT_ERROR;
|
---|
1879 | AssertMsg(rc2 == VERR_BROKEN_PIPE, ("%Rrc\n", rc));
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | /*
|
---|
1883 | * If an error was raised signalled,
|
---|
1884 | */
|
---|
1885 | if (fPollEvt & RTPOLL_EVT_ERROR)
|
---|
1886 | {
|
---|
1887 | rc2 = RTPollSetRemove(hPollSet, enmHndId);
|
---|
1888 | AssertRC(rc2);
|
---|
1889 |
|
---|
1890 | rc2 = RTPipeClose(*phPipeR);
|
---|
1891 | AssertRC(rc2);
|
---|
1892 | *phPipeR = NIL_RTPIPE;
|
---|
1893 | }
|
---|
1894 | return rc;
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | /**
|
---|
1898 | * Try write some more data to the standard input of the child.
|
---|
1899 | *
|
---|
1900 | * @returns IPRT status code.
|
---|
1901 | * @param pStdInBuf The standard input buffer.
|
---|
1902 | * @param hStdInW The standard input pipe.
|
---|
1903 | */
|
---|
1904 | static int txsDoExecHlpWriteStdIn(PTXSEXECSTDINBUF pStdInBuf, RTPIPE hStdInW)
|
---|
1905 | {
|
---|
1906 | size_t cbToWrite = pStdInBuf->cb - pStdInBuf->off;
|
---|
1907 | size_t cbWritten;
|
---|
1908 | int rc = RTPipeWrite(hStdInW, &pStdInBuf->pch[pStdInBuf->off], cbToWrite, &cbWritten);
|
---|
1909 | if (RT_SUCCESS(rc))
|
---|
1910 | {
|
---|
1911 | Assert(cbWritten == cbToWrite);
|
---|
1912 | pStdInBuf->off += cbWritten;
|
---|
1913 | }
|
---|
1914 | return rc;
|
---|
1915 | }
|
---|
1916 |
|
---|
1917 | /**
|
---|
1918 | * Handle an error event on standard input.
|
---|
1919 | *
|
---|
1920 | * @param hPollSet The polling set.
|
---|
1921 | * @param fPollEvt The event mask returned by RTPollNoResume.
|
---|
1922 | * @param phStdInW The standard input pipe handle.
|
---|
1923 | * @param pStdInBuf The standard input buffer.
|
---|
1924 | */
|
---|
1925 | static void txsDoExecHlpHandleStdInErrorEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
|
---|
1926 | PTXSEXECSTDINBUF pStdInBuf)
|
---|
1927 | {
|
---|
1928 | NOREF(fPollEvt);
|
---|
1929 | int rc2;
|
---|
1930 | if (pStdInBuf->off < pStdInBuf->cb)
|
---|
1931 | {
|
---|
1932 | rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE);
|
---|
1933 | AssertRC(rc2);
|
---|
1934 | }
|
---|
1935 |
|
---|
1936 | rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN);
|
---|
1937 | AssertRC(rc2);
|
---|
1938 |
|
---|
1939 | rc2 = RTPipeClose(*phStdInW);
|
---|
1940 | AssertRC(rc2);
|
---|
1941 | *phStdInW = NIL_RTPIPE;
|
---|
1942 |
|
---|
1943 | RTMemFree(pStdInBuf->pch);
|
---|
1944 | pStdInBuf->pch = NULL;
|
---|
1945 | pStdInBuf->off = 0;
|
---|
1946 | pStdInBuf->cb = 0;
|
---|
1947 | pStdInBuf->cbAllocated = 0;
|
---|
1948 | pStdInBuf->fBitBucket = true;
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 | /**
|
---|
1952 | * Handle an event indicating we can write to the standard input pipe of the
|
---|
1953 | * child process.
|
---|
1954 | *
|
---|
1955 | * @param hPollSet The polling set.
|
---|
1956 | * @param fPollEvt The event mask returned by RTPollNoResume.
|
---|
1957 | * @param phStdInW The standard input pipe.
|
---|
1958 | * @param pStdInBuf The standard input buffer.
|
---|
1959 | */
|
---|
1960 | static void txsDoExecHlpHandleStdInWritableEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
|
---|
1961 | PTXSEXECSTDINBUF pStdInBuf)
|
---|
1962 | {
|
---|
1963 | int rc;
|
---|
1964 | if (!(fPollEvt & RTPOLL_EVT_ERROR))
|
---|
1965 | {
|
---|
1966 | rc = txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW);
|
---|
1967 | if (RT_FAILURE(rc) && rc != VERR_BAD_PIPE)
|
---|
1968 | {
|
---|
1969 | /** @todo do we need to do something about this error condition? */
|
---|
1970 | AssertRC(rc);
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | if (pStdInBuf->off < pStdInBuf->cb)
|
---|
1974 | {
|
---|
1975 | rc = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE);
|
---|
1976 | AssertRC(rc);
|
---|
1977 | }
|
---|
1978 | }
|
---|
1979 | else
|
---|
1980 | txsDoExecHlpHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf);
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | /**
|
---|
1984 | * Handle a transport event or successful pfnPollIn() call.
|
---|
1985 | *
|
---|
1986 | * @returns IPRT status code from client send.
|
---|
1987 | * @retval VINF_EOF indicates ABORT command.
|
---|
1988 | *
|
---|
1989 | * @param hPollSet The polling set.
|
---|
1990 | * @param fPollEvt The event mask returned by RTPollNoResume.
|
---|
1991 | * @param idPollHnd The handle ID.
|
---|
1992 | * @param phStdInW The standard input pipe.
|
---|
1993 | * @param pStdInBuf The standard input buffer.
|
---|
1994 | */
|
---|
1995 | static int txsDoExecHlpHandleTransportEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, uint32_t idPollHnd,
|
---|
1996 | PRTPIPE phStdInW, PTXSEXECSTDINBUF pStdInBuf)
|
---|
1997 | {
|
---|
1998 | /* ASSUMES the transport layer will detect or clear any error condition. */
|
---|
1999 | NOREF(fPollEvt); NOREF(idPollHnd);
|
---|
2000 | Log(("txsDoExecHlpHandleTransportEvent\n"));
|
---|
2001 | /** @todo Use a callback for this case? */
|
---|
2002 |
|
---|
2003 | /*
|
---|
2004 | * Read client command packet and process it.
|
---|
2005 | */
|
---|
2006 | /** @todo Sometimes this hangs on windows because there isn't any data pending.
|
---|
2007 | * We probably get woken up at the wrong time or in the wrong way, i.e. RTPoll()
|
---|
2008 | * is busted for sockets.
|
---|
2009 | *
|
---|
2010 | * Temporary workaround: Poll for input before trying to read it. */
|
---|
2011 | if (!g_pTransport->pfnPollIn())
|
---|
2012 | {
|
---|
2013 | Log(("Bad transport event\n"));
|
---|
2014 | RTThreadYield();
|
---|
2015 | return VINF_SUCCESS;
|
---|
2016 | }
|
---|
2017 | PTXSPKTHDR pPktHdr;
|
---|
2018 | int rc = txsRecvPkt(&pPktHdr, false /*fAutoRetryOnFailure*/);
|
---|
2019 | if (RT_FAILURE(rc))
|
---|
2020 | return rc;
|
---|
2021 | Log(("Bad transport event\n"));
|
---|
2022 |
|
---|
2023 | /*
|
---|
2024 | * The most common thing here would be a STDIN request with data
|
---|
2025 | * for the child process.
|
---|
2026 | */
|
---|
2027 | if (txsIsSameOpcode(pPktHdr, "STDIN"))
|
---|
2028 | {
|
---|
2029 | if ( !pStdInBuf->fBitBucket
|
---|
2030 | && pPktHdr->cb >= sizeof(TXSPKTHDR) + sizeof(uint32_t))
|
---|
2031 | {
|
---|
2032 | uint32_t uCrc32 = *(uint32_t *)(pPktHdr + 1);
|
---|
2033 | const char *pch = (const char *)(pPktHdr + 1) + sizeof(uint32_t);
|
---|
2034 | size_t cb = pPktHdr->cb - sizeof(TXSPKTHDR) - sizeof(uint32_t);
|
---|
2035 |
|
---|
2036 | /* Check the CRC */
|
---|
2037 | pStdInBuf->uCrc32 = RTCrc32Process(pStdInBuf->uCrc32, pch, cb);
|
---|
2038 | if (RTCrc32Finish(pStdInBuf->uCrc32) == uCrc32)
|
---|
2039 | {
|
---|
2040 |
|
---|
2041 | /* Rewind the buffer if it's empty. */
|
---|
2042 | size_t cbInBuf = pStdInBuf->cb - pStdInBuf->off;
|
---|
2043 | bool const fAddToSet = cbInBuf == 0;
|
---|
2044 | if (fAddToSet)
|
---|
2045 | pStdInBuf->cb = pStdInBuf->off = 0;
|
---|
2046 |
|
---|
2047 | /* Try and see if we can simply append the data. */
|
---|
2048 | if (cb + pStdInBuf->cb <= pStdInBuf->cbAllocated)
|
---|
2049 | {
|
---|
2050 | memcpy(&pStdInBuf->pch[pStdInBuf->cb], pch, cb);
|
---|
2051 | pStdInBuf->cb += cb;
|
---|
2052 | rc = txsReplyAck(pPktHdr);
|
---|
2053 | }
|
---|
2054 | else
|
---|
2055 | {
|
---|
2056 | /* Try write a bit or two before we move+realloc the buffer. */
|
---|
2057 | if (cbInBuf > 0)
|
---|
2058 | txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW);
|
---|
2059 |
|
---|
2060 | /* Move any buffered data to the front. */
|
---|
2061 | cbInBuf = pStdInBuf->cb - pStdInBuf->off;
|
---|
2062 | if (cbInBuf == 0)
|
---|
2063 | pStdInBuf->cb = pStdInBuf->off = 0;
|
---|
2064 | else
|
---|
2065 | {
|
---|
2066 | memmove(pStdInBuf->pch, &pStdInBuf->pch[pStdInBuf->off], cbInBuf);
|
---|
2067 | pStdInBuf->cb = cbInBuf;
|
---|
2068 | pStdInBuf->off = 0;
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | /* Do we need to grow the buffer? */
|
---|
2072 | if (cb + pStdInBuf->cb > pStdInBuf->cbAllocated)
|
---|
2073 | {
|
---|
2074 | size_t cbAlloc = pStdInBuf->cb + cb;
|
---|
2075 | cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
|
---|
2076 | void *pvNew = RTMemRealloc(pStdInBuf->pch, cbAlloc);
|
---|
2077 | if (pvNew)
|
---|
2078 | {
|
---|
2079 | pStdInBuf->pch = (char *)pvNew;
|
---|
2080 | pStdInBuf->cbAllocated = cbAlloc;
|
---|
2081 | }
|
---|
2082 | }
|
---|
2083 |
|
---|
2084 | /* Finally, copy the data. */
|
---|
2085 | if (cb + pStdInBuf->cb <= pStdInBuf->cbAllocated)
|
---|
2086 | {
|
---|
2087 | memcpy(&pStdInBuf->pch[pStdInBuf->cb], pch, cb);
|
---|
2088 | pStdInBuf->cb += cb;
|
---|
2089 | rc = txsReplyAck(pPktHdr);
|
---|
2090 | }
|
---|
2091 | else
|
---|
2092 | rc = txsReplySimple(pPktHdr, "STDINMEM");
|
---|
2093 | }
|
---|
2094 |
|
---|
2095 | /*
|
---|
2096 | * Flush the buffered data and add/remove the standard input
|
---|
2097 | * handle from the set.
|
---|
2098 | */
|
---|
2099 | txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW);
|
---|
2100 | if (fAddToSet && pStdInBuf->off < pStdInBuf->cb)
|
---|
2101 | {
|
---|
2102 | int rc2 = RTPollSetAddPipe(hPollSet, *phStdInW, RTPOLL_EVT_WRITE, TXSEXECHNDID_STDIN_WRITABLE);
|
---|
2103 | AssertRC(rc2);
|
---|
2104 | }
|
---|
2105 | else if (!fAddToSet && pStdInBuf->off >= pStdInBuf->cb)
|
---|
2106 | {
|
---|
2107 | int rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE);
|
---|
2108 | AssertRC(rc2);
|
---|
2109 | }
|
---|
2110 | }
|
---|
2111 | else
|
---|
2112 | rc = txsReplyFailure(pPktHdr, "STDINCRC", "Invalid CRC checksum expected %#x got %#x",
|
---|
2113 | pStdInBuf->uCrc32, uCrc32);
|
---|
2114 | }
|
---|
2115 | else if (pPktHdr->cb < sizeof(TXSPKTHDR) + sizeof(uint32_t))
|
---|
2116 | rc = txsReplySimple(pPktHdr, "STDINBAD");
|
---|
2117 | else
|
---|
2118 | rc = txsReplySimple(pPktHdr, "STDINIGN");
|
---|
2119 | }
|
---|
2120 | /*
|
---|
2121 | * Marks the end of the stream for stdin.
|
---|
2122 | */
|
---|
2123 | else if (txsIsSameOpcode(pPktHdr, "STDINEOS"))
|
---|
2124 | {
|
---|
2125 | if (RT_LIKELY(pPktHdr->cb == sizeof(TXSPKTHDR)))
|
---|
2126 | {
|
---|
2127 | /* Close the pipe. */
|
---|
2128 | txsDoExecHlpHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf);
|
---|
2129 | rc = txsReplyAck(pPktHdr);
|
---|
2130 | }
|
---|
2131 | else
|
---|
2132 | rc = txsReplySimple(pPktHdr, "STDINBAD");
|
---|
2133 | }
|
---|
2134 | /*
|
---|
2135 | * The only other two requests are connection oriented and we return a error
|
---|
2136 | * code so that we unwind the whole EXEC shebang and start afresh.
|
---|
2137 | */
|
---|
2138 | else if (txsIsSameOpcode(pPktHdr, "BYE"))
|
---|
2139 | {
|
---|
2140 | rc = txsDoBye(pPktHdr);
|
---|
2141 | if (RT_SUCCESS(rc))
|
---|
2142 | rc = VERR_NET_NOT_CONNECTED;
|
---|
2143 | }
|
---|
2144 | else if (txsIsSameOpcode(pPktHdr, "HOWDY"))
|
---|
2145 | {
|
---|
2146 | rc = txsDoHowdy(pPktHdr);
|
---|
2147 | if (RT_SUCCESS(rc))
|
---|
2148 | rc = VERR_NET_NOT_CONNECTED;
|
---|
2149 | }
|
---|
2150 | else if (txsIsSameOpcode(pPktHdr, "ABORT"))
|
---|
2151 | {
|
---|
2152 | rc = txsReplyAck(pPktHdr);
|
---|
2153 | if (RT_SUCCESS(rc))
|
---|
2154 | rc = VINF_EOF; /* this is but ugly! */
|
---|
2155 | }
|
---|
2156 | else
|
---|
2157 | rc = txsReplyFailure(pPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known or not recognized during EXEC", pPktHdr->achOpcode);
|
---|
2158 |
|
---|
2159 | RTMemFree(pPktHdr);
|
---|
2160 | return rc;
|
---|
2161 | }
|
---|
2162 |
|
---|
2163 | /**
|
---|
2164 | * Handles the output and input of the process, waits for it finish up.
|
---|
2165 | *
|
---|
2166 | * @returns IPRT status code from reply send.
|
---|
2167 | * @param pTxsExec The TXSEXEC instance.
|
---|
2168 | */
|
---|
2169 | static int txsDoExecHlp2(PTXSEXEC pTxsExec)
|
---|
2170 | {
|
---|
2171 | int rc; /* client send. */
|
---|
2172 | int rc2;
|
---|
2173 | TXSEXECSTDINBUF StdInBuf = { 0, 0, NULL, 0, pTxsExec->hStdInW == NIL_RTPIPE, RTCrc32Start() };
|
---|
2174 | uint32_t uStdOutCrc32 = RTCrc32Start();
|
---|
2175 | uint32_t uStdErrCrc32 = uStdOutCrc32;
|
---|
2176 | uint32_t uTestPipeCrc32 = uStdOutCrc32;
|
---|
2177 | uint64_t const MsStart = RTTimeMilliTS();
|
---|
2178 | bool fProcessTimedOut = false;
|
---|
2179 | uint64_t MsProcessKilled = UINT64_MAX;
|
---|
2180 | RTMSINTERVAL const cMsPollBase = g_pTransport->pfnPollSetAdd || pTxsExec->hStdInW == NIL_RTPIPE
|
---|
2181 | ? 5000 : 100;
|
---|
2182 | RTMSINTERVAL cMsPollCur = 0;
|
---|
2183 |
|
---|
2184 | /*
|
---|
2185 | * Before entering the loop, tell the client that we've started the guest
|
---|
2186 | * and that it's now OK to send input to the process. (This is not the
|
---|
2187 | * final ACK, so the packet header is NULL ... kind of bogus.)
|
---|
2188 | */
|
---|
2189 | rc = txsReplyAck(NULL);
|
---|
2190 |
|
---|
2191 | /*
|
---|
2192 | * Process input, output, the test pipe and client requests.
|
---|
2193 | */
|
---|
2194 | while ( RT_SUCCESS(rc)
|
---|
2195 | && RT_UNLIKELY(!g_fTerminate))
|
---|
2196 | {
|
---|
2197 | /*
|
---|
2198 | * Wait/Process all pending events.
|
---|
2199 | */
|
---|
2200 | uint32_t idPollHnd;
|
---|
2201 | uint32_t fPollEvt;
|
---|
2202 | Log3(("Calling RTPollNoResume(,%u,)...\n", cMsPollCur));
|
---|
2203 | rc2 = RTPollNoResume(pTxsExec->hPollSet, cMsPollCur, &fPollEvt, &idPollHnd);
|
---|
2204 | Log3(("RTPollNoResume -> fPollEvt=%#x idPollHnd=%u\n", fPollEvt, idPollHnd));
|
---|
2205 | if (g_fTerminate)
|
---|
2206 | continue;
|
---|
2207 | cMsPollCur = 0; /* no rest until we've checked everything. */
|
---|
2208 |
|
---|
2209 | if (RT_SUCCESS(rc2))
|
---|
2210 | {
|
---|
2211 | switch (idPollHnd)
|
---|
2212 | {
|
---|
2213 | case TXSEXECHNDID_STDOUT:
|
---|
2214 | rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdOutR, &uStdOutCrc32,
|
---|
2215 | TXSEXECHNDID_STDOUT, "STDOUT ");
|
---|
2216 | break;
|
---|
2217 |
|
---|
2218 | case TXSEXECHNDID_STDERR:
|
---|
2219 | rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdErrR, &uStdErrCrc32,
|
---|
2220 | TXSEXECHNDID_STDERR, "STDERR ");
|
---|
2221 | break;
|
---|
2222 |
|
---|
2223 | case TXSEXECHNDID_TESTPIPE:
|
---|
2224 | rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hTestPipeR, &uTestPipeCrc32,
|
---|
2225 | TXSEXECHNDID_TESTPIPE, "TESTPIPE");
|
---|
2226 | break;
|
---|
2227 |
|
---|
2228 | case TXSEXECHNDID_STDIN:
|
---|
2229 | txsDoExecHlpHandleStdInErrorEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdInW, &StdInBuf);
|
---|
2230 | break;
|
---|
2231 |
|
---|
2232 | case TXSEXECHNDID_STDIN_WRITABLE:
|
---|
2233 | txsDoExecHlpHandleStdInWritableEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdInW, &StdInBuf);
|
---|
2234 | break;
|
---|
2235 |
|
---|
2236 | case TXSEXECHNDID_THREAD:
|
---|
2237 | rc2 = RTPollSetRemove(pTxsExec->hPollSet, TXSEXECHNDID_THREAD); AssertRC(rc2);
|
---|
2238 | break;
|
---|
2239 |
|
---|
2240 | default:
|
---|
2241 | rc = txsDoExecHlpHandleTransportEvent(pTxsExec->hPollSet, fPollEvt, idPollHnd, &pTxsExec->hStdInW,
|
---|
2242 | &StdInBuf);
|
---|
2243 | break;
|
---|
2244 | }
|
---|
2245 | if (RT_FAILURE(rc) || rc == VINF_EOF)
|
---|
2246 | break; /* abort command, or client dead or something */
|
---|
2247 | continue;
|
---|
2248 | }
|
---|
2249 |
|
---|
2250 | /*
|
---|
2251 | * Check for incoming data.
|
---|
2252 | */
|
---|
2253 | if (g_pTransport->pfnPollIn())
|
---|
2254 | {
|
---|
2255 | rc = txsDoExecHlpHandleTransportEvent(pTxsExec->hPollSet, 0, UINT32_MAX, &pTxsExec->hStdInW, &StdInBuf);
|
---|
2256 | if (RT_FAILURE(rc) || rc == VINF_EOF)
|
---|
2257 | break; /* abort command, or client dead or something */
|
---|
2258 | continue;
|
---|
2259 | }
|
---|
2260 |
|
---|
2261 | /*
|
---|
2262 | * If the process has terminated, we're should head out.
|
---|
2263 | */
|
---|
2264 | if (!ASMAtomicReadBool(&pTxsExec->fProcessAlive))
|
---|
2265 | break;
|
---|
2266 |
|
---|
2267 | /*
|
---|
2268 | * Check for timed out, killing the process.
|
---|
2269 | */
|
---|
2270 | uint32_t cMilliesLeft = RT_INDEFINITE_WAIT;
|
---|
2271 | if (pTxsExec->cMsTimeout != RT_INDEFINITE_WAIT)
|
---|
2272 | {
|
---|
2273 | uint64_t u64Now = RTTimeMilliTS();
|
---|
2274 | uint64_t cMsElapsed = u64Now - MsStart;
|
---|
2275 | if (cMsElapsed >= pTxsExec->cMsTimeout)
|
---|
2276 | {
|
---|
2277 | fProcessTimedOut = true;
|
---|
2278 | if ( MsProcessKilled == UINT64_MAX
|
---|
2279 | || u64Now - MsProcessKilled > 1000)
|
---|
2280 | {
|
---|
2281 | if (u64Now - MsProcessKilled > 20*60*1000)
|
---|
2282 | break; /* give up after 20 mins */
|
---|
2283 | RTCritSectEnter(&pTxsExec->CritSect);
|
---|
2284 | if (pTxsExec->fProcessAlive)
|
---|
2285 | RTProcTerminate(pTxsExec->hProcess);
|
---|
2286 | RTCritSectLeave(&pTxsExec->CritSect);
|
---|
2287 | MsProcessKilled = u64Now;
|
---|
2288 | continue;
|
---|
2289 | }
|
---|
2290 | cMilliesLeft = 10000;
|
---|
2291 | }
|
---|
2292 | else
|
---|
2293 | cMilliesLeft = pTxsExec->cMsTimeout - (uint32_t)cMsElapsed;
|
---|
2294 | }
|
---|
2295 |
|
---|
2296 | /* Reset the polling interval since we've done all pending work. */
|
---|
2297 | cMsPollCur = cMilliesLeft >= cMsPollBase ? cMsPollBase : cMilliesLeft;
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 | /*
|
---|
2301 | * At this point we should hopefully only have to wait 0 ms on the thread
|
---|
2302 | * to release the handle... But if for instance the process refuses to die,
|
---|
2303 | * we'll have to try kill it again. Bothersome.
|
---|
2304 | */
|
---|
2305 | for (size_t i = 0; i < 22; i++)
|
---|
2306 | {
|
---|
2307 | rc2 = RTThreadWait(pTxsExec->hThreadWaiter, 500, NULL);
|
---|
2308 | if (RT_SUCCESS(rc))
|
---|
2309 | {
|
---|
2310 | pTxsExec->hThreadWaiter = NIL_RTTHREAD;
|
---|
2311 | Assert(!pTxsExec->fProcessAlive);
|
---|
2312 | break;
|
---|
2313 | }
|
---|
2314 | if (i == 0 || i == 10 || i == 15 || i == 18 || i > 20)
|
---|
2315 | {
|
---|
2316 | RTCritSectEnter(&pTxsExec->CritSect);
|
---|
2317 | if (pTxsExec->fProcessAlive)
|
---|
2318 | RTProcTerminate(pTxsExec->hProcess);
|
---|
2319 | RTCritSectLeave(&pTxsExec->CritSect);
|
---|
2320 | }
|
---|
2321 | }
|
---|
2322 |
|
---|
2323 | /*
|
---|
2324 | * If we don't have a client problem (RT_FAILURE(rc) we'll reply to the
|
---|
2325 | * clients exec packet now.
|
---|
2326 | */
|
---|
2327 | if (RT_SUCCESS(rc))
|
---|
2328 | rc = txsExecSendExitStatus(pTxsExec, pTxsExec->fProcessAlive, fProcessTimedOut, MsProcessKilled);
|
---|
2329 |
|
---|
2330 | RTMemFree(StdInBuf.pch);
|
---|
2331 | return rc;
|
---|
2332 | }
|
---|
2333 |
|
---|
2334 | /**
|
---|
2335 | * Creates a poll set for the pipes and let the transport layer add stuff to it
|
---|
2336 | * as well.
|
---|
2337 | *
|
---|
2338 | * @returns IPRT status code, reply to client made on error.
|
---|
2339 | * @param pTxsExec The TXSEXEC instance.
|
---|
2340 | */
|
---|
2341 | static int txsExecSetupPollSet(PTXSEXEC pTxsExec)
|
---|
2342 | {
|
---|
2343 | int rc = RTPollSetCreate(&pTxsExec->hPollSet);
|
---|
2344 | if (RT_FAILURE(rc))
|
---|
2345 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetCreate");
|
---|
2346 |
|
---|
2347 | rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdInW, RTPOLL_EVT_ERROR, TXSEXECHNDID_STDIN);
|
---|
2348 | if (RT_FAILURE(rc))
|
---|
2349 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stdin");
|
---|
2350 |
|
---|
2351 | rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
2352 | TXSEXECHNDID_STDOUT);
|
---|
2353 | if (RT_FAILURE(rc))
|
---|
2354 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stdout");
|
---|
2355 |
|
---|
2356 | rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
2357 | TXSEXECHNDID_STDERR);
|
---|
2358 | if (RT_FAILURE(rc))
|
---|
2359 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stderr");
|
---|
2360 |
|
---|
2361 | rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hTestPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
2362 | TXSEXECHNDID_TESTPIPE);
|
---|
2363 | if (RT_FAILURE(rc))
|
---|
2364 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/test");
|
---|
2365 |
|
---|
2366 | rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hWakeUpPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
2367 | TXSEXECHNDID_THREAD);
|
---|
2368 | if (RT_FAILURE(rc))
|
---|
2369 | return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/wakeup");
|
---|
2370 |
|
---|
2371 | if (g_pTransport->pfnPollSetAdd)
|
---|
2372 | {
|
---|
2373 | rc = g_pTransport->pfnPollSetAdd(pTxsExec->hPollSet, TXSEXECHNDID_TRANSPORT);
|
---|
2374 | if (RT_FAILURE(rc))
|
---|
2375 | return txsExecReplyRC(pTxsExec, rc, "%s->pfnPollSetAdd/stdin", g_pTransport->szName);
|
---|
2376 | }
|
---|
2377 |
|
---|
2378 | return VINF_SUCCESS;
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | /**
|
---|
2382 | * Thread that calls RTProcWait and signals the main thread when it returns.
|
---|
2383 | *
|
---|
2384 | * The thread is created before the process is started and is waiting for a user
|
---|
2385 | * signal from the main thread before it calls RTProcWait.
|
---|
2386 | *
|
---|
2387 | * @returns VINF_SUCCESS (ignored).
|
---|
2388 | * @param hThreadSelf The thread handle.
|
---|
2389 | * @param pvUser The TXEEXEC structure.
|
---|
2390 | */
|
---|
2391 | static DECLCALLBACK(int) txsExecWaitThreadProc(RTTHREAD hThreadSelf, void *pvUser)
|
---|
2392 | {
|
---|
2393 | PTXSEXEC pTxsExec = (PTXSEXEC)pvUser;
|
---|
2394 |
|
---|
2395 | /* Wait for the go ahead... */
|
---|
2396 | int rc = RTThreadUserWait(hThreadSelf, RT_INDEFINITE_WAIT); AssertRC(rc);
|
---|
2397 |
|
---|
2398 | RTCritSectEnter(&pTxsExec->CritSect);
|
---|
2399 | for (;;)
|
---|
2400 | {
|
---|
2401 | RTCritSectLeave(&pTxsExec->CritSect);
|
---|
2402 | rc = RTProcWaitNoResume(pTxsExec->hProcess, RTPROCWAIT_FLAGS_BLOCK, &pTxsExec->ProcessStatus);
|
---|
2403 | RTCritSectEnter(&pTxsExec->CritSect);
|
---|
2404 |
|
---|
2405 | /* If the pipe is NIL, the destructor wants us to get lost ASAP. */
|
---|
2406 | if (pTxsExec->hWakeUpPipeW == NIL_RTPIPE)
|
---|
2407 | break;
|
---|
2408 |
|
---|
2409 | if (RT_FAILURE(rc))
|
---|
2410 | {
|
---|
2411 | rc = RTProcWait(pTxsExec->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &pTxsExec->ProcessStatus);
|
---|
2412 | if (rc == VERR_PROCESS_RUNNING)
|
---|
2413 | continue;
|
---|
2414 |
|
---|
2415 | if (RT_FAILURE(rc))
|
---|
2416 | {
|
---|
2417 | AssertRC(rc);
|
---|
2418 | pTxsExec->ProcessStatus.iStatus = rc;
|
---|
2419 | pTxsExec->ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
|
---|
2420 | }
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | /* The process finished, signal the main thread over the pipe. */
|
---|
2424 | ASMAtomicWriteBool(&pTxsExec->fProcessAlive, false);
|
---|
2425 | size_t cbIgnored;
|
---|
2426 | RTPipeWrite(pTxsExec->hWakeUpPipeW, "done", 4, &cbIgnored);
|
---|
2427 | RTPipeClose(pTxsExec->hWakeUpPipeW);
|
---|
2428 | pTxsExec->hWakeUpPipeW = NIL_RTPIPE;
|
---|
2429 | break;
|
---|
2430 | }
|
---|
2431 | RTCritSectLeave(&pTxsExec->CritSect);
|
---|
2432 |
|
---|
2433 | return VINF_SUCCESS;
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | /**
|
---|
2437 | * Sets up the thread that waits for the process to complete.
|
---|
2438 | *
|
---|
2439 | * @returns IPRT status code, reply to client made on error.
|
---|
2440 | * @param pTxsExec The TXSEXEC instance.
|
---|
2441 | */
|
---|
2442 | static int txsExecSetupThread(PTXSEXEC pTxsExec)
|
---|
2443 | {
|
---|
2444 | int rc = RTPipeCreate(&pTxsExec->hWakeUpPipeR, &pTxsExec->hWakeUpPipeW, 0 /*fFlags*/);
|
---|
2445 | if (RT_FAILURE(rc))
|
---|
2446 | {
|
---|
2447 | pTxsExec->hWakeUpPipeR = pTxsExec->hWakeUpPipeW = NIL_RTPIPE;
|
---|
2448 | return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/wait");
|
---|
2449 | }
|
---|
2450 |
|
---|
2451 | rc = RTThreadCreate(&pTxsExec->hThreadWaiter, txsExecWaitThreadProc,
|
---|
2452 | pTxsExec, 0 /*cbStack */, RTTHREADTYPE_DEFAULT,
|
---|
2453 | RTTHREADFLAGS_WAITABLE, "TxsProcW");
|
---|
2454 | if (RT_FAILURE(rc))
|
---|
2455 | {
|
---|
2456 | pTxsExec->hThreadWaiter = NIL_RTTHREAD;
|
---|
2457 | return txsExecReplyRC(pTxsExec, rc, "RTThreadCreate");
|
---|
2458 | }
|
---|
2459 |
|
---|
2460 | return VINF_SUCCESS;
|
---|
2461 | }
|
---|
2462 |
|
---|
2463 | /**
|
---|
2464 | * Sets up the test pipe.
|
---|
2465 | *
|
---|
2466 | * @returns IPRT status code, reply to client made on error.
|
---|
2467 | * @param pTxsExec The TXSEXEC instance.
|
---|
2468 | * @param pszTestPipe How to set up the test pipe.
|
---|
2469 | */
|
---|
2470 | static int txsExecSetupTestPipe(PTXSEXEC pTxsExec, const char *pszTestPipe)
|
---|
2471 | {
|
---|
2472 | if (strcmp(pszTestPipe, "|"))
|
---|
2473 | return VINF_SUCCESS;
|
---|
2474 |
|
---|
2475 | int rc = RTPipeCreate(&pTxsExec->hTestPipeR, &pTxsExec->hTestPipeW, RTPIPE_C_INHERIT_WRITE);
|
---|
2476 | if (RT_FAILURE(rc))
|
---|
2477 | {
|
---|
2478 | pTxsExec->hTestPipeR = pTxsExec->hTestPipeW = NIL_RTPIPE;
|
---|
2479 | return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/test/%s", pszTestPipe);
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | char szVal[64];
|
---|
2483 | RTStrPrintf(szVal, sizeof(szVal), "%#llx", (uint64_t)RTPipeToNative(pTxsExec->hTestPipeW));
|
---|
2484 | rc = RTEnvSetEx(pTxsExec->hEnv, "IPRT_TEST_PIPE", szVal);
|
---|
2485 | if (RT_FAILURE(rc))
|
---|
2486 | return txsExecReplyRC(pTxsExec, rc, "RTEnvSetEx/test/%s", pszTestPipe);
|
---|
2487 |
|
---|
2488 | return VINF_SUCCESS;
|
---|
2489 | }
|
---|
2490 |
|
---|
2491 | /**
|
---|
2492 | * Sets up the redirection / pipe / nothing for one of the standard handles.
|
---|
2493 | *
|
---|
2494 | * @returns IPRT status code, reply to client made on error.
|
---|
2495 | * @param pTxsExec The TXSEXEC instance.
|
---|
2496 | * @param pszHowTo How to set up this standard handle.
|
---|
2497 | * @param pszStdWhat For what to setup redirection (stdin/stdout/stderr).
|
---|
2498 | * @param fd Which standard handle it is (0 == stdin, 1 ==
|
---|
2499 | * stdout, 2 == stderr).
|
---|
2500 | * @param ph The generic handle that @a pph may be set
|
---|
2501 | * pointing to. Always set.
|
---|
2502 | * @param pph Pointer to the RTProcCreateExec argument.
|
---|
2503 | * Always set.
|
---|
2504 | * @param phPipe Where to return the end of the pipe that we
|
---|
2505 | * should service. Always set.
|
---|
2506 | */
|
---|
2507 | static int txsExecSetupRedir(PTXSEXEC pTxsExec, const char *pszHowTo, const char *pszStdWhat, int fd, PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe)
|
---|
2508 | {
|
---|
2509 | ph->enmType = RTHANDLETYPE_PIPE;
|
---|
2510 | ph->u.hPipe = NIL_RTPIPE;
|
---|
2511 | *pph = NULL;
|
---|
2512 | *phPipe = NIL_RTPIPE;
|
---|
2513 |
|
---|
2514 | int rc;
|
---|
2515 | if (!strcmp(pszHowTo, "|"))
|
---|
2516 | {
|
---|
2517 | /*
|
---|
2518 | * Setup a pipe for forwarding to/from the client.
|
---|
2519 | */
|
---|
2520 | if (fd == 0)
|
---|
2521 | rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ);
|
---|
2522 | else
|
---|
2523 | rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE);
|
---|
2524 | if (RT_FAILURE(rc))
|
---|
2525 | return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/%s/%s", pszStdWhat, pszHowTo);
|
---|
2526 | ph->enmType = RTHANDLETYPE_PIPE;
|
---|
2527 | *pph = ph;
|
---|
2528 | }
|
---|
2529 | else if (!strcmp(pszHowTo, "/dev/null"))
|
---|
2530 | {
|
---|
2531 | /*
|
---|
2532 | * Redirect to/from /dev/null.
|
---|
2533 | */
|
---|
2534 | RTFILE hFile;
|
---|
2535 | rc = RTFileOpenBitBucket(&hFile, fd == 0 ? RTFILE_O_READ : RTFILE_O_WRITE);
|
---|
2536 | if (RT_FAILURE(rc))
|
---|
2537 | return txsExecReplyRC(pTxsExec, rc, "RTFileOpenBitBucket/%s/%s", pszStdWhat, pszHowTo);
|
---|
2538 |
|
---|
2539 | ph->enmType = RTHANDLETYPE_FILE;
|
---|
2540 | ph->u.hFile = hFile;
|
---|
2541 | *pph = ph;
|
---|
2542 | }
|
---|
2543 | else if (*pszHowTo)
|
---|
2544 | {
|
---|
2545 | /*
|
---|
2546 | * Redirect to/from file.
|
---|
2547 | */
|
---|
2548 | uint32_t fFlags;
|
---|
2549 | if (fd == 0)
|
---|
2550 | fFlags = RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN;
|
---|
2551 | else
|
---|
2552 | {
|
---|
2553 | if (pszHowTo[0] != '>' || pszHowTo[1] != '>')
|
---|
2554 | fFlags = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE;
|
---|
2555 | else
|
---|
2556 | {
|
---|
2557 | /* append */
|
---|
2558 | pszHowTo += 2;
|
---|
2559 | fFlags = RTFILE_O_WRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND;
|
---|
2560 | }
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | RTFILE hFile;
|
---|
2564 | rc = RTFileOpen(&hFile, pszHowTo, fFlags);
|
---|
2565 | if (RT_FAILURE(rc))
|
---|
2566 | return txsExecReplyRC(pTxsExec, rc, "RTFileOpen/%s/%s", pszStdWhat, pszHowTo);
|
---|
2567 |
|
---|
2568 | ph->enmType = RTHANDLETYPE_FILE;
|
---|
2569 | ph->u.hFile = hFile;
|
---|
2570 | *pph = ph;
|
---|
2571 | }
|
---|
2572 | else
|
---|
2573 | /* same as parent (us) */
|
---|
2574 | rc = VINF_SUCCESS;
|
---|
2575 | return rc;
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | /**
|
---|
2579 | * Create the environment.
|
---|
2580 | *
|
---|
2581 | * @returns IPRT status code, reply to client made on error.
|
---|
2582 | * @param pTxsExec The TXSEXEC instance.
|
---|
2583 | * @param cEnvVars The number of environment variables.
|
---|
2584 | * @param papszEnv The environment variables (var=value).
|
---|
2585 | */
|
---|
2586 | static int txsExecSetupEnv(PTXSEXEC pTxsExec, uint32_t cEnvVars, const char * const *papszEnv)
|
---|
2587 | {
|
---|
2588 | /*
|
---|
2589 | * Create the environment.
|
---|
2590 | */
|
---|
2591 | int rc = RTEnvClone(&pTxsExec->hEnv, RTENV_DEFAULT);
|
---|
2592 | if (RT_FAILURE(rc))
|
---|
2593 | return txsExecReplyRC(pTxsExec, rc, "RTEnvClone");
|
---|
2594 |
|
---|
2595 | for (size_t i = 0; i < cEnvVars; i++)
|
---|
2596 | {
|
---|
2597 | rc = RTEnvPutEx(pTxsExec->hEnv, papszEnv[i]);
|
---|
2598 | if (RT_FAILURE(rc))
|
---|
2599 | return txsExecReplyRC(pTxsExec, rc, "RTEnvPutEx(,'%s')", papszEnv[i]);
|
---|
2600 | }
|
---|
2601 | return VINF_SUCCESS;
|
---|
2602 | }
|
---|
2603 |
|
---|
2604 | /**
|
---|
2605 | * Deletes the TXSEXEC structure and frees the memory backing it.
|
---|
2606 | *
|
---|
2607 | * @param pTxsExec The structure to destroy.
|
---|
2608 | */
|
---|
2609 | static void txsExecDestroy(PTXSEXEC pTxsExec)
|
---|
2610 | {
|
---|
2611 | int rc2;
|
---|
2612 |
|
---|
2613 | rc2 = RTEnvDestroy(pTxsExec->hEnv); AssertRC(rc2);
|
---|
2614 | pTxsExec->hEnv = NIL_RTENV;
|
---|
2615 | rc2 = RTPipeClose(pTxsExec->hTestPipeW); AssertRC(rc2);
|
---|
2616 | pTxsExec->hTestPipeW = NIL_RTPIPE;
|
---|
2617 | rc2 = RTHandleClose(pTxsExec->StdErr.phChild); AssertRC(rc2);
|
---|
2618 | pTxsExec->StdErr.phChild = NULL;
|
---|
2619 | rc2 = RTHandleClose(pTxsExec->StdOut.phChild); AssertRC(rc2);
|
---|
2620 | pTxsExec->StdOut.phChild = NULL;
|
---|
2621 | rc2 = RTHandleClose(pTxsExec->StdIn.phChild); AssertRC(rc2);
|
---|
2622 | pTxsExec->StdIn.phChild = NULL;
|
---|
2623 |
|
---|
2624 | rc2 = RTPipeClose(pTxsExec->hTestPipeR); AssertRC(rc2);
|
---|
2625 | pTxsExec->hTestPipeR = NIL_RTPIPE;
|
---|
2626 | rc2 = RTPipeClose(pTxsExec->hStdErrR); AssertRC(rc2);
|
---|
2627 | pTxsExec->hStdErrR = NIL_RTPIPE;
|
---|
2628 | rc2 = RTPipeClose(pTxsExec->hStdOutR); AssertRC(rc2);
|
---|
2629 | pTxsExec->hStdOutR = NIL_RTPIPE;
|
---|
2630 | rc2 = RTPipeClose(pTxsExec->hStdInW); AssertRC(rc2);
|
---|
2631 | pTxsExec->hStdInW = NIL_RTPIPE;
|
---|
2632 |
|
---|
2633 | RTPollSetDestroy(pTxsExec->hPollSet);
|
---|
2634 | pTxsExec->hPollSet = NIL_RTPOLLSET;
|
---|
2635 |
|
---|
2636 | /*
|
---|
2637 | * If the process is still running we're in a bit of a fix... Try kill it,
|
---|
2638 | * although that's potentially racing process termination and reusage of
|
---|
2639 | * the pid.
|
---|
2640 | */
|
---|
2641 | RTCritSectEnter(&pTxsExec->CritSect);
|
---|
2642 |
|
---|
2643 | RTPipeClose(pTxsExec->hWakeUpPipeW);
|
---|
2644 | pTxsExec->hWakeUpPipeW = NIL_RTPIPE;
|
---|
2645 | RTPipeClose(pTxsExec->hWakeUpPipeR);
|
---|
2646 | pTxsExec->hWakeUpPipeR = NIL_RTPIPE;
|
---|
2647 |
|
---|
2648 | if ( pTxsExec->hProcess != NIL_RTPROCESS
|
---|
2649 | && pTxsExec->fProcessAlive)
|
---|
2650 | RTProcTerminate(pTxsExec->hProcess);
|
---|
2651 |
|
---|
2652 | RTCritSectLeave(&pTxsExec->CritSect);
|
---|
2653 |
|
---|
2654 | int rcThread = VINF_SUCCESS;
|
---|
2655 | if (pTxsExec->hThreadWaiter != NIL_RTTHREAD)
|
---|
2656 | rcThread = RTThreadWait(pTxsExec->hThreadWaiter, 5000, NULL);
|
---|
2657 | if (RT_SUCCESS(rcThread))
|
---|
2658 | {
|
---|
2659 | pTxsExec->hThreadWaiter = NIL_RTTHREAD;
|
---|
2660 | RTCritSectDelete(&pTxsExec->CritSect);
|
---|
2661 | RTMemFree(pTxsExec);
|
---|
2662 | }
|
---|
2663 | /* else: leak it or RTThreadWait may cause heap corruption later. */
|
---|
2664 | }
|
---|
2665 |
|
---|
2666 | /**
|
---|
2667 | * Initializes the TXSEXEC structure.
|
---|
2668 | *
|
---|
2669 | * @returns VINF_SUCCESS and non-NULL *ppTxsExec on success, reply send status
|
---|
2670 | * and *ppTxsExec set to NULL on failure.
|
---|
2671 | * @param pPktHdr The exec packet.
|
---|
2672 | * @param cMsTimeout The time parameter.
|
---|
2673 | * @param ppTxsExec Where to return the structure.
|
---|
2674 | */
|
---|
2675 | static int txsExecCreate(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsTimeout, PTXSEXEC *ppTxsExec)
|
---|
2676 | {
|
---|
2677 | *ppTxsExec = NULL;
|
---|
2678 |
|
---|
2679 | /*
|
---|
2680 | * Allocate the basic resources.
|
---|
2681 | */
|
---|
2682 | PTXSEXEC pTxsExec = (PTXSEXEC)RTMemAlloc(sizeof(*pTxsExec));
|
---|
2683 | if (!pTxsExec)
|
---|
2684 | return txsReplyRC(pPktHdr, VERR_NO_MEMORY, "RTMemAlloc(%zu)", sizeof(*pTxsExec));
|
---|
2685 | int rc = RTCritSectInit(&pTxsExec->CritSect);
|
---|
2686 | if (RT_FAILURE(rc))
|
---|
2687 | {
|
---|
2688 | RTMemFree(pTxsExec);
|
---|
2689 | return txsReplyRC(pPktHdr, rc, "RTCritSectInit");
|
---|
2690 | }
|
---|
2691 |
|
---|
2692 | /*
|
---|
2693 | * Initialize the member to NIL values.
|
---|
2694 | */
|
---|
2695 | pTxsExec->pPktHdr = pPktHdr;
|
---|
2696 | pTxsExec->cMsTimeout = cMsTimeout;
|
---|
2697 | pTxsExec->rcReplySend = VINF_SUCCESS;
|
---|
2698 |
|
---|
2699 | pTxsExec->hPollSet = NIL_RTPOLLSET;
|
---|
2700 | pTxsExec->hStdInW = NIL_RTPIPE;
|
---|
2701 | pTxsExec->hStdOutR = NIL_RTPIPE;
|
---|
2702 | pTxsExec->hStdErrR = NIL_RTPIPE;
|
---|
2703 | pTxsExec->hTestPipeR = NIL_RTPIPE;
|
---|
2704 | pTxsExec->hWakeUpPipeR = NIL_RTPIPE;
|
---|
2705 | pTxsExec->hThreadWaiter = NIL_RTTHREAD;
|
---|
2706 |
|
---|
2707 | pTxsExec->StdIn.phChild = NULL;
|
---|
2708 | pTxsExec->StdOut.phChild = NULL;
|
---|
2709 | pTxsExec->StdErr.phChild = NULL;
|
---|
2710 | pTxsExec->hTestPipeW = NIL_RTPIPE;
|
---|
2711 | pTxsExec->hEnv = NIL_RTENV;
|
---|
2712 |
|
---|
2713 | pTxsExec->hProcess = NIL_RTPROCESS;
|
---|
2714 | pTxsExec->ProcessStatus.iStatus = 254;
|
---|
2715 | pTxsExec->ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
|
---|
2716 | pTxsExec->fProcessAlive = false;
|
---|
2717 | pTxsExec->hWakeUpPipeW = NIL_RTPIPE;
|
---|
2718 |
|
---|
2719 | *ppTxsExec = pTxsExec;
|
---|
2720 | return VINF_SUCCESS;
|
---|
2721 | }
|
---|
2722 |
|
---|
2723 | /**
|
---|
2724 | * txsDoExec helper that takes over when txsDoExec has expanded the packet.
|
---|
2725 | *
|
---|
2726 | * @returns IPRT status code from send.
|
---|
2727 | * @param pPktHdr The exec packet.
|
---|
2728 | * @param fFlags Flags, reserved for future use.
|
---|
2729 | * @param pszExecName The executable name.
|
---|
2730 | * @param cArgs The argument count.
|
---|
2731 | * @param papszArgs The arguments.
|
---|
2732 | * @param cEnvVars The environment variable count.
|
---|
2733 | * @param papszEnv The environment variables.
|
---|
2734 | * @param pszStdIn How to deal with standard in.
|
---|
2735 | * @param pszStdOut How to deal with standard out.
|
---|
2736 | * @param pszStdErr How to deal with standard err.
|
---|
2737 | * @param pszTestPipe How to deal with the test pipe.
|
---|
2738 | * @param pszUsername The user to run the program as.
|
---|
2739 | * @param cMillies The process time limit in milliseconds.
|
---|
2740 | */
|
---|
2741 | static int txsDoExecHlp(PCTXSPKTHDR pPktHdr, uint32_t fFlags, const char *pszExecName,
|
---|
2742 | uint32_t cArgs, const char * const *papszArgs,
|
---|
2743 | uint32_t cEnvVars, const char * const *papszEnv,
|
---|
2744 | const char *pszStdIn, const char *pszStdOut, const char *pszStdErr, const char *pszTestPipe,
|
---|
2745 | const char *pszUsername, RTMSINTERVAL cMillies)
|
---|
2746 | {
|
---|
2747 | int rc2;
|
---|
2748 | RT_NOREF_PV(fFlags);
|
---|
2749 |
|
---|
2750 | /*
|
---|
2751 | * Input validation, filter out things we don't yet support..
|
---|
2752 | */
|
---|
2753 | Assert(!fFlags);
|
---|
2754 | if (!*pszExecName)
|
---|
2755 | return txsReplyFailure(pPktHdr, "STR ZERO", "Executable name is empty");
|
---|
2756 | if (!*pszStdIn)
|
---|
2757 | return txsReplyFailure(pPktHdr, "STR ZERO", "The stdin howto is empty");
|
---|
2758 | if (!*pszStdOut)
|
---|
2759 | return txsReplyFailure(pPktHdr, "STR ZERO", "The stdout howto is empty");
|
---|
2760 | if (!*pszStdErr)
|
---|
2761 | return txsReplyFailure(pPktHdr, "STR ZERO", "The stderr howto is empty");
|
---|
2762 | if (!*pszTestPipe)
|
---|
2763 | return txsReplyFailure(pPktHdr, "STR ZERO", "The testpipe howto is empty");
|
---|
2764 | if (strcmp(pszTestPipe, "|") && strcmp(pszTestPipe, "/dev/null"))
|
---|
2765 | return txsReplyFailure(pPktHdr, "BAD TSTP", "Only \"|\" and \"/dev/null\" are allowed as testpipe howtos ('%s')",
|
---|
2766 | pszTestPipe);
|
---|
2767 | if (*pszUsername)
|
---|
2768 | return txsReplyFailure(pPktHdr, "NOT IMPL", "Executing as a specific user is not implemented ('%s')", pszUsername);
|
---|
2769 |
|
---|
2770 | /*
|
---|
2771 | * Prepare for process launch.
|
---|
2772 | */
|
---|
2773 | PTXSEXEC pTxsExec;
|
---|
2774 | int rc = txsExecCreate(pPktHdr, cMillies, &pTxsExec);
|
---|
2775 | if (pTxsExec == NULL)
|
---|
2776 | return rc;
|
---|
2777 | rc = txsExecSetupEnv(pTxsExec, cEnvVars, papszEnv);
|
---|
2778 | if (RT_SUCCESS(rc))
|
---|
2779 | rc = txsExecSetupRedir(pTxsExec, pszStdIn, "StdIn", 0, &pTxsExec->StdIn.hChild, &pTxsExec->StdIn.phChild, &pTxsExec->hStdInW);
|
---|
2780 | if (RT_SUCCESS(rc))
|
---|
2781 | rc = txsExecSetupRedir(pTxsExec, pszStdOut, "StdOut", 1, &pTxsExec->StdOut.hChild, &pTxsExec->StdOut.phChild, &pTxsExec->hStdOutR);
|
---|
2782 | if (RT_SUCCESS(rc))
|
---|
2783 | rc = txsExecSetupRedir(pTxsExec, pszStdErr, "StdErr", 2, &pTxsExec->StdErr.hChild, &pTxsExec->StdErr.phChild, &pTxsExec->hStdErrR);
|
---|
2784 | if (RT_SUCCESS(rc))
|
---|
2785 | rc = txsExecSetupTestPipe(pTxsExec, pszTestPipe);
|
---|
2786 | if (RT_SUCCESS(rc))
|
---|
2787 | rc = txsExecSetupThread(pTxsExec);
|
---|
2788 | if (RT_SUCCESS(rc))
|
---|
2789 | rc = txsExecSetupPollSet(pTxsExec);
|
---|
2790 | if (RT_SUCCESS(rc))
|
---|
2791 | {
|
---|
2792 | char szPathResolved[RTPATH_MAX + 1];
|
---|
2793 | rc = RTPathReal(pszExecName, szPathResolved, sizeof(szPathResolved));
|
---|
2794 | if (RT_SUCCESS(rc))
|
---|
2795 | {
|
---|
2796 | /*
|
---|
2797 | * Create the process.
|
---|
2798 | */
|
---|
2799 | if (g_fDisplayOutput)
|
---|
2800 | {
|
---|
2801 | RTPrintf("txs: Executing \"%s\" -> \"%s\": ", pszExecName, szPathResolved);
|
---|
2802 | for (uint32_t i = 0; i < cArgs; i++)
|
---|
2803 | RTPrintf(" \"%s\"", papszArgs[i]);
|
---|
2804 | RTPrintf("\n");
|
---|
2805 | }
|
---|
2806 |
|
---|
2807 | rc = RTProcCreateEx(szPathResolved, papszArgs, pTxsExec->hEnv, 0 /*fFlags*/,
|
---|
2808 | pTxsExec->StdIn.phChild, pTxsExec->StdOut.phChild, pTxsExec->StdErr.phChild,
|
---|
2809 | *pszUsername ? pszUsername : NULL, NULL, NULL,
|
---|
2810 | &pTxsExec->hProcess);
|
---|
2811 | if (RT_SUCCESS(rc))
|
---|
2812 | {
|
---|
2813 | ASMAtomicWriteBool(&pTxsExec->fProcessAlive, true);
|
---|
2814 | rc2 = RTThreadUserSignal(pTxsExec->hThreadWaiter); AssertRC(rc2);
|
---|
2815 |
|
---|
2816 | /*
|
---|
2817 | * Close the child ends of any pipes and redirected files.
|
---|
2818 | */
|
---|
2819 | rc2 = RTHandleClose(pTxsExec->StdIn.phChild); AssertRC(rc2);
|
---|
2820 | pTxsExec->StdIn.phChild = NULL;
|
---|
2821 | rc2 = RTHandleClose(pTxsExec->StdOut.phChild); AssertRC(rc2);
|
---|
2822 | pTxsExec->StdOut.phChild = NULL;
|
---|
2823 | rc2 = RTHandleClose(pTxsExec->StdErr.phChild); AssertRC(rc2);
|
---|
2824 | pTxsExec->StdErr.phChild = NULL;
|
---|
2825 | rc2 = RTPipeClose(pTxsExec->hTestPipeW); AssertRC(rc2);
|
---|
2826 | pTxsExec->hTestPipeW = NIL_RTPIPE;
|
---|
2827 |
|
---|
2828 | /*
|
---|
2829 | * Let another worker function funnel output and input to the
|
---|
2830 | * client as well as the process exit code.
|
---|
2831 | */
|
---|
2832 | rc = txsDoExecHlp2(pTxsExec);
|
---|
2833 | }
|
---|
2834 | }
|
---|
2835 |
|
---|
2836 | if (RT_FAILURE(rc))
|
---|
2837 | rc = txsReplyFailure(pPktHdr, "FAILED ", "Executing process \"%s\" failed with %Rrc",
|
---|
2838 | pszExecName, rc);
|
---|
2839 | }
|
---|
2840 | else
|
---|
2841 | rc = pTxsExec->rcReplySend;
|
---|
2842 | txsExecDestroy(pTxsExec);
|
---|
2843 | return rc;
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 | /**
|
---|
2847 | * Execute a program.
|
---|
2848 | *
|
---|
2849 | * @returns IPRT status code from send.
|
---|
2850 | * @param pPktHdr The exec packet.
|
---|
2851 | */
|
---|
2852 | static int txsDoExec(PCTXSPKTHDR pPktHdr)
|
---|
2853 | {
|
---|
2854 | /*
|
---|
2855 | * This packet has a lot of parameters, most of which are zero terminated
|
---|
2856 | * strings. The strings used in items 7 thru 10 are either file names,
|
---|
2857 | * "/dev/null" or a pipe char (|).
|
---|
2858 | *
|
---|
2859 | * Packet content:
|
---|
2860 | * 1. Flags reserved for future use (32-bit unsigned).
|
---|
2861 | * 2. The executable name (string).
|
---|
2862 | * 3. The argument count given as a 32-bit unsigned integer.
|
---|
2863 | * 4. The arguments strings.
|
---|
2864 | * 5. The number of environment strings (32-bit unsigned).
|
---|
2865 | * 6. The environment strings (var=val) to apply the environment.
|
---|
2866 | * 7. What to do about standard in (string).
|
---|
2867 | * 8. What to do about standard out (string).
|
---|
2868 | * 9. What to do about standard err (string).
|
---|
2869 | * 10. What to do about the test pipe (string).
|
---|
2870 | * 11. The name of the user to run the program as (string). Empty string
|
---|
2871 | * means running it as the current user.
|
---|
2872 | * 12. Process time limit in milliseconds (32-bit unsigned). Max == no limit.
|
---|
2873 | */
|
---|
2874 | size_t const cbMin = sizeof(TXSPKTHDR)
|
---|
2875 | + sizeof(uint32_t) /* flags */ + 2
|
---|
2876 | + sizeof(uint32_t) /* argc */ + 2 /* argv */
|
---|
2877 | + sizeof(uint32_t) + 0 /* environ */
|
---|
2878 | + 4 * 1
|
---|
2879 | + sizeof(uint32_t) /* timeout */;
|
---|
2880 | if (pPktHdr->cb < cbMin)
|
---|
2881 | return txsReplyBadMinSize(pPktHdr, cbMin);
|
---|
2882 |
|
---|
2883 | /* unpack the packet */
|
---|
2884 | char const *pchEnd = (char const *)pPktHdr + pPktHdr->cb;
|
---|
2885 | char const *pch = (char const *)(pPktHdr + 1); /* cursor */
|
---|
2886 |
|
---|
2887 | /* 1. flags */
|
---|
2888 | uint32_t const fFlags = *(uint32_t const *)pch;
|
---|
2889 | pch += sizeof(uint32_t);
|
---|
2890 | if (fFlags != 0)
|
---|
2891 | return txsReplyFailure(pPktHdr, "BAD FLAG", "Invalid EXEC flags %#x, expected 0", fFlags);
|
---|
2892 |
|
---|
2893 | /* 2. exec name */
|
---|
2894 | int rc;
|
---|
2895 | char *pszExecName = NULL;
|
---|
2896 | if (!txsIsStringValid(pPktHdr, "execname", pch, &pszExecName, &pch, &rc))
|
---|
2897 | return rc;
|
---|
2898 |
|
---|
2899 | /* 3. argc */
|
---|
2900 | uint32_t const cArgs = (size_t)(pchEnd - pch) > sizeof(uint32_t) ? *(uint32_t const *)pch : 0xff;
|
---|
2901 | pch += sizeof(uint32_t);
|
---|
2902 | if (cArgs * 1 >= (size_t)(pchEnd - pch))
|
---|
2903 | rc = txsReplyFailure(pPktHdr, "BAD ARGC", "Bad or missing argument count (%#x)", cArgs);
|
---|
2904 | else if (cArgs > 128)
|
---|
2905 | rc = txsReplyFailure(pPktHdr, "BAD ARGC", "Too many arguments (%#x)", cArgs);
|
---|
2906 | else
|
---|
2907 | {
|
---|
2908 | char **papszArgs = (char **)RTMemTmpAllocZ(sizeof(char *) * (cArgs + 1));
|
---|
2909 | if (papszArgs)
|
---|
2910 | {
|
---|
2911 | /* 4. argv */
|
---|
2912 | bool fOk = true;
|
---|
2913 | for (size_t i = 0; i < cArgs && fOk; i++)
|
---|
2914 | {
|
---|
2915 | fOk = txsIsStringValid(pPktHdr, "argvN", pch, &papszArgs[i], &pch, &rc);
|
---|
2916 | if (!fOk)
|
---|
2917 | break;
|
---|
2918 | }
|
---|
2919 | if (fOk)
|
---|
2920 | {
|
---|
2921 | /* 5. cEnvVars */
|
---|
2922 | uint32_t const cEnvVars = (size_t)(pchEnd - pch) > sizeof(uint32_t) ? *(uint32_t const *)pch : 0xfff;
|
---|
2923 | pch += sizeof(uint32_t);
|
---|
2924 | if (cEnvVars * 1 >= (size_t)(pchEnd - pch))
|
---|
2925 | rc = txsReplyFailure(pPktHdr, "BAD ENVC", "Bad or missing environment variable count (%#x)", cEnvVars);
|
---|
2926 | else if (cEnvVars > 256)
|
---|
2927 | rc = txsReplyFailure(pPktHdr, "BAD ENVC", "Too many environment variables (%#x)", cEnvVars);
|
---|
2928 | else
|
---|
2929 | {
|
---|
2930 | char **papszEnv = (char **)RTMemTmpAllocZ(sizeof(char *) * (cEnvVars + 1));
|
---|
2931 | if (papszEnv)
|
---|
2932 | {
|
---|
2933 | /* 6. environ */
|
---|
2934 | for (size_t i = 0; i < cEnvVars && fOk; i++)
|
---|
2935 | {
|
---|
2936 | fOk = txsIsStringValid(pPktHdr, "envN", pch, &papszEnv[i], &pch, &rc);
|
---|
2937 | if (!fOk) /* Bail out on error. */
|
---|
2938 | break;
|
---|
2939 | }
|
---|
2940 | if (fOk)
|
---|
2941 | {
|
---|
2942 | /* 7. stdout */
|
---|
2943 | char *pszStdIn;
|
---|
2944 | if (txsIsStringValid(pPktHdr, "stdin", pch, &pszStdIn, &pch, &rc))
|
---|
2945 | {
|
---|
2946 | /* 8. stdout */
|
---|
2947 | char *pszStdOut;
|
---|
2948 | if (txsIsStringValid(pPktHdr, "stdout", pch, &pszStdOut, &pch, &rc))
|
---|
2949 | {
|
---|
2950 | /* 9. stderr */
|
---|
2951 | char *pszStdErr;
|
---|
2952 | if (txsIsStringValid(pPktHdr, "stderr", pch, &pszStdErr, &pch, &rc))
|
---|
2953 | {
|
---|
2954 | /* 10. testpipe */
|
---|
2955 | char *pszTestPipe;
|
---|
2956 | if (txsIsStringValid(pPktHdr, "testpipe", pch, &pszTestPipe, &pch, &rc))
|
---|
2957 | {
|
---|
2958 | /* 11. username */
|
---|
2959 | char *pszUsername;
|
---|
2960 | if (txsIsStringValid(pPktHdr, "username", pch, &pszUsername, &pch, &rc))
|
---|
2961 | {
|
---|
2962 | /** @todo No password value? */
|
---|
2963 |
|
---|
2964 | /* 12. time limit */
|
---|
2965 | uint32_t const cMillies = (size_t)(pchEnd - pch) >= sizeof(uint32_t)
|
---|
2966 | ? *(uint32_t const *)pch
|
---|
2967 | : 0;
|
---|
2968 | if ((size_t)(pchEnd - pch) > sizeof(uint32_t))
|
---|
2969 | rc = txsReplyFailure(pPktHdr, "BAD END ", "Timeout argument not at end of packet (%#x)", (size_t)(pchEnd - pch));
|
---|
2970 | else if ((size_t)(pchEnd - pch) < sizeof(uint32_t))
|
---|
2971 | rc = txsReplyFailure(pPktHdr, "BAD NOTO", "No timeout argument");
|
---|
2972 | else if (cMillies < 1000)
|
---|
2973 | rc = txsReplyFailure(pPktHdr, "BAD TO ", "Timeout is less than a second (%#x)", cMillies);
|
---|
2974 | else
|
---|
2975 | {
|
---|
2976 | pch += sizeof(uint32_t);
|
---|
2977 |
|
---|
2978 | /*
|
---|
2979 | * Time to employ a helper here before we go way beyond
|
---|
2980 | * the right margin...
|
---|
2981 | */
|
---|
2982 | rc = txsDoExecHlp(pPktHdr, fFlags, pszExecName,
|
---|
2983 | cArgs, papszArgs,
|
---|
2984 | cEnvVars, papszEnv,
|
---|
2985 | pszStdIn, pszStdOut, pszStdErr, pszTestPipe,
|
---|
2986 | pszUsername,
|
---|
2987 | cMillies == UINT32_MAX ? RT_INDEFINITE_WAIT : cMillies);
|
---|
2988 | }
|
---|
2989 | RTStrFree(pszUsername);
|
---|
2990 | }
|
---|
2991 | RTStrFree(pszTestPipe);
|
---|
2992 | }
|
---|
2993 | RTStrFree(pszStdErr);
|
---|
2994 | }
|
---|
2995 | RTStrFree(pszStdOut);
|
---|
2996 | }
|
---|
2997 | RTStrFree(pszStdIn);
|
---|
2998 | }
|
---|
2999 | }
|
---|
3000 | for (size_t i = 0; i < cEnvVars; i++)
|
---|
3001 | RTStrFree(papszEnv[i]);
|
---|
3002 | RTMemTmpFree(papszEnv);
|
---|
3003 | }
|
---|
3004 | else
|
---|
3005 | rc = txsReplyFailure(pPktHdr, "NO MEM ", "Failed to allocate %zu bytes environ", sizeof(char *) * (cEnvVars + 1));
|
---|
3006 | }
|
---|
3007 | }
|
---|
3008 | for (size_t i = 0; i < cArgs; i++)
|
---|
3009 | RTStrFree(papszArgs[i]);
|
---|
3010 | RTMemTmpFree(papszArgs);
|
---|
3011 | }
|
---|
3012 | else
|
---|
3013 | rc = txsReplyFailure(pPktHdr, "NO MEM ", "Failed to allocate %zu bytes for argv", sizeof(char *) * (cArgs + 1));
|
---|
3014 | }
|
---|
3015 | RTStrFree(pszExecName);
|
---|
3016 |
|
---|
3017 | return rc;
|
---|
3018 | }
|
---|
3019 |
|
---|
3020 | /**
|
---|
3021 | * The main loop.
|
---|
3022 | *
|
---|
3023 | * @returns exit code.
|
---|
3024 | */
|
---|
3025 | static RTEXITCODE txsMainLoop(void)
|
---|
3026 | {
|
---|
3027 | RTMsgInfo("Version %s r%s %s.%s (%s %s)\n",
|
---|
3028 | RTBldCfgVersion(), RTBldCfgRevisionStr(), KBUILD_TARGET, KBUILD_TARGET_ARCH, __DATE__, __TIME__);
|
---|
3029 |
|
---|
3030 | if (g_cVerbose > 0)
|
---|
3031 | RTMsgInfo("txsMainLoop: start...\n");
|
---|
3032 | RTEXITCODE enmExitCode = RTEXITCODE_SUCCESS;
|
---|
3033 | while (!g_fTerminate)
|
---|
3034 | {
|
---|
3035 | /*
|
---|
3036 | * Read client command packet and process it.
|
---|
3037 | */
|
---|
3038 | PTXSPKTHDR pPktHdr;
|
---|
3039 | int rc = txsRecvPkt(&pPktHdr, true /*fAutoRetryOnFailure*/);
|
---|
3040 | if (RT_FAILURE(rc))
|
---|
3041 | continue;
|
---|
3042 | if (g_cVerbose > 0)
|
---|
3043 | RTMsgInfo("txsMainLoop: CMD: %.8s...", pPktHdr->achOpcode);
|
---|
3044 |
|
---|
3045 | /*
|
---|
3046 | * Do a string switch on the opcode bit.
|
---|
3047 | */
|
---|
3048 | /* Connection: */
|
---|
3049 | if ( txsIsSameOpcode(pPktHdr, "HOWDY "))
|
---|
3050 | rc = txsDoHowdy(pPktHdr);
|
---|
3051 | else if (txsIsSameOpcode(pPktHdr, "BYE "))
|
---|
3052 | rc = txsDoBye(pPktHdr);
|
---|
3053 | else if (txsIsSameOpcode(pPktHdr, "VER "))
|
---|
3054 | rc = txsDoVer(pPktHdr);
|
---|
3055 | else if (txsIsSameOpcode(pPktHdr, "UUID "))
|
---|
3056 | rc = txsDoUuid(pPktHdr);
|
---|
3057 | /* Process: */
|
---|
3058 | else if (txsIsSameOpcode(pPktHdr, "EXEC "))
|
---|
3059 | rc = txsDoExec(pPktHdr);
|
---|
3060 | /* Admin: */
|
---|
3061 | else if (txsIsSameOpcode(pPktHdr, "REBOOT "))
|
---|
3062 | rc = txsDoReboot(pPktHdr);
|
---|
3063 | else if (txsIsSameOpcode(pPktHdr, "SHUTDOWN"))
|
---|
3064 | rc = txsDoShutdown(pPktHdr);
|
---|
3065 | /* CD/DVD control: */
|
---|
3066 | else if (txsIsSameOpcode(pPktHdr, "CD EJECT"))
|
---|
3067 | rc = txsDoCdEject(pPktHdr);
|
---|
3068 | /* File system: */
|
---|
3069 | else if (txsIsSameOpcode(pPktHdr, "CLEANUP "))
|
---|
3070 | rc = txsDoCleanup(pPktHdr);
|
---|
3071 | else if (txsIsSameOpcode(pPktHdr, "MKDIR "))
|
---|
3072 | rc = txsDoMkDir(pPktHdr);
|
---|
3073 | else if (txsIsSameOpcode(pPktHdr, "MKDRPATH"))
|
---|
3074 | rc = txsDoMkDrPath(pPktHdr);
|
---|
3075 | else if (txsIsSameOpcode(pPktHdr, "MKSYMLNK"))
|
---|
3076 | rc = txsDoMkSymlnk(pPktHdr);
|
---|
3077 | else if (txsIsSameOpcode(pPktHdr, "RMDIR "))
|
---|
3078 | rc = txsDoRmDir(pPktHdr);
|
---|
3079 | else if (txsIsSameOpcode(pPktHdr, "RMFILE "))
|
---|
3080 | rc = txsDoRmFile(pPktHdr);
|
---|
3081 | else if (txsIsSameOpcode(pPktHdr, "RMSYMLNK"))
|
---|
3082 | rc = txsDoRmSymlnk(pPktHdr);
|
---|
3083 | else if (txsIsSameOpcode(pPktHdr, "RMTREE "))
|
---|
3084 | rc = txsDoRmTree(pPktHdr);
|
---|
3085 | else if (txsIsSameOpcode(pPktHdr, "CHMOD "))
|
---|
3086 | rc = txsDoChMod(pPktHdr);
|
---|
3087 | else if (txsIsSameOpcode(pPktHdr, "CHOWN "))
|
---|
3088 | rc = txsDoChOwn(pPktHdr);
|
---|
3089 | else if (txsIsSameOpcode(pPktHdr, "ISDIR "))
|
---|
3090 | rc = txsDoIsDir(pPktHdr);
|
---|
3091 | else if (txsIsSameOpcode(pPktHdr, "ISFILE "))
|
---|
3092 | rc = txsDoIsFile(pPktHdr);
|
---|
3093 | else if (txsIsSameOpcode(pPktHdr, "ISSYMLNK"))
|
---|
3094 | rc = txsDoIsSymlnk(pPktHdr);
|
---|
3095 | else if (txsIsSameOpcode(pPktHdr, "STAT "))
|
---|
3096 | rc = txsDoStat(pPktHdr);
|
---|
3097 | else if (txsIsSameOpcode(pPktHdr, "LSTAT "))
|
---|
3098 | rc = txsDoLStat(pPktHdr);
|
---|
3099 | else if (txsIsSameOpcode(pPktHdr, "LIST "))
|
---|
3100 | rc = txsDoList(pPktHdr);
|
---|
3101 | else if (txsIsSameOpcode(pPktHdr, "PUT FILE"))
|
---|
3102 | rc = txsDoPutFile(pPktHdr, false /*fHasMode*/);
|
---|
3103 | else if (txsIsSameOpcode(pPktHdr, "PUT2FILE"))
|
---|
3104 | rc = txsDoPutFile(pPktHdr, true /*fHasMode*/);
|
---|
3105 | else if (txsIsSameOpcode(pPktHdr, "GET FILE"))
|
---|
3106 | rc = txsDoGetFile(pPktHdr);
|
---|
3107 | else if (txsIsSameOpcode(pPktHdr, "PKFILE "))
|
---|
3108 | rc = txsDoPackFile(pPktHdr);
|
---|
3109 | else if (txsIsSameOpcode(pPktHdr, "UNPKFILE"))
|
---|
3110 | rc = txsDoUnpackFile(pPktHdr);
|
---|
3111 | /* Misc: */
|
---|
3112 | else if (txsIsSameOpcode(pPktHdr, "EXP STR "))
|
---|
3113 | rc = txsDoExpandString(pPktHdr);
|
---|
3114 | else
|
---|
3115 | rc = txsReplyUnknown(pPktHdr);
|
---|
3116 |
|
---|
3117 | if (g_cVerbose > 0)
|
---|
3118 | RTMsgInfo("txsMainLoop: CMD: %.8s -> %Rrc", pPktHdr->achOpcode, rc);
|
---|
3119 | RTMemFree(pPktHdr);
|
---|
3120 | }
|
---|
3121 |
|
---|
3122 | if (g_cVerbose > 0)
|
---|
3123 | RTMsgInfo("txsMainLoop: end\n");
|
---|
3124 | return enmExitCode;
|
---|
3125 | }
|
---|
3126 |
|
---|
3127 |
|
---|
3128 | /**
|
---|
3129 | * Finalizes the scratch directory, making sure it exists.
|
---|
3130 | *
|
---|
3131 | * @returns exit code.
|
---|
3132 | */
|
---|
3133 | static RTEXITCODE txsFinalizeScratch(void)
|
---|
3134 | {
|
---|
3135 | RTPathStripTrailingSlash(g_szScratchPath);
|
---|
3136 | char *pszFilename = RTPathFilename(g_szScratchPath);
|
---|
3137 | if (!pszFilename)
|
---|
3138 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "cannot use root for scratch (%s)\n", g_szScratchPath);
|
---|
3139 |
|
---|
3140 | int rc;
|
---|
3141 | if (strchr(pszFilename, 'X'))
|
---|
3142 | {
|
---|
3143 | char ch = *pszFilename;
|
---|
3144 | rc = RTDirCreateFullPath(g_szScratchPath, 0700);
|
---|
3145 | *pszFilename = ch;
|
---|
3146 | if (RT_SUCCESS(rc))
|
---|
3147 | rc = RTDirCreateTemp(g_szScratchPath, 0700);
|
---|
3148 | }
|
---|
3149 | else
|
---|
3150 | {
|
---|
3151 | if (RTDirExists(g_szScratchPath))
|
---|
3152 | rc = VINF_SUCCESS;
|
---|
3153 | else
|
---|
3154 | rc = RTDirCreateFullPath(g_szScratchPath, 0700);
|
---|
3155 | }
|
---|
3156 | if (RT_FAILURE(rc))
|
---|
3157 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to create scratch directory: %Rrc (%s)\n", rc, g_szScratchPath);
|
---|
3158 | return RTEXITCODE_SUCCESS;
|
---|
3159 | }
|
---|
3160 |
|
---|
3161 | /**
|
---|
3162 | * Attempts to complete an upgrade by updating the original and relaunching
|
---|
3163 | * ourselves from there again.
|
---|
3164 | *
|
---|
3165 | * On failure, we'll continue running as the temporary copy.
|
---|
3166 | *
|
---|
3167 | * @returns Exit code. Exit if this is non-zero or @a *pfExit is set.
|
---|
3168 | * @param argc The number of arguments.
|
---|
3169 | * @param argv The argument vector.
|
---|
3170 | * @param pfExit For indicating exit when the exit code is zero.
|
---|
3171 | * @param pszUpgrading The upgraded image path.
|
---|
3172 | */
|
---|
3173 | static RTEXITCODE txsAutoUpdateStage2(int argc, char **argv, bool *pfExit, const char *pszUpgrading)
|
---|
3174 | {
|
---|
3175 | if (g_cVerbose > 0)
|
---|
3176 | RTMsgInfo("Auto update stage 2...");
|
---|
3177 |
|
---|
3178 | /*
|
---|
3179 | * Copy the current executable onto the original.
|
---|
3180 | * Note that we're racing the original program on some platforms, thus the
|
---|
3181 | * 60 sec sleep mess.
|
---|
3182 | */
|
---|
3183 | char szUpgradePath[RTPATH_MAX];
|
---|
3184 | if (!RTProcGetExecutablePath(szUpgradePath, sizeof(szUpgradePath)))
|
---|
3185 | {
|
---|
3186 | RTMsgError("RTProcGetExecutablePath failed (step 2)\n");
|
---|
3187 | return RTEXITCODE_SUCCESS;
|
---|
3188 | }
|
---|
3189 | void *pvUpgrade;
|
---|
3190 | size_t cbUpgrade;
|
---|
3191 | int rc = RTFileReadAll(szUpgradePath, &pvUpgrade, &cbUpgrade);
|
---|
3192 | if (RT_FAILURE(rc))
|
---|
3193 | {
|
---|
3194 | RTMsgError("RTFileReadAllEx(\"%s\"): %Rrc (step 2)\n", szUpgradePath, rc);
|
---|
3195 | return RTEXITCODE_SUCCESS;
|
---|
3196 | }
|
---|
3197 |
|
---|
3198 | uint64_t StartMilliTS = RTTimeMilliTS();
|
---|
3199 | RTFILE hFile;
|
---|
3200 | rc = RTFileOpen(&hFile, pszUpgrading,
|
---|
3201 | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE
|
---|
3202 | | (0755 << RTFILE_O_CREATE_MODE_SHIFT));
|
---|
3203 | while ( RT_FAILURE(rc)
|
---|
3204 | && RTTimeMilliTS() - StartMilliTS < 60000)
|
---|
3205 | {
|
---|
3206 | RTThreadSleep(1000);
|
---|
3207 | rc = RTFileOpen(&hFile, pszUpgrading,
|
---|
3208 | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE
|
---|
3209 | | (0755 << RTFILE_O_CREATE_MODE_SHIFT));
|
---|
3210 | }
|
---|
3211 | if (RT_SUCCESS(rc))
|
---|
3212 | {
|
---|
3213 | rc = RTFileWrite(hFile, pvUpgrade, cbUpgrade, NULL);
|
---|
3214 | RTFileClose(hFile);
|
---|
3215 | if (RT_SUCCESS(rc))
|
---|
3216 | {
|
---|
3217 | /*
|
---|
3218 | * Relaunch the service with the original name, foricbly barring
|
---|
3219 | * further upgrade cycles in case of bugs (and simplifying the code).
|
---|
3220 | */
|
---|
3221 | const char **papszArgs = (const char **)RTMemAlloc((argc + 1 + 1) * sizeof(char **));
|
---|
3222 | if (papszArgs)
|
---|
3223 | {
|
---|
3224 | papszArgs[0] = pszUpgrading;
|
---|
3225 | for (int i = 1; i < argc; i++)
|
---|
3226 | papszArgs[i] = argv[i];
|
---|
3227 | papszArgs[argc] = "--no-auto-upgrade";
|
---|
3228 | papszArgs[argc + 1] = NULL;
|
---|
3229 |
|
---|
3230 | RTMsgInfo("Launching upgraded image: \"%s\"\n", pszUpgrading);
|
---|
3231 | RTPROCESS hProc;
|
---|
3232 | rc = RTProcCreate(pszUpgrading, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc);
|
---|
3233 | if (RT_SUCCESS(rc))
|
---|
3234 | *pfExit = true;
|
---|
3235 | else
|
---|
3236 | RTMsgError("RTProcCreate(\"%s\"): %Rrc (upgrade stage 2)\n", pszUpgrading, rc);
|
---|
3237 | RTMemFree(papszArgs);
|
---|
3238 | }
|
---|
3239 | else
|
---|
3240 | RTMsgError("RTMemAlloc failed during upgrade attempt (stage 2)\n");
|
---|
3241 | }
|
---|
3242 | else
|
---|
3243 | RTMsgError("RTFileWrite(%s,,%zu): %Rrc (step 2) - BAD\n", pszUpgrading, cbUpgrade, rc);
|
---|
3244 | }
|
---|
3245 | else
|
---|
3246 | RTMsgError("RTFileOpen(,%s,): %Rrc\n", pszUpgrading, rc);
|
---|
3247 | RTFileReadAllFree(pvUpgrade, cbUpgrade);
|
---|
3248 | return RTEXITCODE_SUCCESS;
|
---|
3249 | }
|
---|
3250 |
|
---|
3251 | /**
|
---|
3252 | * Checks for an upgrade and respawns if there is.
|
---|
3253 | *
|
---|
3254 | * @returns Exit code. Exit if this is non-zero or @a *pfExit is set.
|
---|
3255 | * @param argc The number of arguments.
|
---|
3256 | * @param argv The argument vector.
|
---|
3257 | * @param cSecsCdWait Number of seconds to wait on the CD.
|
---|
3258 | * @param pfExit For indicating exit when the exit code is zero.
|
---|
3259 | */
|
---|
3260 | static RTEXITCODE txsAutoUpdateStage1(int argc, char **argv, uint32_t cSecsCdWait, bool *pfExit)
|
---|
3261 | {
|
---|
3262 | if (g_cVerbose > 1)
|
---|
3263 | RTMsgInfo("Auto update stage 1...");
|
---|
3264 |
|
---|
3265 | /*
|
---|
3266 | * Figure names of the current service image and the potential upgrade.
|
---|
3267 | */
|
---|
3268 | char szOrgPath[RTPATH_MAX];
|
---|
3269 | if (!RTProcGetExecutablePath(szOrgPath, sizeof(szOrgPath)))
|
---|
3270 | {
|
---|
3271 | RTMsgError("RTProcGetExecutablePath failed\n");
|
---|
3272 | return RTEXITCODE_SUCCESS;
|
---|
3273 | }
|
---|
3274 |
|
---|
3275 | char szUpgradePath[RTPATH_MAX];
|
---|
3276 | int rc = RTPathJoin(szUpgradePath, sizeof(szUpgradePath), g_szCdRomPath, g_szOsSlashArchShortName);
|
---|
3277 | if (RT_SUCCESS(rc))
|
---|
3278 | rc = RTPathAppend(szUpgradePath, sizeof(szUpgradePath), RTPathFilename(szOrgPath));
|
---|
3279 | if (RT_FAILURE(rc))
|
---|
3280 | {
|
---|
3281 | RTMsgError("Failed to construct path to potential service upgrade: %Rrc\n", rc);
|
---|
3282 | return RTEXITCODE_SUCCESS;
|
---|
3283 | }
|
---|
3284 |
|
---|
3285 | /*
|
---|
3286 | * Query information about the two images and read the entire potential source file.
|
---|
3287 | * Because the CD may take a little time to be mounted when the system boots, we
|
---|
3288 | * need to do some fudging here.
|
---|
3289 | */
|
---|
3290 | uint64_t nsStart = RTTimeNanoTS();
|
---|
3291 | RTFSOBJINFO UpgradeInfo;
|
---|
3292 | for (;;)
|
---|
3293 | {
|
---|
3294 | rc = RTPathQueryInfo(szUpgradePath, &UpgradeInfo, RTFSOBJATTRADD_NOTHING);
|
---|
3295 | if (RT_SUCCESS(rc))
|
---|
3296 | break;
|
---|
3297 | if ( rc != VERR_FILE_NOT_FOUND
|
---|
3298 | && rc != VERR_PATH_NOT_FOUND
|
---|
3299 | && rc != VERR_MEDIA_NOT_PRESENT
|
---|
3300 | && rc != VERR_MEDIA_NOT_RECOGNIZED)
|
---|
3301 | {
|
---|
3302 | RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (upgrade)\n", szUpgradePath, rc);
|
---|
3303 | return RTEXITCODE_SUCCESS;
|
---|
3304 | }
|
---|
3305 | uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
|
---|
3306 | if (cNsElapsed >= cSecsCdWait * RT_NS_1SEC_64)
|
---|
3307 | {
|
---|
3308 | if (g_cVerbose > 0)
|
---|
3309 | RTMsgInfo("Auto update: Giving up waiting for media.");
|
---|
3310 | return RTEXITCODE_SUCCESS;
|
---|
3311 | }
|
---|
3312 | RTThreadSleep(500);
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | RTFSOBJINFO OrgInfo;
|
---|
3316 | rc = RTPathQueryInfo(szOrgPath, &OrgInfo, RTFSOBJATTRADD_NOTHING);
|
---|
3317 | if (RT_FAILURE(rc))
|
---|
3318 | {
|
---|
3319 | RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (old)\n", szOrgPath, rc);
|
---|
3320 | return RTEXITCODE_SUCCESS;
|
---|
3321 | }
|
---|
3322 |
|
---|
3323 | void *pvUpgrade;
|
---|
3324 | size_t cbUpgrade;
|
---|
3325 | rc = RTFileReadAllEx(szUpgradePath, 0, UpgradeInfo.cbObject, RTFILE_RDALL_O_DENY_NONE, &pvUpgrade, &cbUpgrade);
|
---|
3326 | if (RT_FAILURE(rc))
|
---|
3327 | {
|
---|
3328 | RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (old)\n", szOrgPath, rc);
|
---|
3329 | return RTEXITCODE_SUCCESS;
|
---|
3330 | }
|
---|
3331 |
|
---|
3332 | /*
|
---|
3333 | * Compare and see if we've got a different service image or not.
|
---|
3334 | */
|
---|
3335 | if (OrgInfo.cbObject == UpgradeInfo.cbObject)
|
---|
3336 | {
|
---|
3337 | /* must compare bytes. */
|
---|
3338 | void *pvOrg;
|
---|
3339 | size_t cbOrg;
|
---|
3340 | rc = RTFileReadAllEx(szOrgPath, 0, OrgInfo.cbObject, RTFILE_RDALL_O_DENY_NONE, &pvOrg, &cbOrg);
|
---|
3341 | if (RT_FAILURE(rc))
|
---|
3342 | {
|
---|
3343 | RTMsgError("RTFileReadAllEx(\"%s\"): %Rrc\n", szOrgPath, rc);
|
---|
3344 | RTFileReadAllFree(pvUpgrade, cbUpgrade);
|
---|
3345 | return RTEXITCODE_SUCCESS;
|
---|
3346 | }
|
---|
3347 | bool fSame = !memcmp(pvUpgrade, pvOrg, OrgInfo.cbObject);
|
---|
3348 | RTFileReadAllFree(pvOrg, cbOrg);
|
---|
3349 | if (fSame)
|
---|
3350 | {
|
---|
3351 | RTFileReadAllFree(pvUpgrade, cbUpgrade);
|
---|
3352 | if (g_cVerbose > 0)
|
---|
3353 | RTMsgInfo("Auto update: Not necessary.");
|
---|
3354 | return RTEXITCODE_SUCCESS;
|
---|
3355 | }
|
---|
3356 | }
|
---|
3357 |
|
---|
3358 | /*
|
---|
3359 | * Should upgrade. Start by creating an executable copy of the update
|
---|
3360 | * image in the scratch area.
|
---|
3361 | */
|
---|
3362 | RTEXITCODE rcExit = txsFinalizeScratch();
|
---|
3363 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
3364 | {
|
---|
3365 | char szTmpPath[RTPATH_MAX];
|
---|
3366 | rc = RTPathJoin(szTmpPath, sizeof(szTmpPath), g_szScratchPath, RTPathFilename(szOrgPath));
|
---|
3367 | if (RT_SUCCESS(rc))
|
---|
3368 | {
|
---|
3369 | RTFileDelete(szTmpPath); /* shouldn't hurt. */
|
---|
3370 |
|
---|
3371 | RTFILE hFile;
|
---|
3372 | rc = RTFileOpen(&hFile, szTmpPath,
|
---|
3373 | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE
|
---|
3374 | | (0755 << RTFILE_O_CREATE_MODE_SHIFT));
|
---|
3375 | if (RT_SUCCESS(rc))
|
---|
3376 | {
|
---|
3377 | rc = RTFileWrite(hFile, pvUpgrade, UpgradeInfo.cbObject, NULL);
|
---|
3378 | RTFileClose(hFile);
|
---|
3379 | if (RT_SUCCESS(rc))
|
---|
3380 | {
|
---|
3381 | /*
|
---|
3382 | * Try execute the new image and quit if it works.
|
---|
3383 | */
|
---|
3384 | const char **papszArgs = (const char **)RTMemAlloc((argc + 2 + 1) * sizeof(char **));
|
---|
3385 | if (papszArgs)
|
---|
3386 | {
|
---|
3387 | papszArgs[0] = szTmpPath;
|
---|
3388 | for (int i = 1; i < argc; i++)
|
---|
3389 | papszArgs[i] = argv[i];
|
---|
3390 | papszArgs[argc] = "--upgrading";
|
---|
3391 | papszArgs[argc + 1] = szOrgPath;
|
---|
3392 | papszArgs[argc + 2] = NULL;
|
---|
3393 |
|
---|
3394 | RTMsgInfo("Launching intermediate automatic upgrade stage: \"%s\"\n", szTmpPath);
|
---|
3395 | RTPROCESS hProc;
|
---|
3396 | rc = RTProcCreate(szTmpPath, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc);
|
---|
3397 | if (RT_SUCCESS(rc))
|
---|
3398 | *pfExit = true;
|
---|
3399 | else
|
---|
3400 | RTMsgError("RTProcCreate(\"%s\"): %Rrc (upgrade stage 1)\n", szTmpPath, rc);
|
---|
3401 | RTMemFree(papszArgs);
|
---|
3402 | }
|
---|
3403 | else
|
---|
3404 | RTMsgError("RTMemAlloc failed during upgrade attempt (stage)\n");
|
---|
3405 | }
|
---|
3406 | else
|
---|
3407 | RTMsgError("RTFileWrite(%s,,%zu): %Rrc\n", szTmpPath, UpgradeInfo.cbObject, rc);
|
---|
3408 | }
|
---|
3409 | else
|
---|
3410 | RTMsgError("RTFileOpen(,%s,): %Rrc\n", szTmpPath, rc);
|
---|
3411 | }
|
---|
3412 | else
|
---|
3413 | RTMsgError("Failed to construct path to temporary upgrade image: %Rrc\n", rc);
|
---|
3414 | }
|
---|
3415 |
|
---|
3416 | RTFileReadAllFree(pvUpgrade, cbUpgrade);
|
---|
3417 | return rcExit;
|
---|
3418 | }
|
---|
3419 |
|
---|
3420 | /**
|
---|
3421 | * Determines the default configuration.
|
---|
3422 | */
|
---|
3423 | static void txsSetDefaults(void)
|
---|
3424 | {
|
---|
3425 | /*
|
---|
3426 | * OS and ARCH.
|
---|
3427 | */
|
---|
3428 | AssertCompile(sizeof(KBUILD_TARGET) <= sizeof(g_szOsShortName));
|
---|
3429 | strcpy(g_szOsShortName, KBUILD_TARGET);
|
---|
3430 |
|
---|
3431 | AssertCompile(sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szArchShortName));
|
---|
3432 | strcpy(g_szArchShortName, KBUILD_TARGET_ARCH);
|
---|
3433 |
|
---|
3434 | AssertCompile(sizeof(KBUILD_TARGET) + sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szOsDotArchShortName));
|
---|
3435 | strcpy(g_szOsDotArchShortName, KBUILD_TARGET);
|
---|
3436 | g_szOsDotArchShortName[sizeof(KBUILD_TARGET) - 1] = '.';
|
---|
3437 | strcpy(&g_szOsDotArchShortName[sizeof(KBUILD_TARGET)], KBUILD_TARGET_ARCH);
|
---|
3438 |
|
---|
3439 | AssertCompile(sizeof(KBUILD_TARGET) + sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szOsSlashArchShortName));
|
---|
3440 | strcpy(g_szOsSlashArchShortName, KBUILD_TARGET);
|
---|
3441 | g_szOsSlashArchShortName[sizeof(KBUILD_TARGET) - 1] = '/';
|
---|
3442 | strcpy(&g_szOsSlashArchShortName[sizeof(KBUILD_TARGET)], KBUILD_TARGET_ARCH);
|
---|
3443 |
|
---|
3444 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
3445 | strcpy(g_szExeSuff, ".exe");
|
---|
3446 | strcpy(g_szScriptSuff, ".cmd");
|
---|
3447 | #else
|
---|
3448 | strcpy(g_szExeSuff, "");
|
---|
3449 | strcpy(g_szScriptSuff, ".sh");
|
---|
3450 | #endif
|
---|
3451 |
|
---|
3452 | int rc = RTPathGetCurrent(g_szCwd, sizeof(g_szCwd));
|
---|
3453 | if (RT_FAILURE(rc))
|
---|
3454 | RTMsgError("RTPathGetCurrent failed: %Rrc\n", rc);
|
---|
3455 | g_szCwd[sizeof(g_szCwd) - 1] = '\0';
|
---|
3456 |
|
---|
3457 | if (!RTProcGetExecutablePath(g_szTxsDir, sizeof(g_szTxsDir)))
|
---|
3458 | RTMsgError("RTProcGetExecutablePath failed!\n");
|
---|
3459 | g_szTxsDir[sizeof(g_szTxsDir) - 1] = '\0';
|
---|
3460 | RTPathStripFilename(g_szTxsDir);
|
---|
3461 | RTPathStripTrailingSlash(g_szTxsDir);
|
---|
3462 |
|
---|
3463 | /*
|
---|
3464 | * The CD/DVD-ROM location.
|
---|
3465 | */
|
---|
3466 | /** @todo do a better job here :-) */
|
---|
3467 | #ifdef RT_OS_WINDOWS
|
---|
3468 | strcpy(g_szDefCdRomPath, "D:/");
|
---|
3469 | #elif defined(RT_OS_OS2)
|
---|
3470 | strcpy(g_szDefCdRomPath, "D:/");
|
---|
3471 | #else
|
---|
3472 | if (RTDirExists("/media"))
|
---|
3473 | strcpy(g_szDefCdRomPath, "/media/cdrom");
|
---|
3474 | else
|
---|
3475 | strcpy(g_szDefCdRomPath, "/mnt/cdrom");
|
---|
3476 | #endif
|
---|
3477 | strcpy(g_szCdRomPath, g_szDefCdRomPath);
|
---|
3478 |
|
---|
3479 | /*
|
---|
3480 | * Temporary directory.
|
---|
3481 | */
|
---|
3482 | rc = RTPathTemp(g_szDefScratchPath, sizeof(g_szDefScratchPath));
|
---|
3483 | if (RT_SUCCESS(rc))
|
---|
3484 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) || defined(RT_OS_DOS)
|
---|
3485 | rc = RTPathAppend(g_szDefScratchPath, sizeof(g_szDefScratchPath), "txs-XXXX.tmp");
|
---|
3486 | #else
|
---|
3487 | rc = RTPathAppend(g_szDefScratchPath, sizeof(g_szDefScratchPath), "txs-XXXXXXXXX.tmp");
|
---|
3488 | #endif
|
---|
3489 | if (RT_FAILURE(rc))
|
---|
3490 | {
|
---|
3491 | RTMsgError("RTPathTemp/Append failed when constructing scratch path: %Rrc\n", rc);
|
---|
3492 | strcpy(g_szDefScratchPath, "/tmp/txs-XXXX.tmp");
|
---|
3493 | }
|
---|
3494 | strcpy(g_szScratchPath, g_szDefScratchPath);
|
---|
3495 |
|
---|
3496 | /*
|
---|
3497 | * The default transporter is the first one.
|
---|
3498 | */
|
---|
3499 | g_pTransport = g_apTransports[0];
|
---|
3500 | }
|
---|
3501 |
|
---|
3502 | /**
|
---|
3503 | * Prints the usage.
|
---|
3504 | *
|
---|
3505 | * @param pStrm Where to print it.
|
---|
3506 | * @param pszArgv0 The program name (argv[0]).
|
---|
3507 | */
|
---|
3508 | static void txsUsage(PRTSTREAM pStrm, const char *pszArgv0)
|
---|
3509 | {
|
---|
3510 | RTStrmPrintf(pStrm,
|
---|
3511 | "Usage: %Rbn [options]\n"
|
---|
3512 | "\n"
|
---|
3513 | "Options:\n"
|
---|
3514 | " --cdrom <path>\n"
|
---|
3515 | " Where the CD/DVD-ROM will be mounted.\n"
|
---|
3516 | " Default: %s\n"
|
---|
3517 | " --scratch <path>\n"
|
---|
3518 | " Where to put scratch files.\n"
|
---|
3519 | " Default: %s \n"
|
---|
3520 | ,
|
---|
3521 | pszArgv0,
|
---|
3522 | g_szDefCdRomPath,
|
---|
3523 | g_szDefScratchPath);
|
---|
3524 | RTStrmPrintf(pStrm,
|
---|
3525 | " --transport <name>\n"
|
---|
3526 | " Use the specified transport layer, one of the following:\n");
|
---|
3527 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3528 | RTStrmPrintf(pStrm, " %s - %s\n", g_apTransports[i]->szName, g_apTransports[i]->pszDesc);
|
---|
3529 | RTStrmPrintf(pStrm, " Default: %s\n", g_pTransport->szName);
|
---|
3530 | RTStrmPrintf(pStrm,
|
---|
3531 | " --auto-upgrade, --no-auto-upgrade\n"
|
---|
3532 | " To enable or disable the automatic upgrade mechanism where any different\n"
|
---|
3533 | " version found on the CD-ROM on startup will replace the initial copy.\n"
|
---|
3534 | " Default: --auto-upgrade\n"
|
---|
3535 | " --wait-cdrom <secs>\n"
|
---|
3536 | " Number of seconds to wait for the CD-ROM to be mounted before giving up\n"
|
---|
3537 | " on automatic upgrading.\n"
|
---|
3538 | " Default: --wait-cdrom 1; solaris: --wait-cdrom 8\n"
|
---|
3539 | " --upgrading <org-path>\n"
|
---|
3540 | " Internal use only.\n");
|
---|
3541 | RTStrmPrintf(pStrm,
|
---|
3542 | " --display-output, --no-display-output\n"
|
---|
3543 | " Display the output and the result of all child processes.\n");
|
---|
3544 | RTStrmPrintf(pStrm,
|
---|
3545 | " --foreground\n"
|
---|
3546 | " Don't daemonize, run in the foreground.\n");
|
---|
3547 | RTStrmPrintf(pStrm,
|
---|
3548 | " --verbose, -v\n"
|
---|
3549 | " Increases the verbosity level. Can be specified multiple times.\n");
|
---|
3550 | RTStrmPrintf(pStrm,
|
---|
3551 | " --quiet, -q\n"
|
---|
3552 | " Mutes any logging output.\n");
|
---|
3553 | RTStrmPrintf(pStrm,
|
---|
3554 | " --help, -h, -?\n"
|
---|
3555 | " Display this message and exit.\n"
|
---|
3556 | " --version, -V\n"
|
---|
3557 | " Display the version and exit.\n");
|
---|
3558 |
|
---|
3559 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3560 | if (g_apTransports[i]->cOpts)
|
---|
3561 | {
|
---|
3562 | RTStrmPrintf(pStrm,
|
---|
3563 | "\n"
|
---|
3564 | "Options for %s:\n", g_apTransports[i]->szName);
|
---|
3565 | g_apTransports[i]->pfnUsage(g_pStdOut);
|
---|
3566 | }
|
---|
3567 | }
|
---|
3568 |
|
---|
3569 | /**
|
---|
3570 | * Parses the arguments.
|
---|
3571 | *
|
---|
3572 | * @returns Exit code. Exit if this is non-zero or @a *pfExit is set.
|
---|
3573 | * @param argc The number of arguments.
|
---|
3574 | * @param argv The argument vector.
|
---|
3575 | * @param pfExit For indicating exit when the exit code is zero.
|
---|
3576 | */
|
---|
3577 | static RTEXITCODE txsParseArgv(int argc, char **argv, bool *pfExit)
|
---|
3578 | {
|
---|
3579 | *pfExit = false;
|
---|
3580 |
|
---|
3581 | /*
|
---|
3582 | * Storage for locally handled options.
|
---|
3583 | */
|
---|
3584 | bool fAutoUpgrade = true;
|
---|
3585 | bool fDaemonize = true;
|
---|
3586 | bool fDaemonized = false;
|
---|
3587 | const char *pszUpgrading = NULL;
|
---|
3588 | #ifdef RT_OS_SOLARIS
|
---|
3589 | uint32_t cSecsCdWait = 8;
|
---|
3590 | #else
|
---|
3591 | uint32_t cSecsCdWait = 1;
|
---|
3592 | #endif
|
---|
3593 |
|
---|
3594 | /*
|
---|
3595 | * Combine the base and transport layer option arrays.
|
---|
3596 | */
|
---|
3597 | static const RTGETOPTDEF s_aBaseOptions[] =
|
---|
3598 | {
|
---|
3599 | { "--transport", 't', RTGETOPT_REQ_STRING },
|
---|
3600 | { "--cdrom", 'c', RTGETOPT_REQ_STRING },
|
---|
3601 | { "--wait-cdrom", 'w', RTGETOPT_REQ_UINT32 },
|
---|
3602 | { "--scratch", 's', RTGETOPT_REQ_STRING },
|
---|
3603 | { "--auto-upgrade", 'a', RTGETOPT_REQ_NOTHING },
|
---|
3604 | { "--no-auto-upgrade", 'A', RTGETOPT_REQ_NOTHING },
|
---|
3605 | { "--upgrading", 'U', RTGETOPT_REQ_STRING },
|
---|
3606 | { "--display-output", 'd', RTGETOPT_REQ_NOTHING },
|
---|
3607 | { "--no-display-output",'D', RTGETOPT_REQ_NOTHING },
|
---|
3608 | { "--foreground", 'f', RTGETOPT_REQ_NOTHING },
|
---|
3609 | { "--daemonized", 'Z', RTGETOPT_REQ_NOTHING },
|
---|
3610 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
3611 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
3612 | };
|
---|
3613 |
|
---|
3614 | size_t cOptions = RT_ELEMENTS(s_aBaseOptions);
|
---|
3615 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3616 | cOptions += g_apTransports[i]->cOpts;
|
---|
3617 |
|
---|
3618 | PRTGETOPTDEF paOptions = (PRTGETOPTDEF)alloca(cOptions * sizeof(RTGETOPTDEF));
|
---|
3619 | if (!paOptions)
|
---|
3620 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "alloca failed\n");
|
---|
3621 |
|
---|
3622 | memcpy(paOptions, s_aBaseOptions, sizeof(s_aBaseOptions));
|
---|
3623 | cOptions = RT_ELEMENTS(s_aBaseOptions);
|
---|
3624 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3625 | {
|
---|
3626 | memcpy(&paOptions[cOptions], g_apTransports[i]->paOpts, g_apTransports[i]->cOpts * sizeof(RTGETOPTDEF));
|
---|
3627 | cOptions += g_apTransports[i]->cOpts;
|
---|
3628 | }
|
---|
3629 |
|
---|
3630 | /*
|
---|
3631 | * Parse the arguments.
|
---|
3632 | */
|
---|
3633 | RTGETOPTSTATE GetState;
|
---|
3634 | int rc = RTGetOptInit(&GetState, argc, argv, paOptions, cOptions, 1, 0 /* fFlags */);
|
---|
3635 | AssertRC(rc);
|
---|
3636 |
|
---|
3637 | int ch;
|
---|
3638 | RTGETOPTUNION Val;
|
---|
3639 | while ((ch = RTGetOpt(&GetState, &Val)))
|
---|
3640 | {
|
---|
3641 | switch (ch)
|
---|
3642 | {
|
---|
3643 | case 'a':
|
---|
3644 | fAutoUpgrade = true;
|
---|
3645 | break;
|
---|
3646 |
|
---|
3647 | case 'A':
|
---|
3648 | fAutoUpgrade = false;
|
---|
3649 | break;
|
---|
3650 |
|
---|
3651 | case 'c':
|
---|
3652 | rc = RTStrCopy(g_szCdRomPath, sizeof(g_szCdRomPath), Val.psz);
|
---|
3653 | if (RT_FAILURE(rc))
|
---|
3654 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "CD/DVD-ROM is path too long (%Rrc)\n", rc);
|
---|
3655 | break;
|
---|
3656 |
|
---|
3657 | case 'd':
|
---|
3658 | g_fDisplayOutput = true;
|
---|
3659 | break;
|
---|
3660 |
|
---|
3661 | case 'D':
|
---|
3662 | g_fDisplayOutput = false;
|
---|
3663 | break;
|
---|
3664 |
|
---|
3665 | case 'f':
|
---|
3666 | fDaemonize = false;
|
---|
3667 | break;
|
---|
3668 |
|
---|
3669 | case 'h':
|
---|
3670 | txsUsage(g_pStdOut, argv[0]);
|
---|
3671 | *pfExit = true;
|
---|
3672 | return RTEXITCODE_SUCCESS;
|
---|
3673 |
|
---|
3674 | case 's':
|
---|
3675 | rc = RTStrCopy(g_szScratchPath, sizeof(g_szScratchPath), Val.psz);
|
---|
3676 | if (RT_FAILURE(rc))
|
---|
3677 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "scratch path is too long (%Rrc)\n", rc);
|
---|
3678 | break;
|
---|
3679 |
|
---|
3680 | case 't':
|
---|
3681 | {
|
---|
3682 | PCTXSTRANSPORT pTransport = NULL;
|
---|
3683 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3684 | if (!strcmp(g_apTransports[i]->szName, Val.psz))
|
---|
3685 | {
|
---|
3686 | pTransport = g_apTransports[i];
|
---|
3687 | break;
|
---|
3688 | }
|
---|
3689 | if (!pTransport)
|
---|
3690 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown transport layer name '%s'\n", Val.psz);
|
---|
3691 | g_pTransport = pTransport;
|
---|
3692 | break;
|
---|
3693 | }
|
---|
3694 |
|
---|
3695 | case 'U':
|
---|
3696 | pszUpgrading = Val.psz;
|
---|
3697 | break;
|
---|
3698 |
|
---|
3699 | case 'w':
|
---|
3700 | cSecsCdWait = Val.u32;
|
---|
3701 | break;
|
---|
3702 |
|
---|
3703 | case 'q':
|
---|
3704 | g_cVerbose = 0;
|
---|
3705 | break;
|
---|
3706 |
|
---|
3707 | case 'v':
|
---|
3708 | g_cVerbose++;
|
---|
3709 | break;
|
---|
3710 |
|
---|
3711 | case 'V':
|
---|
3712 | RTPrintf("$Revision: 84924 $\n");
|
---|
3713 | *pfExit = true;
|
---|
3714 | return RTEXITCODE_SUCCESS;
|
---|
3715 |
|
---|
3716 | case 'Z':
|
---|
3717 | fDaemonized = true;
|
---|
3718 | fDaemonize = false;
|
---|
3719 | break;
|
---|
3720 |
|
---|
3721 | default:
|
---|
3722 | {
|
---|
3723 | rc = VERR_TRY_AGAIN;
|
---|
3724 | for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++)
|
---|
3725 | if (g_apTransports[i]->cOpts)
|
---|
3726 | {
|
---|
3727 | rc = g_apTransports[i]->pfnOption(ch, &Val);
|
---|
3728 | if (RT_SUCCESS(rc))
|
---|
3729 | break;
|
---|
3730 | if (rc != VERR_TRY_AGAIN)
|
---|
3731 | {
|
---|
3732 | *pfExit = true;
|
---|
3733 | return RTEXITCODE_SYNTAX;
|
---|
3734 | }
|
---|
3735 | }
|
---|
3736 | if (rc == VERR_TRY_AGAIN)
|
---|
3737 | {
|
---|
3738 | *pfExit = true;
|
---|
3739 | return RTGetOptPrintError(ch, &Val);
|
---|
3740 | }
|
---|
3741 | break;
|
---|
3742 | }
|
---|
3743 | }
|
---|
3744 | }
|
---|
3745 |
|
---|
3746 | /*
|
---|
3747 | * Handle automatic upgrading of the service.
|
---|
3748 | */
|
---|
3749 | if (fAutoUpgrade && !*pfExit)
|
---|
3750 | {
|
---|
3751 | RTEXITCODE rcExit;
|
---|
3752 | if (pszUpgrading)
|
---|
3753 | rcExit = txsAutoUpdateStage2(argc, argv, pfExit, pszUpgrading);
|
---|
3754 | else
|
---|
3755 | rcExit = txsAutoUpdateStage1(argc, argv, cSecsCdWait, pfExit);
|
---|
3756 | if ( *pfExit
|
---|
3757 | || rcExit != RTEXITCODE_SUCCESS)
|
---|
3758 | return rcExit;
|
---|
3759 | }
|
---|
3760 |
|
---|
3761 | /*
|
---|
3762 | * Daemonize ourselves if asked to.
|
---|
3763 | */
|
---|
3764 | if (fDaemonize && !*pfExit)
|
---|
3765 | {
|
---|
3766 | if (g_cVerbose > 0)
|
---|
3767 | RTMsgInfo("Daemonizing...");
|
---|
3768 | rc = RTProcDaemonize(argv, "--daemonized");
|
---|
3769 | if (RT_FAILURE(rc))
|
---|
3770 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize: %Rrc\n", rc);
|
---|
3771 | *pfExit = true;
|
---|
3772 | }
|
---|
3773 |
|
---|
3774 | return RTEXITCODE_SUCCESS;
|
---|
3775 | }
|
---|
3776 |
|
---|
3777 |
|
---|
3778 | int main(int argc, char **argv)
|
---|
3779 | {
|
---|
3780 | /*
|
---|
3781 | * Initialize the runtime.
|
---|
3782 | */
|
---|
3783 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
3784 | if (RT_FAILURE(rc))
|
---|
3785 | return RTMsgInitFailure(rc);
|
---|
3786 |
|
---|
3787 | /*
|
---|
3788 | * Determine defaults and parse the arguments.
|
---|
3789 | */
|
---|
3790 | txsSetDefaults();
|
---|
3791 | bool fExit;
|
---|
3792 | RTEXITCODE rcExit = txsParseArgv(argc, argv, &fExit);
|
---|
3793 | if (rcExit != RTEXITCODE_SUCCESS || fExit)
|
---|
3794 | return rcExit;
|
---|
3795 |
|
---|
3796 | /*
|
---|
3797 | * Generate a UUID for this TXS instance.
|
---|
3798 | */
|
---|
3799 | rc = RTUuidCreate(&g_InstanceUuid);
|
---|
3800 | if (RT_FAILURE(rc))
|
---|
3801 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTUuidCreate failed: %Rrc", rc);
|
---|
3802 | if (g_cVerbose > 0)
|
---|
3803 | RTMsgInfo("Instance UUID: %RTuuid", &g_InstanceUuid);
|
---|
3804 |
|
---|
3805 | /*
|
---|
3806 | * Finalize the scratch directory and initialize the transport layer.
|
---|
3807 | */
|
---|
3808 | rcExit = txsFinalizeScratch();
|
---|
3809 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
3810 | return rcExit;
|
---|
3811 |
|
---|
3812 | rc = g_pTransport->pfnInit();
|
---|
3813 | if (RT_FAILURE(rc))
|
---|
3814 | return RTEXITCODE_FAILURE;
|
---|
3815 |
|
---|
3816 | /*
|
---|
3817 | * Ok, start working
|
---|
3818 | */
|
---|
3819 | rcExit = txsMainLoop();
|
---|
3820 |
|
---|
3821 | /*
|
---|
3822 | * Cleanup.
|
---|
3823 | */
|
---|
3824 | g_pTransport->pfnTerm();
|
---|
3825 |
|
---|
3826 | return rcExit;
|
---|
3827 | }
|
---|
3828 |
|
---|