Changeset 102049 in vbox for trunk/src/libs/xpcom18a4/nsprpub
- Timestamp:
- Nov 9, 2023 7:48:01 PM (15 months ago)
- Location:
- trunk/src/libs/xpcom18a4/nsprpub/pr
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/xpcom18a4/nsprpub/pr/include/nspr.h
r102007 r102049 52 52 #include "prmon.h" 53 53 #include "prprf.h" 54 #include "prproces.h"55 54 #include "prthread.h" 56 55 #include "prtime.h" -
trunk/src/libs/xpcom18a4/nsprpub/pr/src/misc/prinit.c
r102007 r102049 110 110 } /* PR_Initialize */ 111 111 112 PR_IMPLEMENT(PRProcessAttr *)113 PR_NewProcessAttr(void)114 {115 PRProcessAttr *attr;116 117 attr = PR_NEWZAP(PRProcessAttr);118 if (!attr) {119 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);120 }121 return attr;122 }123 124 PR_IMPLEMENT(void)125 PR_ResetProcessAttr(PRProcessAttr *attr)126 {127 PR_FREEIF(attr->currentDirectory);128 PR_FREEIF(attr->fdInheritBuffer);129 memset(attr, 0, sizeof(*attr));130 }131 132 PR_IMPLEMENT(void)133 PR_DestroyProcessAttr(PRProcessAttr *attr)134 {135 PR_FREEIF(attr->currentDirectory);136 PR_FREEIF(attr->fdInheritBuffer);137 PR_DELETE(attr);138 }139 140 PR_IMPLEMENT(void)141 PR_ProcessAttrSetStdioRedirect(142 PRProcessAttr *attr,143 PRSpecialFD stdioFd,144 PRFileDesc *redirectFd)145 {146 switch (stdioFd) {147 case PR_StandardInput:148 attr->stdinFd = redirectFd;149 break;150 case PR_StandardOutput:151 attr->stdoutFd = redirectFd;152 break;153 case PR_StandardError:154 attr->stderrFd = redirectFd;155 break;156 default:157 AssertFailed();158 }159 }160 161 PR_IMPLEMENT(PRStatus)162 PR_ProcessAttrSetCurrentDirectory(163 PRProcessAttr *attr,164 const char *dir)165 {166 PR_FREEIF(attr->currentDirectory);167 attr->currentDirectory = (char *) PR_MALLOC(strlen(dir) + 1);168 if (!attr->currentDirectory) {169 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);170 return PR_FAILURE;171 }172 strcpy(attr->currentDirectory, dir);173 return PR_SUCCESS;174 }175 176 PR_IMPLEMENT(PRStatus)177 PR_ProcessAttrSetInheritableFD(178 PRProcessAttr *attr,179 PRFileDesc *fd,180 const char *name)181 {182 /* We malloc the fd inherit buffer in multiples of this number. */183 #define FD_INHERIT_BUFFER_INCR 128184 /* The length of "NSPR_INHERIT_FDS=" */185 #define NSPR_INHERIT_FDS_STRLEN 17186 /* The length of osfd (PRInt32) printed in hexadecimal with 0x prefix */187 #define OSFD_STRLEN 10188 /* The length of fd type (PRDescType) printed in decimal */189 #define FD_TYPE_STRLEN 1190 PRSize newSize;191 int remainder;192 char *newBuffer;193 int nwritten;194 char *cur;195 int freeSize;196 197 if (fd->identity != PR_NSPR_IO_LAYER) {198 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);199 return PR_FAILURE;200 }201 if (fd->secret->inheritable == _PR_TRI_UNKNOWN) {202 _PR_MD_QUERY_FD_INHERITABLE(fd);203 }204 if (fd->secret->inheritable != _PR_TRI_TRUE) {205 PR_SetError(PR_NO_ACCESS_RIGHTS_ERROR, 0);206 return PR_FAILURE;207 }208 209 /*210 * We also need to account for the : separators and the211 * terminating null byte.212 */213 if (NULL == attr->fdInheritBuffer) {214 /* The first time, we print "NSPR_INHERIT_FDS=<name>:<type>:<val>" */215 newSize = NSPR_INHERIT_FDS_STRLEN + strlen(name)216 + FD_TYPE_STRLEN + OSFD_STRLEN + 2 + 1;217 } else {218 /* At other times, we print ":<name>:<type>:<val>" */219 newSize = attr->fdInheritBufferUsed + strlen(name)220 + FD_TYPE_STRLEN + OSFD_STRLEN + 3 + 1;221 }222 if (newSize > attr->fdInheritBufferSize) {223 /* Make newSize a multiple of FD_INHERIT_BUFFER_INCR */224 remainder = newSize % FD_INHERIT_BUFFER_INCR;225 if (remainder != 0) {226 newSize += (FD_INHERIT_BUFFER_INCR - remainder);227 }228 if (NULL == attr->fdInheritBuffer) {229 newBuffer = (char *) PR_MALLOC(newSize);230 } else {231 newBuffer = (char *) PR_REALLOC(attr->fdInheritBuffer, newSize);232 }233 if (NULL == newBuffer) {234 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);235 return PR_FAILURE;236 }237 attr->fdInheritBuffer = newBuffer;238 attr->fdInheritBufferSize = newSize;239 }240 cur = attr->fdInheritBuffer + attr->fdInheritBufferUsed;241 freeSize = attr->fdInheritBufferSize - attr->fdInheritBufferUsed;242 if (0 == attr->fdInheritBufferUsed) {243 nwritten = PR_snprintf(cur, freeSize,244 "NSPR_INHERIT_FDS=%s:%d:0x%lx",245 name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd);246 } else {247 nwritten = PR_snprintf(cur, freeSize, ":%s:%d:0x%lx",248 name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd);249 }250 attr->fdInheritBufferUsed += nwritten;251 return PR_SUCCESS;252 }253 254 PR_IMPLEMENT(PRFileDesc *) PR_GetInheritedFD(255 const char *name)256 {257 PRFileDesc *fd;258 const char *envVar;259 const char *ptr;260 int len = strlen(name);261 PRInt32 osfd;262 int nColons;263 PRIntn fileType;264 265 envVar = RTEnvGet("NSPR_INHERIT_FDS");266 if (NULL == envVar || '\0' == envVar[0]) {267 PR_SetError(PR_UNKNOWN_ERROR, 0);268 return NULL;269 }270 271 ptr = envVar;272 while (1) {273 if ((strncmp(ptr, name, len) == 0) && (ptr[len] == ':')) {274 ptr += len + 1;275 PR_sscanf(ptr, "%d:0x%lx", &fileType, &osfd);276 switch ((PRDescType)fileType) {277 case PR_DESC_FILE:278 fd = PR_ImportFile(osfd);279 break;280 case PR_DESC_PIPE:281 fd = PR_ImportPipe(osfd);282 break;283 case PR_DESC_SOCKET_TCP:284 fd = PR_ImportTCPSocket(osfd);285 break;286 case PR_DESC_SOCKET_UDP:287 fd = PR_ImportUDPSocket(osfd);288 break;289 default:290 AssertFailed();291 PR_SetError(PR_UNKNOWN_ERROR, 0);292 fd = NULL;293 break;294 }295 if (fd) {296 /*297 * An inherited FD is inheritable by default.298 * The child process needs to call PR_SetFDInheritable299 * to make it non-inheritable if so desired.300 */301 fd->secret->inheritable = _PR_TRI_TRUE;302 }303 return fd;304 }305 /* Skip three colons */306 nColons = 0;307 while (*ptr) {308 if (*ptr == ':') {309 if (++nColons == 3) {310 break;311 }312 }313 ptr++;314 }315 if (*ptr == '\0') {316 PR_SetError(PR_UNKNOWN_ERROR, 0);317 return NULL;318 }319 ptr++;320 }321 }322 323 PR_IMPLEMENT(PRStatus) PR_CreateProcessDetached(324 const char *path,325 char *const *argv,326 char *const *envp,327 const PRProcessAttr *attr)328 {329 int vrc;330 int nEnv, idx;331 RTENV childEnv;332 RTENV newEnv = RTENV_DEFAULT;333 334 /* this code doesn't support all attributes */335 Assert(!attr || !attr->currentDirectory);336 /* no custom environment, please */337 Assert(!envp);338 339 childEnv = RTENV_DEFAULT;340 if (attr && attr->fdInheritBuffer) {341 vrc = RTEnvClone(&newEnv, childEnv);342 if (RT_FAILURE(vrc))343 return PR_FAILURE;344 vrc = RTEnvPutEx(newEnv, attr->fdInheritBuffer);345 if (RT_FAILURE(vrc))346 {347 RTEnvDestroy(newEnv);348 return PR_FAILURE;349 }350 childEnv = newEnv;351 }352 353 PRTHANDLE pStdIn = NULL, pStdOut = NULL, pStdErr = NULL;354 RTHANDLE hStdIn, hStdOut, hStdErr;355 if (attr && attr->stdinFd)356 {357 hStdIn.enmType = RTHANDLETYPE_FILE;358 RTFileFromNative(&hStdIn.u.hFile, attr->stdinFd->secret->md.osfd);359 pStdIn = &hStdIn;360 }361 if (attr && attr->stdoutFd)362 {363 hStdOut.enmType = RTHANDLETYPE_FILE;364 RTFileFromNative(&hStdOut.u.hFile, attr->stdoutFd->secret->md.osfd);365 pStdOut = &hStdOut;366 }367 if (attr && attr->stderrFd)368 {369 hStdErr.enmType = RTHANDLETYPE_FILE;370 RTFileFromNative(&hStdErr.u.hFile, attr->stderrFd->secret->md.osfd);371 pStdErr = &hStdErr;372 }373 374 vrc = RTProcCreateEx(path, (const char **)argv, childEnv,375 RTPROC_FLAGS_DETACHED, pStdIn, pStdOut, pStdErr,376 NULL /* pszAsUser */, NULL /* pszPassword */, NULL /* pExtraData */,377 NULL /* phProcess */);378 if (newEnv != RTENV_DEFAULT) {379 RTEnvDestroy(newEnv);380 }381 if (RT_SUCCESS(vrc))382 return PR_SUCCESS;383 else384 return PR_FAILURE;385 }386 387 112 /* prinit.c */ 388 113
Note:
See TracChangeset
for help on using the changeset viewer.