Changeset 1052
- Timestamp:
- Jan 25, 2004, 3:17:26 AM (21 years ago)
- Location:
- trunk/src/emx/src/lib/lgpl
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/src/emx/src/lib/lgpl/posix/spawn_int.h ¶
-
Property cvs2svn:cvs-rev
changed from
1.2
to1.3
r1051 r1052 28 28 } open_action; 29 29 } action; 30 31 #ifdef __EMX__32 /* reserve space for saving the handle which we're closing / replacing. */33 int fd_saved; /* handle to the saved file. -1 if N/A. */34 int fd_saved_coe; /* original close on exec flag. */35 #endif36 30 }; 37 31 -
Property cvs2svn:cvs-rev
changed from
-
TabularUnified trunk/src/emx/src/lib/lgpl/sysdeps/os2/__spawni.c ¶
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.2
r1051 r1052 26 26 27 27 28 /******************************************************************************* 29 * Header Files * 30 *******************************************************************************/ 28 31 #include <stdlib.h> 29 32 #include <string.h> … … 34 37 #include <process.h> 35 38 #include <emx/io.h> 39 #include <emx/umalloc.h> 40 #ifdef DEBUG 41 #include <assert.h> 42 #endif 36 43 #include "../../posix/spawn_int.h" 44 45 /******************************************************************************* 46 * Structures and Typedefs * 47 *******************************************************************************/ 48 typedef struct RestoreAction 49 { 50 int fdClose; /* handle to close before restoring fdRestore. */ 51 int fdRestore; /* fd which fdSaved is a saved version of. */ 52 int fdSaved; /* close-on-exec dup of fdRestore. */ 53 int fSavedCoE; /* the original close-on-exec flag. */ 54 } RESTOREACTION, *PRESTOREACTION; 37 55 38 56 /** … … 49 67 * Set to -1 on failure. 50 68 */ 51 static int save_handle(int fdToSave, int *pfdMin, int *pfdSaved, int *pfCoE)69 static int save_handle(int fdToSave, int *pfdMin, PRESTOREACTION pRAction) 52 70 { 53 int fCoE;54 71 int fdSaved; 55 56 *pfCoE = *pfdSaved = -1; 72 int fSavedCoE; 73 74 pRAction->fdRestore = - 1; 75 pRAction->fdSaved = -1; 76 pRAction->fSavedCoE = -1; 77 if (!__libc_FH(fdToSave)) 78 return 0; /* no such handle. */ 57 79 58 80 /* Get close on exec flag. */ 59 f CoE = fcntl(fdToSave, F_GETFD);60 if (f CoE < 0)81 fSavedCoE = fcntl(fdToSave, F_GETFD); 82 if (fSavedCoE < 0) 61 83 return -1; 62 84 63 85 /* find free handle and duplicate fdToSave onto it */ 64 while ( !__libc_FH(*pfdMin))86 while (__libc_FH(*pfdMin)) 65 87 (*pfdMin)++; 66 if (*pfdMin >= 65536)88 if (*pfdMin >= 0x10000) 67 89 return -1; 68 90 fdSaved = dup2(fdToSave, *pfdMin); … … 71 93 72 94 /* marke it close on exit. */ 73 if ( f CoE95 if ( fSavedCoE 74 96 && fcntl(fdSaved, F_SETFD, FD_CLOEXEC) < 0) 75 97 { … … 78 100 return -1; 79 101 } 80 *pfCoE = fCoE; 81 *pfdSaved = fdSaved; 102 pRAction->fdRestore = fdToSave; 103 pRAction->fdSaved = fdSaved; 104 pRAction->fSavedCoE = fSavedCoE; 82 105 return 0; 83 106 } … … 88 111 * 89 112 * @param fdToRestore The original handle which is to be restored. 90 * @param pfdSaved Pointer to the save handle. 91 * This will be set to -1. 92 * @param pfCoE Pointer to the 93 * This will be set to -1. 113 * @param pRAction Restore action structure. 94 114 */ 95 static void restore_handle( int fdToRestore, int *pfdSaved, int *pfCoE)115 static void restore_handle(PRESTOREACTION pRAction) 96 116 { 97 int saved_errno; 98 if (*pfdSaved == -1) 99 return; 100 saved_errno = errno; 101 102 dup2(*pfdSaved, fdToRestore); 103 close(*pfdSaved); 104 fcntl(fdToRestore, F_SETFD, *pfCoE); 105 *pfdSaved = -1; 106 *pfCoE = -1; 117 int saved_errno = errno; 118 119 if (pRAction->fdClose >= 0) 120 { 121 #ifndef DEBUG 122 close(pRAction->fdClose); 123 #else 124 if (close(pRAction->fdClose)) 125 assert(!"__spawni: restore close() failed"); 126 #endif 127 pRAction->fdClose = -1; 128 } 129 130 if (pRAction->fdSaved >= 0) 131 { 132 #ifndef DEBUG 133 dup2(pRAction->fdSaved, pRAction->fdRestore); 134 close(pRAction->fdSaved); 135 fcntl(pRAction->fdRestore, F_SETFD, pRAction->fSavedCoE); 136 #else 137 if (dup2(pRAction->fdSaved, pRAction->fdRestore) < 0) 138 assert(!"__spawni: restore dup2() failed"); 139 if (close(pRAction->fdSaved) < 0) 140 assert(!"__spawni: restore close(saved) failed"); 141 if (fcntl(pRAction->fdRestore, F_SETFD, pRAction->fSavedCoE) < 0) 142 assert(!"__spawni: restore fcntl() failed"); 143 #endif 144 pRAction->fdSaved = -1; 145 pRAction->fSavedCoE = -1; 146 } 107 147 108 148 errno = saved_errno; … … 120 160 int use_path) 121 161 { 122 int iFileAction = 0; /* where to start cleaning up file actions from. */ 123 int rc; /* result. */ 124 pid_t pidChild; 162 int iFileAction = 0; /* where to start cleaning up file actions from. */ 163 PRESTOREACTION paRestore = NULL; 164 int rc; /* result. */ 165 pid_t pidChild; 125 166 #if 0 /* requires some process management from LIBC */ 126 167 unsigned fFlags = attrp ? attrp->__flags : 0; … … 170 211 171 212 /* Verify handles and predict what open() and dup2() will return so we can 172 find the min save handle number. */ 213 find the min save handle number. 214 Note. we're counting too many opens here... but who cares. */ 173 215 for (i = cOpens = 0; !rc && i < file_actions->__used; ++i) 174 216 { 175 217 struct __spawn_action *pAction = (struct __spawn_action *)&file_actions->__actions[i]; /* nasty - const */ 176 pAction->fd_saved = pAction->fd_saved_coe = -1;177 218 switch (pAction->tag) 178 219 { … … 199 240 } 200 241 201 for (fdMin = 0; cOpens >= 0; fdMin++) 242 paRestore = _hmalloc(sizeof(RESTOREACTION) * file_actions->__used); 243 if (!paRestore) 244 { 245 /* TODO: release the file handle table lock. */ 246 return -1; 247 } 248 memset(paRestore, -1, sizeof(RESTOREACTION) * file_actions->__used); 249 250 for (fdMin = 0; cOpens >= 0 && fdMin < 0x10000; fdMin++) 202 251 { 203 252 int fClosed; … … 217 266 break; 218 267 case spawn_do_open: 268 if (pAction->action.open_action.fd == fdMin) 269 fDuped = 1; 219 270 break; 220 271 } … … 225 276 continue; 226 277 /* if not closed or not being freed we cannot use it. */ 227 if (!fClosed ||__libc_FH(fdMin))278 if (!fClosed && __libc_FH(fdMin)) 228 279 continue; 229 280 /* it's either closed or free */ … … 235 286 { 236 287 struct __spawn_action *pAction = (struct __spawn_action *)&file_actions->__actions[i]; /* nasty - const */ 288 PRESTOREACTION pRAction = &paRestore[i]; 237 289 238 290 switch (pAction->tag) 239 291 { 240 292 case spawn_do_close: 241 rc = save_handle(pAction->action.close_action.fd, &fdMin, &pAction->fd_saved_coe, &pAction->fd_saved_coe);293 rc = save_handle(pAction->action.close_action.fd, &fdMin, pRAction); 242 294 if (rc) 243 295 break; 244 296 if (close(pAction->action.close_action.fd)) 245 297 { 246 restore_handle(p Action->action.close_action.fd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);298 restore_handle(pRAction); 247 299 rc = -1; 248 300 } … … 250 302 251 303 case spawn_do_open: 252 pAction->fd_saved = open(pAction->action.open_action.path, 253 pAction->action.open_action.mode, 254 pAction->action.open_action.oflag); 255 if ( pAction->fd_saved < 0 256 || pAction->fd_saved != pAction->action.open_action.fd) 257 rc = -1; 304 pRAction->fdClose = open(pAction->action.open_action.path, 305 pAction->action.open_action.oflag, 306 pAction->action.open_action.mode); 307 if (pRAction->fdClose < 0) 308 rc = -1; 309 else if ( pRAction->fdClose != pAction->action.open_action.fd 310 && pAction->action.open_action.fd != -1 /* -1 is not standard */) 311 { 312 int fd = pRAction->fdClose; 313 pRAction->fdClose = -1; 314 rc = save_handle(pAction->action.open_action.fd, &fdMin, pRAction); 315 if (!rc) 316 { 317 if (dup2(fd, pAction->action.open_action.fd) < 0) 318 { 319 restore_handle(pRAction); 320 rc = -1; 321 } 322 } 323 close(fd); 324 } 258 325 break; 259 326 … … 261 328 if (pAction->action.dup2_action.newfd != -1) 262 329 { 263 rc = save_handle(pAction->action.dup2_action.newfd, &fdMin, &pAction->fd_saved_coe, &pAction->fd_saved_coe);330 rc = save_handle(pAction->action.dup2_action.newfd, &fdMin, pRAction); 264 331 if (rc) 265 332 break; 266 333 } 267 334 268 if (dup2(pAction->action.dup2_action.fd, pAction->action.dup2_action.newfd) < 0) 335 pRAction->fdClose = dup2(pAction->action.dup2_action.fd, pAction->action.dup2_action.newfd); 336 if (pRAction->fdClose < 0) 269 337 { 270 restore_handle(p Action->action.dup2_action.newfd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);338 restore_handle(pRAction); 271 339 rc = -1; 272 340 } … … 305 373 * Restore the file handles. 306 374 */ 307 if ( file_actions)375 if (paRestore) 308 376 { 309 377 int save_errno = errno; 310 378 while (iFileAction-- > 0) 311 { 312 struct __spawn_action *pAction = (struct __spawn_action *)&file_actions->__actions[iFileAction]; /* nasty - const */ 313 switch (pAction->tag) 314 { 315 case spawn_do_close: 316 restore_handle(pAction->action.close_action.fd, &pAction->fd_saved_coe, &pAction->fd_saved_coe); 317 break; 318 case spawn_do_open: 319 close(pAction->fd_saved); 320 break; 321 case spawn_do_dup2: 322 restore_handle(pAction->action.dup2_action.newfd, &pAction->fd_saved_coe, &pAction->fd_saved_coe); 323 break; 324 } 325 } 379 restore_handle(&paRestore[iFileAction]); 380 free(paRestore); 326 381 errno = save_errno; 327 382 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.