This is Unofficial EPICS BASE Doxygen Site
osdProcess.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
11 /*
12  * Operating System Dependent Implementation of osiProcess.h
13  *
14  * Author: Jeff Hill
15  *
16  */
17 
18 #include <string.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <limits.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sched.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28 
29 #include "osiProcess.h"
30 #include "errlog.h"
31 #include "epicsAssert.h"
32 
33 LIBCOM_API osiGetUserNameReturn epicsStdCall osiGetUserName (char *pBuf, unsigned bufSizeIn)
34 {
35  struct passwd *p;
36 
37  p = getpwuid ( getuid () );
38  if ( p && p->pw_name ) {
39  size_t len = strlen ( p->pw_name );
40  unsigned uiLength;
41 
42  if ( len > UINT_MAX || len <= 0 ) {
43  return osiGetUserNameFail;
44  }
45  uiLength = (unsigned) len;
46 
47  if ( uiLength + 1 >= bufSizeIn ) {
48  return osiGetUserNameFail;
49  }
50 
51  strncpy ( pBuf, p->pw_name, (size_t) bufSizeIn );
52 
53  return osiGetUserNameSuccess;
54  }
55  else {
56  return osiGetUserNameFail;
57  }
58 }
59 
61  (const char *pProcessName, const char *pBaseExecutableName)
62 {
63  int status;
64 
65  /*
66  * create a duplicate process
67  */
68  status = fork ();
69  if (status < 0) {
71  }
72 
73  /*
74  * return to the caller
75  * in the initiating (parent) process
76  */
77  if (status) {
79  }
80 
81  /*
82  * This is executed only by the new child process.
83  * Close all open files except for STDIO, so they will not
84  * be inherited by the new program.
85  */
86  {
87  int fd, maxfd = sysconf ( _SC_OPEN_MAX );
88  for ( fd = 0; fd <= maxfd; fd++ ) {
89  if (fd==STDIN_FILENO) continue;
90  if (fd==STDOUT_FILENO) continue;
91  if (fd==STDERR_FILENO) continue;
92  close (fd);
93  }
94  }
95 
96 #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && _POSIX_THREAD_PRIORITY_SCHEDULING > 0
97  /*
98  * Drop real-time SCHED_FIFO priority
99  */
100  {
101  struct sched_param p;
102 
103  p.sched_priority = 0;
104  status = sched_setscheduler(0, SCHED_OTHER, &p);
105  }
106 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
107 
108  /*
109  * Run the specified executable
110  */
111  status = execlp (pBaseExecutableName, pBaseExecutableName, (char *)NULL);
112  if ( status < 0 ) {
113  fprintf ( stderr, "**** The executable \"%s\" couldn't be located\n", pBaseExecutableName );
114  fprintf ( stderr, "**** because of errno = \"%s\".\n", strerror (errno) );
115  fprintf ( stderr, "**** You may need to modify your PATH environment variable.\n" );
116  fprintf ( stderr, "**** Unable to start \"%s\" process.\n", pProcessName);
117  }
118  /* Don't run our parent's atexit() handlers */
119  _exit ( -1 );
120 }
pvd::Status status
An EPICS-specific replacement for ANSI C&#39;s assert.
#define NULL
Definition: catime.c:38
osiSpawnDetachedProcessReturn
Definition: osiProcess.h:35
LIBCOM_API osiGetUserNameReturn epicsStdCall osiGetUserName(char *pBuf, unsigned bufSizeIn)
Definition: osdProcess.c:33
LIBCOM_API osiSpawnDetachedProcessReturn epicsStdCall osiSpawnDetachedProcess(const char *pProcessName, const char *pBaseExecutableName)
Definition: osdProcess.c:61
osiGetUserNameReturn
Definition: osiProcess.h:25
#define stderr
Definition: epicsStdio.h:32