Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/compat
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2008-01-30 22:58:38 +0100
committerArthur de Jong <arthur@arthurdejong.org>2008-01-30 22:58:38 +0100
commite4a65c53d2faad3bfaa2e9d5d8b74e8a2bed9f86 (patch)
treeab85f9692b9e57c9e205080c359c4abbbb59af1a /compat
parentf5b3d3fffd26017f01b575a107e939024667258f (diff)
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
Diffstat (limited to 'compat')
-rw-r--r--compat/Makefile.am4
-rw-r--r--compat/daemon.c71
-rw-r--r--compat/daemon.h31
-rw-r--r--compat/getopt_long.c92
-rw-r--r--compat/getopt_long.h50
5 files changed, 248 insertions, 0 deletions
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 <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+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 <unistd.h>
+
+/* 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 <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#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<argc) &&
+ (strncmp(argv[optind],"--",2)==0) &&
+ (argv[optind][2]!='\0') )
+ {
+ /* check the longopts list for a valid option */
+ for (i=0;longopts[i].name!=NULL;i++)
+ {
+ /* save the length for later */
+ l=strlen(longopts[i].name);
+ if (strncmp(argv[optind]+2,longopts[i].name,l)==0)
+ {
+ /* we have a match */
+ if ( (longopts[i].has_arg==no_argument) &&
+ (argv[optind][2+l]=='\0') )
+ {
+ optind++;
+ return longopts[i].val;
+ }
+ else if ( (longopts[i].has_arg==required_argument) &&
+ (argv[optind][2+l]=='=') )
+ {
+ optarg=argv[optind]+3+l;
+ optind++;
+ return longopts[i].val;
+ }
+ else if ( (longopts[i].has_arg==required_argument) &&
+ (argv[optind][2+l]=='\0') )
+ {
+ optarg=argv[optind+1];
+ optind+=2;
+ return longopts[i].val;
+ }
+ else if ( (longopts[i].has_arg==optional_argument) &&
+ (argv[optind][2+l]=='=') )
+ {
+ optarg=argv[optind]+3+l;
+ optind++;
+ return longopts[i].val;
+ }
+ else if ( (longopts[i].has_arg==optional_argument) &&
+ (argv[optind][2+l]=='\0') )
+ {
+ optind++;
+ return longopts[i].val;
+ }
+ }
+ }
+ }
+ /* if all else fails use plain getopt() */
+ return getopt(argc,argv,optstring);
+}
diff --git a/compat/getopt_long.h b/compat/getopt_long.h
new file mode 100644
index 0000000..7ddf5a3
--- /dev/null
+++ b/compat/getopt_long.h
@@ -0,0 +1,50 @@
+/*
+ getopt_long.h - definition 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
+*/
+
+#ifndef _GETOPT_LONG_H
+#define _GETOPT_LONG_H 1
+
+#ifndef HAVE_GETOPT_LONG
+
+#define no_argument 0
+#define required_argument 1
+#define optional_argument 2
+
+struct option {
+ const char *name;
+ int has_arg;
+ int *flag;
+ int val;
+};
+
+/* this is a (poor) getopt_long() replacement for systems that don't have it
+ (this 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
+ */
+int getopt_long(int argc,char * const argv[],
+ const char *optstring,
+ const struct option *longopts,int *longindex);
+
+#endif /* not HAVE_GETOPT_LONG */
+
+#endif /* _GETOPT_LONG_H */