VirtualBox

source: vbox/trunk/src/VBox/Main/src-helper-apps/os2/os2_util.c@ 93149

Last change on this file since 93149 was 93149, checked in by vboxsync, 3 years ago

Main/Unattended/os2_util: Try avoid triggering ERROR_SMG_START_IN_BACKGROUND and deal with it (it's a warning) when it occurs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.6 KB
Line 
1/* $Id: os2_util.c 93149 2022-01-08 15:59:30Z vboxsync $ */
2/** @file
3 * Os2Util - Unattended Installation Helper Utility for OS/2.
4 *
5 * Helps TEE'ing the installation script output to VBox.log and guest side log
6 * files. Also helps with displaying program exit codes, something CMD.exe can't.
7 */
8
9/*
10 * Copyright (C) 2015-2022 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21
22/*********************************************************************************************************************************
23* Header Files *
24*********************************************************************************************************************************/
25#define INCL_BASE
26#include <os2.h>
27#include <iprt/asm-amd64-x86.h>
28#include <VBox/log.h>
29
30#include "VBox/version.h"
31
32
33/*********************************************************************************************************************************
34* Defined Constants And Macros *
35*********************************************************************************************************************************/
36#define IS_BLANK(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\r' || (ch) == '\n')
37
38
39/*********************************************************************************************************************************
40* Structures and Typedefs *
41*********************************************************************************************************************************/
42/** Pointer to buffered output. */
43typedef struct MYBUFFER __far *PMYBUFFER;
44
45/** Buffered output. */
46typedef struct MYBUFFER
47{
48 PMYBUFFER pNext;
49 USHORT cb;
50 USHORT off;
51 CHAR sz[65536 - sizeof(USHORT) * 2 - sizeof(PMYBUFFER) - 2];
52} MYBUFFER;
53
54
55/*********************************************************************************************************************************
56* Internal Functions *
57*********************************************************************************************************************************/
58void __far VBoxBackdoorPrint(PSZ psz, unsigned cch);
59static PSZ MyGetOptValue(PSZ psz, PSZ pszOption, PSZ *ppszValue);
60
61
62/*********************************************************************************************************************************
63* Global Variables *
64*********************************************************************************************************************************/
65static HFILE g_hStdOut = 1;
66static HFILE g_hStdErr = 2;
67static BOOL g_fOutputToBackdoor = FALSE;
68static USHORT g_cBuffers = 0;
69static PMYBUFFER g_pBufferHead = NULL;
70static PMYBUFFER g_pBufferTail = NULL;
71
72
73
74/** strlen-like function. */
75static unsigned MyStrLen(PSZ psz)
76{
77 unsigned cch = 0;
78 while (psz[cch] != '\0')
79 cch++;
80 return cch;
81}
82
83
84/** strchr-like function. */
85static char __far *MyStrChr(const char __far *psz, char chNeedle)
86{
87 char ch;
88 while ((ch = *psz) != '\0')
89 {
90 if (ch == chNeedle)
91 return (char __far *)psz;
92 psz++;
93 }
94 return NULL;
95}
96
97
98/** memcpy-like function. */
99static void *MyMemCopy(void __far *pvDst, void const __far *pvSrc, USHORT cb)
100{
101 BYTE __far *pbDst = (BYTE __far *)pvDst;
102 BYTE const __far *pbSrc = (BYTE const __far *)pvSrc;
103 while (cb-- > 0)
104 *pbDst++ = *pbSrc++;
105 return pvDst;
106}
107
108
109static void MyOutStr(PSZ psz)
110{
111 unsigned const cch = MyStrLen(psz);
112 USHORT usIgnored;
113 DosWrite(g_hStdErr, psz, cch, &usIgnored);
114 if (g_fOutputToBackdoor)
115 VBoxBackdoorPrint(psz, cch);
116}
117
118
119static PSZ MyNumToString(PSZ pszBuf, unsigned uNum)
120{
121 /* Convert to decimal and inverted digit order: */
122 char szTmp[32];
123 unsigned off = 0;
124 do
125 {
126 szTmp[off++] = uNum % 10 + '0';
127 uNum /= 10;
128 } while (uNum);
129
130 /* Copy it out to the destination buffer in the right order and add a terminator: */
131 while (off-- > 0)
132 *pszBuf++ = szTmp[off];
133 *pszBuf = '\0';
134 return pszBuf;
135}
136
137
138static void MyOutNum(unsigned uNum)
139{
140 char szTmp[32];
141 MyNumToString(szTmp, uNum);
142 MyOutStr(szTmp);
143}
144
145
146static DECL_NO_RETURN(void) MyApiErrorAndQuit(PSZ pszOperation, USHORT rc)
147{
148 MyOutStr("Os2Util: error: ");
149 MyOutStr(pszOperation);
150 MyOutStr(" failed: ");
151 MyOutNum(rc);
152 MyOutStr("\r\n");
153 DosExit(EXIT_PROCESS, 1);
154}
155
156
157static DECL_NO_RETURN(void) MyApiError3AndQuit(PSZ pszOperation, PSZ psz2, PSZ psz3, USHORT rc)
158{
159 MyOutStr("Os2Util: error: ");
160 MyOutStr(pszOperation);
161 MyOutStr(psz2);
162 MyOutStr(psz3);
163 MyOutStr(" failed: ");
164 MyOutNum(rc);
165 MyOutStr("\r\n");
166 DosExit(EXIT_PROCESS, 1);
167}
168
169
170static DECL_NO_RETURN(void) MySyntaxErrorAndQuit(PSZ pszMsg)
171{
172 MyOutStr("Os2Util: syntax error: ");
173 MyOutStr(pszMsg);
174 MyOutStr("\r\n");
175 DosExit(EXIT_PROCESS, 1);
176}
177
178
179static HFILE OpenTeeFile(PSZ pszTeeToFile, BOOL fAppend, PSZ pszToWrite, USHORT cchToWrite)
180{
181 PMYBUFFER pBuf, pNext;
182 USHORT usIgnored;
183 USHORT usAction = 0;
184 HFILE hFile = -1;
185 USHORT rc;
186 rc = DosOpen(pszTeeToFile, &hFile, &usAction, 0 /*cbInitial*/, 0 /*fFileAttribs*/,
187 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
188 OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_SEQUENTIAL, 0 /*Reserved*/);
189 if (rc == NO_ERROR)
190 {
191
192 if (fAppend)
193 {
194 ULONG offNew = 0;
195 DosChgFilePtr(hFile, 0, FILE_END, &offNew);
196 }
197
198 /*
199 * Write out buffered data
200 */
201 /** @todo this does not seem to work. */
202 pBuf = g_pBufferHead;
203 while (pBuf)
204 {
205 do
206 rc = DosWrite(hFile, pBuf->sz, pBuf->off, &usIgnored);
207 while (rc == ERROR_INTERRUPT);
208 pNext = pBuf->pNext;
209 DosFreeSeg((__segment)pBuf);
210 pBuf = pNext;
211 }
212 g_pBufferTail = g_pBufferHead = NULL;
213
214 /*
215 * Write the current output.
216 */
217 do
218 rc = DosWrite(hFile, pszToWrite, cchToWrite, &usIgnored);
219 while (rc == ERROR_INTERRUPT);
220 }
221 else
222 {
223 /*
224 * Failed to open the file. Buffer a bit in case the file can be
225 * opened later (like when we've formatted the disk).
226 */
227 pBuf = g_pBufferTail;
228 if (pBuf && pBuf->off < pBuf->cb)
229 {
230 USHORT cbToCopy = pBuf->cb - pBuf->off;
231 if (cbToCopy > cchToWrite)
232 cbToCopy = cchToWrite;
233 MyMemCopy(&pBuf->sz[pBuf->off], pszToWrite, cbToCopy);
234 pszToWrite += cbToCopy;
235 cchToWrite -= cbToCopy;
236 }
237 if (cchToWrite > 0)
238 {
239 USHORT uSel = 0xffff;
240 if ( g_cBuffers < 10
241 && (rc = DosAllocSeg(0 /*64KiB*/, &uSel, 0 /*fFlags*/)) == NO_ERROR)
242 {
243 pBuf = ((__segment)uSel) :> ((MYBUFFER __near *)0);
244 pBuf->pNext = NULL;
245 pBuf->cb = sizeof(pBuf->sz);
246 pBuf->off = cchToWrite;
247 MyMemCopy(&pBuf->sz[0], pszToWrite, cchToWrite);
248
249 if (g_pBufferTail)
250 g_pBufferTail->pNext = pBuf;
251 else
252 g_pBufferHead = pBuf;
253 g_pBufferTail = pBuf;
254 }
255 else if (g_cBuffers > 0)
256 {
257 pBuf = g_pBufferHead;
258 pBuf->off = cchToWrite;
259 MyMemCopy(&pBuf->sz[0], pszToWrite, cchToWrite);
260
261 if (g_pBufferTail != pBuf)
262 {
263 g_pBufferHead = pBuf->pNext;
264 pBuf->pNext = NULL;
265 g_pBufferTail->pNext = pBuf;
266 g_pBufferTail = pBuf;
267 }
268 }
269 }
270 hFile = -1;
271 }
272 return hFile;
273}
274
275
276/**
277 * Waits for the child progress to complete, returning it's status code.
278 */
279static void DoWait(PID pidChild, USHORT idSession, HQUEUE hQueue, PRESULTCODES pResultCodes)
280{
281 /*
282 * Can we use DosCwait?
283 */
284 if (idSession == 0 && hQueue == NULL)
285 {
286 for (;;)
287 {
288 PID pidIgnored;
289 USHORT rc = DosCwait(DCWA_PROCESS, DCWW_WAIT, pResultCodes, &pidIgnored, pidChild);
290 if (rc == NO_ERROR)
291 break;
292 if (rc != ERROR_INTERRUPT)
293 {
294 MyOutStr("Os2Util: error: DosCwait(DCWA_PROCESS,DCWW_WAIT,,,");
295 MyOutNum(pidChild);
296 MyOutStr(") failed: ");
297 MyOutNum(rc);
298 MyOutStr("\r\n");
299 }
300 }
301 }
302 else
303 {
304 /*
305 * No we have to use the queue interface to the session manager.
306 */
307 for (;;)
308 {
309 ULONG ulAdderPidAndEvent = 0;
310 PUSHORT pausData = NULL;
311 USHORT cbData = 0;
312 BYTE bPriority = 0;
313 HSEM hSem = NULL;
314 USHORT rc = DosReadQueue(hQueue, &ulAdderPidAndEvent, &cbData, (PULONG)&pausData,
315 0 /*uElementCode*/, 0 /* fNoWait */, &bPriority, &hSem);
316 if (rc == NO_ERROR)
317 {
318 if (cbData >= sizeof(USHORT) * 2)
319 {
320 USHORT idTermSession = pausData[0];
321 USHORT uExitCode = pausData[1];
322 if (idTermSession == idSession)
323 {
324 pResultCodes->codeTerminate = 0;
325 pResultCodes->codeResult = uExitCode;
326 break;
327 }
328 if (1)
329 {
330 MyOutStr("OutUtil: info: idTermSession=");
331 MyOutNum(idTermSession);
332 MyOutStr(" uExitCode=");
333 MyOutNum(uExitCode);
334 MyOutStr("\r\n");
335 }
336 }
337 else
338 {
339 MyOutStr("OutUtil: warning: bogus queue element size: cbData=");
340 MyOutNum(cbData);
341 MyOutStr("\r\n");
342 }
343 DosFreeSeg((__segment)pausData);
344 }
345 else if (rc != ERROR_INTERRUPT)
346 {
347 DosCloseQueue(hQueue);
348 MyApiErrorAndQuit("DosReadQueue", rc);
349 }
350 }
351 }
352}
353
354
355/**
356 * Handles --file-to-backdoor / -c.
357 */
358static void CopyFileToBackdoorAndQuit(PSZ psz, BOOL fLongOpt, PSZ pszBuf, USHORT cbBuf)
359{
360 HFILE hFile = 0;
361 USHORT usAction = 0;
362 USHORT rc;
363
364 /*
365 * Get the filename and check that it is the last thing on the commandline.
366 */
367 PSZ pszFilename = NULL;
368 CHAR ch;
369 psz = MyGetOptValue(psz, fLongOpt ? "--file-to-backdoor" : "-c", &pszFilename);
370 while ((ch = *psz) != '\0' && IS_BLANK(ch))
371 psz++;
372 if (ch != '\0')
373 MySyntaxErrorAndQuit("No options allowed after -c/--file-to-backdoor");
374
375 /*
376 * Open the file
377 */
378 rc = DosOpen(pszFilename, &hFile, &usAction, 0 /*cbInitial*/, 0 /*fFileAttribs*/,
379 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
380 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_SEQUENTIAL, 0 /*Reserved*/);
381 if (rc != NO_ERROR)
382 MyApiError3AndQuit("Failed to open \"", pszFilename, "\" for reading", rc);
383
384 VBoxBackdoorPrint(RT_STR_TUPLE("--- BEGIN OF \""));
385 VBoxBackdoorPrint(pszFilename, MyStrLen(pszFilename));
386 VBoxBackdoorPrint(RT_STR_TUPLE("\" ---\n"));
387
388 for (;;)
389 {
390 USHORT cbRead = 0;
391 rc = DosRead(hFile, pszBuf, cbBuf, &cbRead);
392 if (rc == NO_ERROR)
393 {
394 if (cbRead == 0)
395 break;
396 VBoxBackdoorPrint(pszBuf, cbRead);
397 }
398 else if (rc != ERROR_INTERRUPT)
399 MyApiError3AndQuit("Reading \"", pszFilename, "\"", rc);
400 }
401
402 VBoxBackdoorPrint(RT_STR_TUPLE("--- END OF \""));
403 VBoxBackdoorPrint(pszFilename, MyStrLen(pszFilename));
404 VBoxBackdoorPrint(RT_STR_TUPLE("\" ---\n"));
405
406 DosClose(hFile);
407 DosExit(EXIT_PROCESS, 1);
408}
409
410
411/** Displays version string and quits. */
412static DECL_NO_RETURN(void) ShowVersionAndQuit(void)
413{
414 CHAR szVer[] = "$Rev: 93149 $\r\n";
415 USHORT usIgnored;
416 DosWrite(g_hStdOut, szVer, sizeof(szVer) - 1, &usIgnored);
417 DosExit(EXIT_PROCESS, 0);
418}
419
420
421/** Displays usage info and quits. */
422static DECL_NO_RETURN(void) ShowUsageAndQuit(void)
423{
424 static char s_szHelp[] =
425 VBOX_PRODUCT " OS/2 Unattended Helper Version " VBOX_VERSION_STRING "\r\n"
426 "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\r\n"
427 "\r\n"
428 "Os2Util.exe is tiny helper utility that implements TEE'ing to the VBox release\r\n"
429 "log, files and shows the actual exit code of a program. Standard error and\r\n"
430 "output will be merged into one for simplicity reasons.\r\n"
431 "\r\n"
432 "Usage: Os2Util.exe [-a|--append] [-f<filename>|--tee-to-file <filename>] \\\r\n"
433 " [-b|--tee-to-backdoor] [-z<exit>|--as-zero <exit> [..]] \\\r\n"
434 " -- <prog> [args]\r\n"
435 " or Os2Util.exe <-w<msg>|--write-backdoor <msg>>\r\n"
436 " or Os2Util.exe <-c<file>|--file-to-backdoor <file>>\r\n"
437 "\r\n"
438 "Note! Does not supported any kind of quoting before the child arguments.\r\n"
439 ;
440 USHORT usIgnored;
441 DosWrite(g_hStdOut, s_szHelp, sizeof(s_szHelp) - 1, &usIgnored);
442 DosExit(EXIT_PROCESS, 0);
443}
444
445
446/**
447 * Gets the an option value.
448 *
449 * The option value string will be terminated.
450 */
451static PSZ MyGetOptValue(PSZ psz, PSZ pszOption, PSZ *ppszValue)
452{
453 CHAR ch;
454 while ((ch = *psz) != '\0' && IS_BLANK(ch))
455 psz++;
456 if (*psz == '\0')
457 {
458 MyOutStr("Os2Util: syntax error: Option '");
459 MyOutStr(pszOption);
460 MyOutStr("' takes a value\r\n");
461 DosExit(EXIT_PROCESS, 2);
462 }
463
464 *ppszValue = psz;
465
466 while ((ch = *psz) != '\0' && !IS_BLANK(ch))
467 psz++;
468 if (ch != '\0')
469 *psz++ = '\0';
470 return psz;
471}
472
473
474/**
475 * Gets the an numeric option value.
476 */
477static PSZ MyGetOptNum(PSZ psz, PSZ pszOption, PUSHORT puValue)
478{
479 PSZ pszError = NULL;
480 PSZ pszValue = NULL;
481 PSZ const pszRet = MyGetOptValue(psz, pszOption, &pszValue);
482 PSZ const pszValueStart = pszValue;
483 USHORT uValue = 0;
484 CHAR ch;
485 if (pszValue[0] == '0' && ((ch = pszValue[1]) == 'x' || ch == 'X'))
486 {
487 pszValue += 2;
488 while ((ch = *pszValue++) != '\0')
489 {
490 BYTE bDigit;
491 if (ch <= '9' && ch >= '0')
492 bDigit = ch - '0';
493 else if (ch <= 'f' && ch >= 'a')
494 bDigit = ch - 'a' + 10;
495 else if (ch <= 'F' && ch >= 'A')
496 bDigit = ch - 'A' + 10;
497 else
498 {
499 pszError = "': invalid hex value\r\n";
500 break;
501 }
502 if (uValue >> 12)
503 {
504 pszError = "': hex value out of range\r\n";
505 break;
506 }
507 uValue <<= 4;
508 uValue |= bDigit;
509 }
510 }
511 else
512 {
513 while ((ch = *pszValue++) != '\0')
514 {
515 BYTE bDigit;
516 if (ch <= '9' && ch >= '0')
517 bDigit = ch - '0';
518 else
519 {
520 pszError = "': invalid decimal value\r\n";
521 break;
522 }
523 if (uValue * 10 / 10 != uValue)
524 {
525 pszError = "': decimal value out of range\r\n";
526 break;
527 }
528 uValue *= 10;
529 uValue += bDigit;
530 }
531 }
532
533 if (pszError)
534 {
535 MyOutStr("Os2Util: syntax error: Option '");
536 MyOutStr(pszOption);
537 MyOutStr("' with value '");
538 MyOutStr(pszValueStart);
539 MyOutStr(pszError);
540 DosExit(EXIT_PROCESS, 2);
541 }
542
543 *puValue = uValue;
544 return pszRet;
545}
546
547
548/**
549 * Checks if @a pszOption matches @a *ppsz, advance *ppsz if TRUE.
550 */
551static BOOL MyMatchLongOption(PSZ _far *ppsz, PSZ pszOption, unsigned cchOption)
552{
553 /* Match option and command line strings: */
554 PSZ psz = *ppsz;
555 while (cchOption-- > 0)
556 {
557 if (*psz != *pszOption)
558 return FALSE;
559 psz++;
560 pszOption++;
561 }
562
563 /* Is this the end of a word on the command line? */
564 if (*psz == '\0')
565 *ppsz = psz;
566 else if (IS_BLANK(*psz))
567 *ppsz = psz + 1;
568 else
569 return FALSE;
570 return TRUE;
571}
572
573
574/**
575 * The entrypoint (no crt).
576 */
577#pragma aux Os2UtilMain "_*" parm caller [ ax ] [ bx ];
578void Os2UtilMain(USHORT uSelEnv, USHORT offCmdLine)
579{
580 PSZ pszzEnv = ((__segment)uSelEnv) :> ((char _near *)0);
581 PSZ pszzCmdLine = ((__segment)uSelEnv) :> ((char _near *)offCmdLine);
582 USHORT uExitCode = 1;
583 BOOL fTeeToBackdoor = FALSE;
584 BOOL fAppend = FALSE;
585 PSZ pszTeeToFile = NULL;
586 HFILE hTeeToFile = -1;
587 HFILE hPipeRead = -1;
588 PSZ pszzNewCmdLine;
589 PSZ psz;
590 CHAR ch;
591 USHORT usIgnored;
592 USHORT rc;
593 RESULTCODES ResultCodes = { 0xffff, 0xffff };
594 CHAR szBuf[512];
595 CHAR szExeFull[CCHMAXPATH];
596 PSZ pszExe;
597 USHORT uExeType;
598 USHORT idSession = 0;
599 PID pidChild = 0;
600 HQUEUE hQueue = ~(HQUEUE)0;
601 CHAR szQueueName[64];
602 unsigned cAsZero = 0;
603 USHORT auAsZero[16];
604
605 /*
606 * Parse the command line.
607 * Note! We do not accept any kind of quoting.
608 */
609 /* Skip the executable filename: */
610 psz = pszzCmdLine;
611 while (*psz != '\0')
612 psz++;
613 psz++;
614
615 /* Now parse arguments. */
616 while ((ch = *psz) != '\0')
617 {
618 if (IS_BLANK(ch))
619 psz++;
620 else if (ch != '-')
621 break;
622 else
623 {
624 PSZ const pszOptStart = psz;
625 ch = *++psz;
626 if (ch == '-')
627 {
628 ch = *++psz;
629 if (IS_BLANK(ch))
630 {
631 /* Found end-of-arguments marker "--" */
632 psz++;
633 break;
634 }
635 if (ch == 'a' && MyMatchLongOption(&psz, RT_STR_TUPLE("append")))
636 fAppend = TRUE;
637 else if (ch == 'a' && MyMatchLongOption(&psz, RT_STR_TUPLE("as-zero")))
638 {
639 if (cAsZero > RT_ELEMENTS(auAsZero))
640 MySyntaxErrorAndQuit("Too many --as-zero/-z options");
641 psz = MyGetOptNum(psz, "--as-zero", &auAsZero[cAsZero]);
642 cAsZero++;
643 }
644 else if (ch == 'f' && MyMatchLongOption(&psz, RT_STR_TUPLE("file-to-backdoor")))
645 CopyFileToBackdoorAndQuit(psz, TRUE /*fLongOpt*/, szBuf, sizeof(szBuf));
646 else if (ch == 'h' && MyMatchLongOption(&psz, RT_STR_TUPLE("help")))
647 ShowUsageAndQuit();
648 else if (ch == 't' && MyMatchLongOption(&psz, RT_STR_TUPLE("tee-to-backdoor")))
649 g_fOutputToBackdoor = fTeeToBackdoor = TRUE;
650 else if (ch == 't' && MyMatchLongOption(&psz, RT_STR_TUPLE("tee-to-file")))
651 psz = MyGetOptValue(psz, "--tee-to-file", &pszTeeToFile);
652 else if (ch == 'v' && MyMatchLongOption(&psz, RT_STR_TUPLE("version")))
653 ShowVersionAndQuit();
654 else if (ch == 'w' && MyMatchLongOption(&psz, RT_STR_TUPLE("write-backdoor")))
655 {
656 VBoxBackdoorPrint(psz, MyStrLen(psz));
657 VBoxBackdoorPrint("\n", 1);
658 DosExit(EXIT_PROCESS, 0);
659 }
660 else
661 {
662 MyOutStr("Os2util: syntax error: ");
663 MyOutStr(pszOptStart);
664 MyOutStr("\r\n");
665 DosExit(EXIT_PROCESS, 2);
666 }
667 }
668 else
669 {
670 do
671 {
672 if (ch == 'a')
673 fAppend = TRUE;
674 else if (ch == 'b')
675 g_fOutputToBackdoor = fTeeToBackdoor = TRUE;
676 else if (ch == 'c')
677 CopyFileToBackdoorAndQuit(psz + 1, FALSE /*fLongOpt*/, szBuf, sizeof(szBuf));
678 else if (ch == 'f')
679 {
680 psz = MyGetOptValue(psz + 1, "-f", &pszTeeToFile);
681 break;
682 }
683 else if (ch == 'w')
684 {
685 psz++;
686 VBoxBackdoorPrint(psz, MyStrLen(psz));
687 VBoxBackdoorPrint("\n", 1);
688 DosExit(EXIT_PROCESS, 0);
689 }
690 else if (ch == 'z')
691 {
692 if (cAsZero > RT_ELEMENTS(auAsZero))
693 MySyntaxErrorAndQuit("Too many --as-zero/-z options");
694 psz = MyGetOptNum(psz + 1, "-z", &auAsZero[cAsZero]);
695 cAsZero++;
696 }
697 else if (ch == '?' || ch == 'h' || ch == 'H')
698 ShowUsageAndQuit();
699 else if (ch == 'V')
700 ShowVersionAndQuit();
701 else
702 {
703 MyOutStr("Os2util: syntax error: ");
704 if (ch)
705 DosWrite(g_hStdErr, &ch, 1, &usIgnored);
706 else
707 MyOutStr("lone dash");
708 MyOutStr(" (");
709 MyOutStr(pszOptStart);
710 MyOutStr(")\r\n");
711 DosExit(EXIT_PROCESS, 2);
712 }
713 ch = *++psz;
714 } while (!IS_BLANK(ch) && ch != '\0');
715 }
716 }
717 }
718
719 /*
720 * Zero terminate the executable name in the command line.
721 */
722 pszzNewCmdLine = psz;
723 if (ch == '\0')
724 {
725 MyOutStr("Os2Util: syntax error: No program specified\r\n");
726 DosExit(EXIT_PROCESS, 2);
727 }
728 psz++;
729 while ((ch = *psz) != '\0' && !IS_BLANK(ch))
730 psz++;
731 *psz++ = '\0';
732
733 /*
734 * Find the executable and check its type.
735 */
736 if ( pszzNewCmdLine[1] == ':'
737 || MyStrChr(pszzNewCmdLine, '\\')
738 || MyStrChr(pszzNewCmdLine, '/'))
739 pszExe = pszzNewCmdLine;
740 else
741 {
742 rc = DosSearchPath(SEARCH_CUR_DIRECTORY | SEARCH_ENVIRONMENT | SEARCH_IGNORENETERRS, "PATH",
743 pszzNewCmdLine, szExeFull, sizeof(szExeFull));
744 if (rc != NO_ERROR)
745 MyApiError3AndQuit("DosSearchPath(7, \"PATH\", \"", pszzNewCmdLine, "\",,)", rc);
746 pszExe = &szExeFull[0];
747 }
748
749 /* Perhapse we should use WinQueryProgramType here instead? */
750 rc = DosQAppType(pszExe, &uExeType);
751 if (rc != NO_ERROR)
752 MyApiErrorAndQuit("DosQAppType(pszExe, &uExeType)", rc);
753#ifdef DEBUG
754 MyOutStr("Os2Util: debug: uExeType="); MyOutNum(uExeType); MyOutStr("\r\n");
755#endif
756 /** @todo deal with launching winos2 programs too... */
757
758 /*
759 * Prepare redirection.
760 */
761 if (fTeeToBackdoor || pszTeeToFile != NULL)
762 {
763 HFILE hPipeWrite = -1;
764 HFILE hDup;
765
766 /* Make new copies of the standard handles. */
767 hDup = 0xffff;
768 rc = DosDupHandle(g_hStdErr, &hDup);
769 if (rc != NO_ERROR)
770 MyApiErrorAndQuit("DosDupHandle(g_hStdErr, &hDup)", rc);
771 g_hStdErr = hDup;
772 DosSetFHandState(hDup, OPEN_FLAGS_NOINHERIT); /* not strictly necessary, so ignore errors */
773
774 hDup = 0xffff;
775 rc = DosDupHandle(g_hStdOut, &hDup);
776 if (rc != NO_ERROR)
777 MyApiErrorAndQuit("DosDupHandle(g_hStdOut, &hDup)", rc);
778 g_hStdOut = hDup;
779 DosSetFHandState(hDup, OPEN_FLAGS_NOINHERIT); /* not strictly necessary, so ignore errors */
780
781 /* Create the pipe and make the read-end non-inheritable (we'll hang otherwise). */
782 rc = DosMakePipe(&hPipeRead, &hPipeWrite, 0 /*default size*/);
783 if (rc != NO_ERROR)
784 MyApiErrorAndQuit("DosMakePipe", rc);
785
786 rc = DosSetFHandState(hPipeRead, OPEN_FLAGS_NOINHERIT);
787 if (rc != NO_ERROR)
788 MyApiErrorAndQuit("DosSetFHandState(hPipeRead, OPEN_FLAGS_NOINHERIT)", rc);
789
790 /* Replace standard output and standard error with the write end of the pipe. */
791 hDup = 1;
792 rc = DosDupHandle(hPipeWrite, &hDup);
793 if (rc != NO_ERROR)
794 MyApiErrorAndQuit("DosDupHandle(hPipeWrite, &hDup[=1])", rc);
795
796 hDup = 2;
797 rc = DosDupHandle(hPipeWrite, &hDup);
798 if (rc != NO_ERROR)
799 MyApiErrorAndQuit("DosDupHandle(hPipeWrite, &hDup[=2])", rc);
800
801 /* We can close the write end of the pipe as we don't need the original handle any more. */
802 DosClose(hPipeWrite);
803 }
804
805 /*
806 * Execute the program.
807 */
808 szBuf[0] = '\0';
809#define FAPPTYP_TYPE_MASK 7
810 if ((uExeType & FAPPTYP_TYPE_MASK) == PT_WINDOWABLEVIO) /** @todo what if we're in fullscreen ourselves? */
811 {
812 /* For same type programs we can use DosExecPgm: */
813 rc = DosExecPgm(szBuf, sizeof(szBuf), hPipeRead == -1 ? EXEC_SYNC : EXEC_ASYNCRESULT,
814 pszzNewCmdLine, pszzEnv, &ResultCodes, pszExe);
815 if (rc != NO_ERROR)
816 {
817 MyOutStr("Os2Util: error: DosExecPgm failed for \"");
818 MyOutStr(pszzNewCmdLine);
819 MyOutStr("\": ");
820 MyOutNum(rc);
821 if (szBuf[0])
822 {
823 MyOutStr(" ErrObj=");
824 szBuf[sizeof(szBuf) - 1] = '\0';
825 MyOutStr(szBuf);
826 }
827 MyOutStr("\r\n");
828 DosExit(EXIT_PROCESS, 1);
829 }
830 if (hPipeRead != -1)
831 {
832 pidChild = ResultCodes.codeTerminate;
833 MyOutStr("info: started pid ");
834 MyOutNum(pidChild);
835 MyOutStr("\r\n");
836 }
837 }
838 else
839 {
840 /* For different typed programs we have to use DosStartSession, which
841 is a lot more tedious to use. */
842 static const char s_szQueueBase[] = "\\QUEUES\\OS2_UTIL-";
843 union
844 {
845 STARTDATA StartData;
846 BYTE abPadding[sizeof(STARTDATA) + 64];
847 struct
848 {
849 STARTDATA Core;
850 ULONG ulReserved;
851 PSZ pszBuf;
852 USHORT cbBuf;
853 } s;
854 } u;
855 PIDINFO PidInfo = {0, 0, 0};
856
857 /* Create the wait queue first. */
858 DosGetPID(&PidInfo);
859 MyMemCopy(szQueueName, s_szQueueBase, sizeof(s_szQueueBase));
860 MyNumToString(&szQueueName[sizeof(s_szQueueBase) - 1], PidInfo.pid);
861
862 rc = DosCreateQueue(&hQueue, 0 /*FIFO*/, szQueueName);
863 if (rc != NO_ERROR)
864 MyApiError3AndQuit("DosCreateQueue(&hQueue, 0, \"", szQueueName, "\")", rc);
865
866 u.StartData.Length = sizeof(u.StartData);
867 u.StartData.Related = 1 /* SSF_RELATED_CHILD */;
868 u.StartData.FgBg = (uExeType & FAPPTYP_TYPE_MASK) == PT_PM
869 ? 1 /* SSF_FGBG_BACK - try avoid ERROR_SMG_START_IN_BACKGROUND */
870 : 0 /* SSF_FGBG_FORE */;
871 u.StartData.TraceOpt = 0 /* SSF_TRACEOPT_NONE */;
872 u.StartData.PgmTitle = NULL;
873 u.StartData.PgmName = pszExe;
874 u.StartData.PgmInputs = psz; /* just arguments, not exec apparently.*/
875 u.StartData.TermQ = szQueueName;
876 u.StartData.Environment = NULL; /* Inherit our env. Note! Using pszzEnv causes it to be freed
877 and we'll crash reporting the error. */
878 u.StartData.InheritOpt = 1 /* SSF_INHERTOPT_PARENT */;
879 u.StartData.SessionType = uExeType & FAPPTYP_TYPE_MASK;
880 if (uExeType & 0x20 /*FAPPTYP_DOS*/)
881 u.StartData.SessionType = 4 /* SSF_TYPE_VDM */;
882 u.StartData.IconFile = NULL;
883 u.StartData.PgmHandle = 0;
884 u.StartData.PgmControl = 0 /* SSF_CONTROL_VISIBLE */;
885 u.StartData.InitXPos = 0;
886 u.StartData.InitYPos = 0;
887 u.StartData.InitXSize = 0;
888 u.StartData.InitYSize = 0;
889 u.s.ulReserved = 0;
890 u.s.pszBuf = NULL;
891 u.s.cbBuf = 0;
892
893 rc = DosStartSession(&u.StartData, &idSession, &pidChild);
894 if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND)
895 {
896 DosCloseQueue(hQueue);
897 MyApiError3AndQuit("DosStartSession for \"", pszExe, "\"", rc);
898 }
899
900 if (1)
901 {
902 MyOutStr("info: started session ");
903 MyOutNum(idSession);
904 MyOutStr(", pid ");
905 MyOutNum(pidChild);
906 MyOutStr("\r\n");
907 }
908 }
909
910 /*
911 * Wait for the child process to complete.
912 */
913 if (hPipeRead != -1)
914 {
915
916 /* Close the write handles or we'll hang in the read loop. */
917 DosClose(1);
918 DosClose(2);
919
920 /* Disable hard error popups (file output to unformatted disks). */
921 DosError(2 /* only exceptions */);
922
923 /*
924 * Read the pipe and tee it to the desired outputs
925 */
926 for (;;)
927 {
928 USHORT cbRead = 0;
929 rc = DosRead(hPipeRead, szBuf, sizeof(szBuf), &cbRead);
930 if (rc == NO_ERROR)
931 {
932 if (cbRead == 0)
933 break; /* No more writers. */
934
935 /* Standard output: */
936 do
937 rc = DosWrite(g_hStdOut, szBuf, cbRead, &usIgnored);
938 while (rc == ERROR_INTERRUPT);
939
940 /* Backdoor: */
941 if (fTeeToBackdoor)
942 VBoxBackdoorPrint(szBuf, cbRead);
943
944 /* File: */
945 if (hTeeToFile != -1)
946 do
947 rc = DosWrite(hTeeToFile, szBuf, cbRead, &usIgnored);
948 while (rc == ERROR_INTERRUPT);
949 else if (pszTeeToFile != NULL)
950 hTeeToFile = OpenTeeFile(pszTeeToFile, fAppend, szBuf, cbRead);
951 }
952 else if (rc == ERROR_BROKEN_PIPE)
953 break;
954 else
955 {
956 MyOutStr("Os2Util: error: Error reading pipe: ");
957 MyOutNum(rc);
958 MyOutStr("\r\n");
959 break;
960 }
961 }
962
963 DosClose(hPipeRead);
964
965 /*
966 * Wait for the process to complete.
967 */
968 DoWait(pidChild, idSession, hQueue, &ResultCodes);
969 }
970 /*
971 * Must wait for the session completion too.
972 */
973 else if (idSession != 0)
974 DoWait(pidChild, idSession, hQueue, &ResultCodes);
975
976 /*
977 * Report the status code and quit.
978 */
979 MyOutStr("Os2Util: Child: ");
980 MyOutStr(pszzNewCmdLine);
981 MyOutStr(" ");
982 MyOutStr(psz);
983 MyOutStr("\r\n"
984 "Os2Util: codeTerminate=");
985 MyOutNum(ResultCodes.codeTerminate);
986 MyOutStr(" codeResult=");
987 MyOutNum(ResultCodes.codeResult);
988 MyOutStr("\r\n");
989
990 /* Treat it as zero? */
991 if (ResultCodes.codeTerminate == 0)
992 {
993 unsigned i = cAsZero;
994 while (i-- > 0)
995 if (auAsZero[i] == ResultCodes.codeResult)
996 {
997 MyOutStr("Os2Util: info: treating status as zero\r\n");
998 ResultCodes.codeResult = 0;
999 break;
1000 }
1001 }
1002
1003 if (idSession != 0)
1004 DosCloseQueue(hQueue);
1005 for (;;)
1006 DosExit(EXIT_PROCESS, ResultCodes.codeTerminate == 0 ? ResultCodes.codeResult : 127);
1007}
1008
1009
1010/**
1011 * Backdoor print function living in an IOPL=2 segment.
1012 */
1013#pragma code_seg("IOPL", "CODE")
1014void __far VBoxBackdoorPrint(PSZ psz, unsigned cch)
1015{
1016 ASMOutStrU8(RTLOG_DEBUG_PORT, psz, cch);
1017}
1018
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette