diff options
author | Jeremy Allison <jra@samba.org> | 2000-05-02 02:23:41 +0000 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2000-05-02 02:23:41 +0000 |
commit | 693ffb8466ada58ecc59fde754ba79fc6f51528d (patch) | |
tree | 639fae54b3d874aae78e5732aef20f52de5b60bf /source3/lib/system.c | |
parent | 830a9e571eee5330097376e94af7dc0f2d5f2f02 (diff) | |
download | samba-693ffb8466ada58ecc59fde754ba79fc6f51528d.tar.gz samba-693ffb8466ada58ecc59fde754ba79fc6f51528d.tar.xz samba-693ffb8466ada58ecc59fde754ba79fc6f51528d.zip |
Added sys_fork() and sys_getpid() functions to stop the overhead
of doing a system call every time we want to just get our pid.
Jeremy.
(This used to be commit 148628b616b5c29ba6340d65fc3ddbcabba6e67a)
Diffstat (limited to 'source3/lib/system.c')
-rw-r--r-- | source3/lib/system.c | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/source3/lib/system.c b/source3/lib/system.c index 539f21d49c1..80a968b634c 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -933,10 +933,40 @@ static char **extract_args(const char *command) } /************************************************************************** + Wrapper for fork. Ensures that mypid is reset. Used so we can write + a sys_getpid() that only does a system call *once*. +****************************************************************************/ + +static pid_t mypid = (pid_t)-1; + +pid_t sys_fork(void) +{ + pid_t forkret = fork(); + + if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */ + mypid = (pid_t) -1; + + return forkret; +} + +/************************************************************************** + Wrapper for getpid. Ensures we only do a system call *once*. +****************************************************************************/ + +pid_t sys_getpid(void) +{ + if (mypid == (pid_t)-1) + mypid = getpid(); + + return mypid; +} + +/************************************************************************** Wrapper for popen. Safer as it doesn't search a path. Modified from the glibc sources. modified by tridge to return a file descriptor. We must kick our FILE* habit ****************************************************************************/ + typedef struct _popen_list { int fd; @@ -974,7 +1004,7 @@ int sys_popen(const char *command) if(!(argl = extract_args(command))) goto err_exit; - entry->child_pid = fork(); + entry->child_pid = sys_fork(); if (entry->child_pid == -1) { goto err_exit; |