VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/src/misc/prinit.c@ 101869

Last change on this file since 101869 was 101869, checked in by vboxsync, 17 months ago

libs/xpcom: Remove code for unused platforms from nsprpub, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "primpl.h"
39#include <ctype.h>
40#include <string.h>
41#ifdef VBOX_USE_IPRT_IN_NSPR
42# include <iprt/initterm.h>
43#endif
44
45#ifdef VBOX
46#include <iprt/err.h>
47#include <iprt/env.h>
48#include <iprt/file.h>
49#include <iprt/process.h>
50#endif
51
52PRLogModuleInfo *_pr_clock_lm;
53PRLogModuleInfo *_pr_cmon_lm;
54PRLogModuleInfo *_pr_io_lm;
55PRLogModuleInfo *_pr_cvar_lm;
56PRLogModuleInfo *_pr_mon_lm;
57PRLogModuleInfo *_pr_linker_lm;
58PRLogModuleInfo *_pr_sched_lm;
59PRLogModuleInfo *_pr_thread_lm;
60PRLogModuleInfo *_pr_gc_lm;
61PRLogModuleInfo *_pr_shm_lm;
62PRLogModuleInfo *_pr_shma_lm;
63
64PRFileDesc *_pr_stdin;
65PRFileDesc *_pr_stdout;
66PRFileDesc *_pr_stderr;
67
68PRLock *_pr_sleeplock; /* used in PR_Sleep(), classic and pthreads */
69
70static void _PR_InitCallOnce(void);
71
72PRBool _pr_initialized = PR_FALSE;
73
74
75PR_IMPLEMENT(PRBool) PR_VersionCheck(const char *importedVersion)
76{
77 /*
78 ** This is the secret handshake algorithm.
79 **
80 ** This release has a simple version compatibility
81 ** check algorithm. This release is not backward
82 ** compatible with previous major releases. It is
83 ** not compatible with future major, minor, or
84 ** patch releases.
85 */
86 int vmajor = 0, vminor = 0, vpatch = 0;
87 const char *ptr = importedVersion;
88
89 while (isdigit(*ptr)) {
90 vmajor = 10 * vmajor + *ptr - '0';
91 ptr++;
92 }
93 if (*ptr == '.') {
94 ptr++;
95 while (isdigit(*ptr)) {
96 vminor = 10 * vminor + *ptr - '0';
97 ptr++;
98 }
99 if (*ptr == '.') {
100 ptr++;
101 while (isdigit(*ptr)) {
102 vpatch = 10 * vpatch + *ptr - '0';
103 ptr++;
104 }
105 }
106 }
107
108 if (vmajor != PR_VMAJOR) {
109 return PR_FALSE;
110 }
111 if (vmajor == PR_VMAJOR && vminor > PR_VMINOR) {
112 return PR_FALSE;
113 }
114 if (vmajor == PR_VMAJOR && vminor == PR_VMINOR && vpatch > PR_VPATCH) {
115 return PR_FALSE;
116 }
117 return PR_TRUE;
118} /* PR_VersionCheck */
119
120
121PR_IMPLEMENT(PRBool) PR_Initialized(void)
122{
123 return _pr_initialized;
124}
125
126PRInt32 _native_threads_only = 0;
127
128static void _PR_InitStuff(void)
129{
130
131 if (_pr_initialized) return;
132 _pr_initialized = PR_TRUE;
133#ifdef VBOX_USE_IPRT_IN_NSPR
134 RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
135#endif
136
137 (void) PR_GetPageSize();
138
139 _pr_clock_lm = PR_NewLogModule("clock");
140 _pr_cmon_lm = PR_NewLogModule("cmon");
141 _pr_io_lm = PR_NewLogModule("io");
142 _pr_mon_lm = PR_NewLogModule("mon");
143 _pr_linker_lm = PR_NewLogModule("linker");
144 _pr_cvar_lm = PR_NewLogModule("cvar");
145 _pr_sched_lm = PR_NewLogModule("sched");
146 _pr_thread_lm = PR_NewLogModule("thread");
147 _pr_gc_lm = PR_NewLogModule("gc");
148 _pr_shm_lm = PR_NewLogModule("shm");
149 _pr_shma_lm = PR_NewLogModule("shma");
150
151 /* NOTE: These init's cannot depend on _PR_MD_CURRENT_THREAD() */
152 _PR_MD_EARLY_INIT();
153
154 _PR_InitLocks();
155 _PR_InitAtomic();
156 _PR_InitTPD();
157 _PR_InitEnv();
158 _PR_InitLayerCache();
159 _PR_InitClock();
160
161 _pr_sleeplock = PR_NewLock();
162 PR_ASSERT(NULL != _pr_sleeplock);
163
164 _PR_InitThreads(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
165
166 _PR_InitCMon();
167 _PR_InitIO();
168 _PR_InitLog();
169 _PR_InitLinker();
170 _PR_InitCallOnce();
171 _PR_InitDtoa();
172
173 nspr_InitializePRErrorTable();
174
175 _PR_MD_FINAL_INIT();
176}
177
178void _PR_ImplicitInitialization(void)
179{
180 _PR_InitStuff();
181}
182
183PR_IMPLEMENT(void) PR_DisableClockInterrupts(void)
184{
185}
186
187PR_IMPLEMENT(void) PR_EnableClockInterrupts(void)
188{
189}
190
191PR_IMPLEMENT(void) PR_BlockClockInterrupts(void)
192{
193}
194
195PR_IMPLEMENT(void) PR_UnblockClockInterrupts(void)
196{
197}
198
199PR_IMPLEMENT(void) PR_Init(
200 PRThreadType type, PRThreadPriority priority, PRUintn maxPTDs)
201{
202 _PR_ImplicitInitialization();
203}
204
205PR_IMPLEMENT(PRIntn) PR_Initialize(
206 PRPrimordialFn prmain, PRIntn argc, char **argv, PRUintn maxPTDs)
207{
208 PRIntn rv;
209 _PR_ImplicitInitialization();
210 rv = prmain(argc, argv);
211 PR_Cleanup();
212 return rv;
213} /* PR_Initialize */
214
215PR_IMPLEMENT(PRProcessAttr *)
216PR_NewProcessAttr(void)
217{
218 PRProcessAttr *attr;
219
220 attr = PR_NEWZAP(PRProcessAttr);
221 if (!attr) {
222 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
223 }
224 return attr;
225}
226
227PR_IMPLEMENT(void)
228PR_ResetProcessAttr(PRProcessAttr *attr)
229{
230 PR_FREEIF(attr->currentDirectory);
231 PR_FREEIF(attr->fdInheritBuffer);
232 memset(attr, 0, sizeof(*attr));
233}
234
235PR_IMPLEMENT(void)
236PR_DestroyProcessAttr(PRProcessAttr *attr)
237{
238 PR_FREEIF(attr->currentDirectory);
239 PR_FREEIF(attr->fdInheritBuffer);
240 PR_DELETE(attr);
241}
242
243PR_IMPLEMENT(void)
244PR_ProcessAttrSetStdioRedirect(
245 PRProcessAttr *attr,
246 PRSpecialFD stdioFd,
247 PRFileDesc *redirectFd)
248{
249 switch (stdioFd) {
250 case PR_StandardInput:
251 attr->stdinFd = redirectFd;
252 break;
253 case PR_StandardOutput:
254 attr->stdoutFd = redirectFd;
255 break;
256 case PR_StandardError:
257 attr->stderrFd = redirectFd;
258 break;
259 default:
260 PR_ASSERT(0);
261 }
262}
263
264PR_IMPLEMENT(PRStatus)
265PR_ProcessAttrSetCurrentDirectory(
266 PRProcessAttr *attr,
267 const char *dir)
268{
269 PR_FREEIF(attr->currentDirectory);
270 attr->currentDirectory = (char *) PR_MALLOC(strlen(dir) + 1);
271 if (!attr->currentDirectory) {
272 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
273 return PR_FAILURE;
274 }
275 strcpy(attr->currentDirectory, dir);
276 return PR_SUCCESS;
277}
278
279PR_IMPLEMENT(PRStatus)
280PR_ProcessAttrSetInheritableFD(
281 PRProcessAttr *attr,
282 PRFileDesc *fd,
283 const char *name)
284{
285 /* We malloc the fd inherit buffer in multiples of this number. */
286#define FD_INHERIT_BUFFER_INCR 128
287 /* The length of "NSPR_INHERIT_FDS=" */
288#define NSPR_INHERIT_FDS_STRLEN 17
289 /* The length of osfd (PRInt32) printed in hexadecimal with 0x prefix */
290#define OSFD_STRLEN 10
291 /* The length of fd type (PRDescType) printed in decimal */
292#define FD_TYPE_STRLEN 1
293 PRSize newSize;
294 int remainder;
295 char *newBuffer;
296 int nwritten;
297 char *cur;
298 int freeSize;
299
300 if (fd->identity != PR_NSPR_IO_LAYER) {
301 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
302 return PR_FAILURE;
303 }
304 if (fd->secret->inheritable == _PR_TRI_UNKNOWN) {
305 _PR_MD_QUERY_FD_INHERITABLE(fd);
306 }
307 if (fd->secret->inheritable != _PR_TRI_TRUE) {
308 PR_SetError(PR_NO_ACCESS_RIGHTS_ERROR, 0);
309 return PR_FAILURE;
310 }
311
312 /*
313 * We also need to account for the : separators and the
314 * terminating null byte.
315 */
316 if (NULL == attr->fdInheritBuffer) {
317 /* The first time, we print "NSPR_INHERIT_FDS=<name>:<type>:<val>" */
318 newSize = NSPR_INHERIT_FDS_STRLEN + strlen(name)
319 + FD_TYPE_STRLEN + OSFD_STRLEN + 2 + 1;
320 } else {
321 /* At other times, we print ":<name>:<type>:<val>" */
322 newSize = attr->fdInheritBufferUsed + strlen(name)
323 + FD_TYPE_STRLEN + OSFD_STRLEN + 3 + 1;
324 }
325 if (newSize > attr->fdInheritBufferSize) {
326 /* Make newSize a multiple of FD_INHERIT_BUFFER_INCR */
327 remainder = newSize % FD_INHERIT_BUFFER_INCR;
328 if (remainder != 0) {
329 newSize += (FD_INHERIT_BUFFER_INCR - remainder);
330 }
331 if (NULL == attr->fdInheritBuffer) {
332 newBuffer = (char *) PR_MALLOC(newSize);
333 } else {
334 newBuffer = (char *) PR_REALLOC(attr->fdInheritBuffer, newSize);
335 }
336 if (NULL == newBuffer) {
337 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
338 return PR_FAILURE;
339 }
340 attr->fdInheritBuffer = newBuffer;
341 attr->fdInheritBufferSize = newSize;
342 }
343 cur = attr->fdInheritBuffer + attr->fdInheritBufferUsed;
344 freeSize = attr->fdInheritBufferSize - attr->fdInheritBufferUsed;
345 if (0 == attr->fdInheritBufferUsed) {
346 nwritten = PR_snprintf(cur, freeSize,
347 "NSPR_INHERIT_FDS=%s:%d:0x%lx",
348 name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd);
349 } else {
350 nwritten = PR_snprintf(cur, freeSize, ":%s:%d:0x%lx",
351 name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd);
352 }
353 attr->fdInheritBufferUsed += nwritten;
354 return PR_SUCCESS;
355}
356
357PR_IMPLEMENT(PRFileDesc *) PR_GetInheritedFD(
358 const char *name)
359{
360 PRFileDesc *fd;
361 const char *envVar;
362 const char *ptr;
363 int len = strlen(name);
364 PRInt32 osfd;
365 int nColons;
366 PRIntn fileType;
367
368 envVar = PR_GetEnv("NSPR_INHERIT_FDS");
369 if (NULL == envVar || '\0' == envVar[0]) {
370 PR_SetError(PR_UNKNOWN_ERROR, 0);
371 return NULL;
372 }
373
374 ptr = envVar;
375 while (1) {
376 if ((strncmp(ptr, name, len) == 0) && (ptr[len] == ':')) {
377 ptr += len + 1;
378 PR_sscanf(ptr, "%d:0x%lx", &fileType, &osfd);
379 switch ((PRDescType)fileType) {
380 case PR_DESC_FILE:
381 fd = PR_ImportFile(osfd);
382 break;
383 case PR_DESC_PIPE:
384 fd = PR_ImportPipe(osfd);
385 break;
386 case PR_DESC_SOCKET_TCP:
387 fd = PR_ImportTCPSocket(osfd);
388 break;
389 case PR_DESC_SOCKET_UDP:
390 fd = PR_ImportUDPSocket(osfd);
391 break;
392 default:
393 PR_ASSERT(0);
394 PR_SetError(PR_UNKNOWN_ERROR, 0);
395 fd = NULL;
396 break;
397 }
398 if (fd) {
399 /*
400 * An inherited FD is inheritable by default.
401 * The child process needs to call PR_SetFDInheritable
402 * to make it non-inheritable if so desired.
403 */
404 fd->secret->inheritable = _PR_TRI_TRUE;
405 }
406 return fd;
407 }
408 /* Skip three colons */
409 nColons = 0;
410 while (*ptr) {
411 if (*ptr == ':') {
412 if (++nColons == 3) {
413 break;
414 }
415 }
416 ptr++;
417 }
418 if (*ptr == '\0') {
419 PR_SetError(PR_UNKNOWN_ERROR, 0);
420 return NULL;
421 }
422 ptr++;
423 }
424}
425
426PR_IMPLEMENT(PRStatus) PR_CreateProcessDetached(
427 const char *path,
428 char *const *argv,
429 char *const *envp,
430 const PRProcessAttr *attr)
431{
432 int vrc;
433 int nEnv, idx;
434 RTENV childEnv;
435 RTENV newEnv = RTENV_DEFAULT;
436
437 /* this code doesn't support all attributes */
438 PR_ASSERT(!attr || !attr->currentDirectory);
439 /* no custom environment, please */
440 PR_ASSERT(!envp);
441
442 childEnv = RTENV_DEFAULT;
443 if (attr && attr->fdInheritBuffer) {
444 vrc = RTEnvClone(&newEnv, childEnv);
445 if (RT_FAILURE(vrc))
446 return PR_FAILURE;
447 vrc = RTEnvPutEx(newEnv, attr->fdInheritBuffer);
448 if (RT_FAILURE(vrc))
449 {
450 RTEnvDestroy(newEnv);
451 return PR_FAILURE;
452 }
453 childEnv = newEnv;
454 }
455
456 PRTHANDLE pStdIn = NULL, pStdOut = NULL, pStdErr = NULL;
457 RTHANDLE hStdIn, hStdOut, hStdErr;
458 if (attr && attr->stdinFd)
459 {
460 hStdIn.enmType = RTHANDLETYPE_FILE;
461 RTFileFromNative(&hStdIn.u.hFile, attr->stdinFd->secret->md.osfd);
462 pStdIn = &hStdIn;
463 }
464 if (attr && attr->stdoutFd)
465 {
466 hStdOut.enmType = RTHANDLETYPE_FILE;
467 RTFileFromNative(&hStdOut.u.hFile, attr->stdoutFd->secret->md.osfd);
468 pStdOut = &hStdOut;
469 }
470 if (attr && attr->stderrFd)
471 {
472 hStdErr.enmType = RTHANDLETYPE_FILE;
473 RTFileFromNative(&hStdErr.u.hFile, attr->stderrFd->secret->md.osfd);
474 pStdErr = &hStdErr;
475 }
476
477 vrc = RTProcCreateEx(path, (const char **)argv, childEnv,
478 RTPROC_FLAGS_DETACHED, pStdIn, pStdOut, pStdErr,
479 NULL /* pszAsUser */, NULL /* pszPassword */, NULL /* pExtraData */,
480 NULL /* phProcess */);
481 if (newEnv != RTENV_DEFAULT) {
482 RTEnvDestroy(newEnv);
483 }
484 if (RT_SUCCESS(vrc))
485 return PR_SUCCESS;
486 else
487 return PR_FAILURE;
488}
489
490/*
491 ********************************************************************
492 *
493 * Module initialization
494 *
495 ********************************************************************
496 */
497
498static struct {
499 PRLock *ml;
500 PRCondVar *cv;
501} mod_init;
502
503static void _PR_InitCallOnce(void) {
504 mod_init.ml = PR_NewLock();
505 PR_ASSERT(NULL != mod_init.ml);
506 mod_init.cv = PR_NewCondVar(mod_init.ml);
507 PR_ASSERT(NULL != mod_init.cv);
508}
509
510void _PR_CleanupCallOnce()
511{
512 PR_DestroyLock(mod_init.ml);
513 mod_init.ml = NULL;
514 PR_DestroyCondVar(mod_init.cv);
515 mod_init.cv = NULL;
516}
517
518PR_IMPLEMENT(PRStatus) PR_CallOnce(
519 PRCallOnceType *once,
520 PRCallOnceFN func)
521{
522 if (!_pr_initialized) _PR_ImplicitInitialization();
523
524 if (!once->initialized) {
525 if (PR_AtomicSet(&once->inProgress, 1) == 0) {
526 once->status = (*func)();
527 PR_Lock(mod_init.ml);
528 once->initialized = 1;
529 PR_NotifyAllCondVar(mod_init.cv);
530 PR_Unlock(mod_init.ml);
531 } else {
532 PR_Lock(mod_init.ml);
533 while (!once->initialized) {
534 PR_WaitCondVar(mod_init.cv, PR_INTERVAL_NO_TIMEOUT);
535 }
536 PR_Unlock(mod_init.ml);
537 }
538 }
539 return once->status;
540}
541
542PR_IMPLEMENT(PRStatus) PR_CallOnceWithArg(
543 PRCallOnceType *once,
544 PRCallOnceWithArgFN func,
545 void *arg)
546{
547 if (!_pr_initialized) _PR_ImplicitInitialization();
548
549 if (!once->initialized) {
550 if (PR_AtomicSet(&once->inProgress, 1) == 0) {
551 once->status = (*func)(arg);
552 PR_Lock(mod_init.ml);
553 once->initialized = 1;
554 PR_NotifyAllCondVar(mod_init.cv);
555 PR_Unlock(mod_init.ml);
556 } else {
557 PR_Lock(mod_init.ml);
558 while (!once->initialized) {
559 PR_WaitCondVar(mod_init.cv, PR_INTERVAL_NO_TIMEOUT);
560 }
561 PR_Unlock(mod_init.ml);
562 }
563 }
564 return once->status;
565}
566
567PRBool _PR_Obsolete(const char *obsolete, const char *preferred)
568{
569#if defined(DEBUG)
570 PR_fprintf(
571 PR_STDERR, "'%s' is obsolete. Use '%s' instead.\n",
572 obsolete, (NULL == preferred) ? "something else" : preferred);
573#endif
574 return PR_FALSE;
575} /* _PR_Obsolete */
576
577/* prinit.c */
578
579
Note: See TracBrowser for help on using the repository browser.

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