From e4a65c53d2faad3bfaa2e9d5d8b74e8a2bed9f86 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Wed, 30 Jan 2008 21:58:38 +0000 Subject: provide replacement functions for daemon() and getopt_long() when they are not available on the system git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@581 ef36b2f9-881f-0410-afb5-c4e39611909c --- compat/Makefile.am | 4 +++ compat/daemon.c | 71 ++++++++++++++++++++++++++++++++++++++++ compat/daemon.h | 31 ++++++++++++++++++ compat/getopt_long.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ compat/getopt_long.h | 50 ++++++++++++++++++++++++++++ 5 files changed, 248 insertions(+) create mode 100644 compat/daemon.c create mode 100644 compat/daemon.h create mode 100644 compat/getopt_long.c create mode 100644 compat/getopt_long.h (limited to 'compat') diff --git a/compat/Makefile.am b/compat/Makefile.am index 480d207..54e9be1 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -22,4 +22,8 @@ noinst_LIBRARIES = libcompat.a AM_CPPFLAGS=-I$(top_srcdir) AM_CFLAGS = -fPIC +EXTRA_DIST = getopt_long.c getopt_long.h \ + daemon.c daemon.h + libcompat_a_SOURCES = getpeercred.c getpeercred.h +libcompat_a_LIBADD = @LIBOBJS@ diff --git a/compat/daemon.c b/compat/daemon.c new file mode 100644 index 0000000..cd8e0f3 --- /dev/null +++ b/compat/daemon.c @@ -0,0 +1,71 @@ +/* + daemon.c - implementation of daemon() for systems that lack it + + Copyright (C) 2002, 2003, 2008 Arthur de Jong + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include "daemon.h" + +#include +#include +#include +#include + +int daemon(int nochdir,int noclose) +{ + /* change directory */ + if (!nochdir) + if (chdir("/")!=0) + return -1; + /* fork() and exit() to detach from the parent process */ + switch (fork()) + { + case 0: /* we are the child */ + break; + case -1: /* we are the parent, but have an error */ + return -1; + default: /* we are the parent and we're done*/ + _exit(0); + } + /* become process leader */ + if (setsid()<0) + { + return -1; + } + /* fork again so we cannot allocate a pty */ + switch (fork()) + { + case 0: /* we are the child */ + break; + case -1: /* we are the parent, but have an error */ + return -1; + default: /* we are the parent and we're done*/ + _exit(0); + } + /* close stdin, stdout and stderr and reconnect to /dev/null */ + if (!noclose) + { + close(0); /* stdin */ + close(1); /* stdout */ + close(2); /* stderr */ + open("/dev/null",O_RDWR); /* stdin, fd=0 */ + dup(0); /* stdout, fd=1 */ + dup(0); /* stderr, fd=2 */ + } + return 0; +} diff --git a/compat/daemon.h b/compat/daemon.h new file mode 100644 index 0000000..4d0f33a --- /dev/null +++ b/compat/daemon.h @@ -0,0 +1,31 @@ +/* + daemon.h - definition of daemon() for systems that lack it + + Copyright (C) 2002, 2003, 2008 Arthur de Jong + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#ifndef _DAEMON_H +#define _DAEMON_H 1 + +#include + +/* deamonize process, optionally chdir to / and optionally close stdin, + strdout and stderr and redirect them to /dev/null */ +int daemon(int nochdir,int noclose); + +#endif /* not _DAEMON_H */ diff --git a/compat/getopt_long.c b/compat/getopt_long.c new file mode 100644 index 0000000..a276dd5 --- /dev/null +++ b/compat/getopt_long.c @@ -0,0 +1,92 @@ +/* + getopt_long.c - implementation of getopt_long() for systems that lack it + + Copyright (C) 2001, 2002, 2008 Arthur de Jong + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include +#include +#include + +#include "getopt_long.h" + +/* this is a (poor) getopt_long() replacement for systems that don't have it + (getopt_long() is generaly a GNU extention) + this implementation is by no meens flawless, especialy the optional arguments + to options and options following filenames is not quite right, allso + minimal error checking is provided + */ +int getopt_long(int argc,char * const argv[], + const char *optstring, + const struct option *longopts,int *longindex) +{ + int i; /* for looping through options */ + int l; /* for length */ + + /* first check if there realy is a -- option */ + if ( (optind>0)&&(optind