From 693ffb8466ada58ecc59fde754ba79fc6f51528d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 2 May 2000 02:23:41 +0000 Subject: 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) --- source3/lib/debug.c | 2 +- source3/lib/fault.c | 2 +- source3/lib/genrand.c | 2 +- source3/lib/ms_fnmatch.c | 2 ++ source3/lib/pidfile.c | 2 +- source3/lib/smbrun.c | 2 +- source3/lib/substitute.c | 2 +- source3/lib/system.c | 32 +++++++++++++++++++++++++++++++- source3/lib/util.c | 6 +++--- 9 files changed, 42 insertions(+), 10 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/debug.c b/source3/lib/debug.c index ed27b93cfd..675c2d8cfc 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -580,7 +580,7 @@ BOOL dbghdr( int level, char *file, char *func, int line ) header_str[0] = '\0'; if( lp_debug_pid()) - slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)getpid()); + slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)sys_getpid()); if( lp_debug_uid()) { size_t hs_len = strlen(header_str); diff --git a/source3/lib/fault.c b/source3/lib/fault.c index 6effaf7d7c..29272f1928 100644 --- a/source3/lib/fault.c +++ b/source3/lib/fault.c @@ -38,7 +38,7 @@ static void fault_report(int sig) counter++; DEBUG(0,("===============================================================\n")); - DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),VERSION)); + DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),VERSION)); DEBUG(0,("\nPlease read the file BUGS.txt in the distribution\n")); DEBUG(0,("===============================================================\n")); diff --git a/source3/lib/genrand.c b/source3/lib/genrand.c index a9698d4cd1..102eec6300 100644 --- a/source3/lib/genrand.c +++ b/source3/lib/genrand.c @@ -163,7 +163,7 @@ static uint32 do_reseed(unsigned char *md4_outbuf) * Finally add the counter, time of day, and pid. */ GetTimeOfDay(&tval); - mypid = getpid(); + mypid = sys_getpid(); v1 = (counter++) + mypid + tval.tv_sec; v2 = (counter++) * mypid + tval.tv_usec; diff --git a/source3/lib/ms_fnmatch.c b/source3/lib/ms_fnmatch.c index ee9b1cf985..364fd6f347 100644 --- a/source3/lib/ms_fnmatch.c +++ b/source3/lib/ms_fnmatch.c @@ -36,6 +36,8 @@ matching routine! NOTE: this matches only filenames with no directory component + + Returns 0 on match, -1 on fail. */ int ms_fnmatch(char *pattern, char *string) { diff --git a/source3/lib/pidfile.c b/source3/lib/pidfile.c index 726e8c1f21..a6dc327fd7 100644 --- a/source3/lib/pidfile.c +++ b/source3/lib/pidfile.c @@ -101,7 +101,7 @@ void pidfile_create(char *name) } memset(buf, 0, sizeof(buf)); - slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) getpid()); + slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid()); if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { DEBUG(0,("ERROR: can't write to file %s: %s\n", pidFile, strerror(errno))); diff --git a/source3/lib/smbrun.c b/source3/lib/smbrun.c index 8d666980b3..81dfc10dfb 100644 --- a/source3/lib/smbrun.c +++ b/source3/lib/smbrun.c @@ -124,7 +124,7 @@ int smbrun(char *cmd,char *outfile,BOOL shared) CatchChildLeaveStatus(); - if ((pid=fork()) < 0) { + if ((pid=sys_fork()) < 0) { DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) )); CatchChild(); return errno; diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c index c43c133d6a..2a575f0c38 100644 --- a/source3/lib/substitute.c +++ b/source3/lib/substitute.c @@ -176,7 +176,7 @@ void standard_sub_basic(char *str) case 'T' : string_sub(p,"%T", timestring(False),l); break; case 'a' : string_sub(p,"%a", remote_arch,l); break; case 'd' : - slprintf(pidstr,sizeof(pidstr), "%d",(int)getpid()); + slprintf(pidstr,sizeof(pidstr), "%d",(int)sys_getpid()); string_sub(p,"%d", pidstr,l); break; case 'h' : string_sub(p,"%h", myhostname(),l); break; diff --git a/source3/lib/system.c b/source3/lib/system.c index 539f21d49c..80a968b634 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -932,11 +932,41 @@ static char **extract_args(const char *command) return argl; } +/************************************************************************** + 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; diff --git a/source3/lib/util.c b/source3/lib/util.c index 955e1df080..8ac9223f2e 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -741,7 +741,7 @@ become a daemon, discarding the controlling terminal ****************************************************************************/ void become_daemon(void) { - if (fork()) { + if (sys_fork()) { _exit(0); } @@ -1584,7 +1584,7 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type) if ((ret != -1) && (lock.l_type != F_UNLCK) && (lock.l_pid != 0) && - (lock.l_pid != getpid())) + (lock.l_pid != sys_getpid())) { DEBUG(3,("fd %d is locked by pid %d\n",fd,(int)lock.l_pid)); return(True); @@ -2110,7 +2110,7 @@ int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6) char pidstr[10]; pstring cmd = "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'"; - slprintf(pidstr, sizeof(pidstr), "%d", getpid()); + slprintf(pidstr, sizeof(pidstr), "%d", sys_getpid()); pstring_sub(cmd, "%d", pidstr); if (!fn) { -- cgit