From 8c0748073a0192ab16e9267979d7507e6270690d Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Fri, 2 Jul 2004 23:34:52 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1616 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib_internal/errinfo-writeerrno.c | 44 ++++++++++++ util-vserver/lib_internal/errinfo.h | 31 ++++++++ util-vserver/lib_internal/filecfg-readentryflag.c | 39 ++++++++++ util-vserver/lib_internal/filecfg-readentrystr.c | 74 +++++++++++++++++++ util-vserver/lib_internal/filecfg.h | 31 ++++++++ util-vserver/lib_internal/util-canonify.c | 40 +++++++++++ util-vserver/lib_internal/util-lockfile.c | 86 +++++++++++++++++++++++ util-vserver/lib_internal/util-lockfile.h | 30 ++++++++ 8 files changed, 375 insertions(+) create mode 100644 util-vserver/lib_internal/errinfo-writeerrno.c create mode 100644 util-vserver/lib_internal/errinfo.h create mode 100644 util-vserver/lib_internal/filecfg-readentryflag.c create mode 100644 util-vserver/lib_internal/filecfg-readentrystr.c create mode 100644 util-vserver/lib_internal/filecfg.h create mode 100644 util-vserver/lib_internal/util-canonify.c create mode 100644 util-vserver/lib_internal/util-lockfile.c create mode 100644 util-vserver/lib_internal/util-lockfile.h diff --git a/util-vserver/lib_internal/errinfo-writeerrno.c b/util-vserver/lib_internal/errinfo-writeerrno.c new file mode 100644 index 0000000..ae9d947 --- /dev/null +++ b/util-vserver/lib_internal/errinfo-writeerrno.c @@ -0,0 +1,44 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "errinfo.h" +#include "util.h" + +void +ErrInfo_writeErrno(struct ErrorInformation const *info) +{ + if (info->app) { + WRITE_STR(2, info->app); + WRITE_MSG(2, ": "); + } + + if (info->pos) { + WRITE_STR(2, info->pos); + if (info->id!=0) WRITE_MSG(2, ": "); + } + + if (info->id!=0) { + WRITE_STR(2, strerror(info->id)); + } + + WRITE_MSG(2, "\n"); +} diff --git a/util-vserver/lib_internal/errinfo.h b/util-vserver/lib_internal/errinfo.h new file mode 100644 index 0000000..10046a0 --- /dev/null +++ b/util-vserver/lib_internal/errinfo.h @@ -0,0 +1,31 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifndef H_UTIL_VSERVER_LIB_INTERNAL_ERRINFO_H +#define H_UTIL_VSERVER_LIB_INTERNAL_ERRINFO_H + +struct ErrorInformation +{ + char const * app; // the application-name + char const * pos; // the detailed position of the error + int id; // usually errno +}; + +void ErrInfo_writeErrno(struct ErrorInformation const *); + +#endif // H_UTIL_VSERVER_LIB_INTERNAL_ERRINFO_H diff --git a/util-vserver/lib_internal/filecfg-readentryflag.c b/util-vserver/lib_internal/filecfg-readentryflag.c new file mode 100644 index 0000000..38d7a8d --- /dev/null +++ b/util-vserver/lib_internal/filecfg-readentryflag.c @@ -0,0 +1,39 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "filecfg.h" + +#include +#include +#include + +bool +FileCfg_readEntryFlag(PathInfo const *base, char const *file, bool dflt) +{ + PathInfo filepath = { .d = file, .l = strlen(file) }; + PathInfo path = *base; + char path_buf[ENSC_PI_APPSZ(path, filepath)]; + struct stat st; + + PathInfo_append(&path, &filepath, path_buf); + return stat(path.d, &st)!=-1 || dflt; +} diff --git a/util-vserver/lib_internal/filecfg-readentrystr.c b/util-vserver/lib_internal/filecfg-readentrystr.c new file mode 100644 index 0000000..43627ac --- /dev/null +++ b/util-vserver/lib_internal/filecfg-readentrystr.c @@ -0,0 +1,74 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "filecfg.h" +#include +#include +#include + +char * +FileCfg_readEntryStr (PathInfo const *base, char const *file, + bool allow_multiline, char const *dflt) +{ + PathInfo filepath = { .d = file, .l = strlen(file) }; + PathInfo path = *base; + char path_buf[ENSC_PI_APPSZ(path, filepath)]; + int fd = -1; + off_t sz; + char * res = 0; + + PathInfo_append(&path, &filepath, path_buf); + fd = open(path.d, O_RDONLY); + if (fd==-1) goto err; + + sz = lseek(fd, 0, SEEK_END); + if (sz==-1 || + lseek(fd, 0, SEEK_SET)==-1) goto err; + + + if (sz>0 && sz0 && buf[sz-1]=='\n') --sz; + buf[sz] = '\0'; + } + + res = strdup(buf); + } + + err: + if (res==0 && dflt) + res = strdup(dflt); + + if (fd!=-1) close(fd); + return res; +} diff --git a/util-vserver/lib_internal/filecfg.h b/util-vserver/lib_internal/filecfg.h new file mode 100644 index 0000000..1c7f70b --- /dev/null +++ b/util-vserver/lib_internal/filecfg.h @@ -0,0 +1,31 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifndef H_UTIL_VSERVER_LIB_INTERNAL_FILECFG_H +#define H_UTIL_VSERVER_LIB_INTERNAL_FILECFG_H + +#include "pathinfo.h" +#include + +// 1MiB should be enough for all applications +#define FILECFG_MAX_FILESIZE 0x100000 + +char * FileCfg_readEntryStr (PathInfo const *base, char const *file, bool allow_multiline, char const *dflt); +bool FileCfg_readEntryFlag(PathInfo const *base, char const *file, bool dflt); + +#endif // H_UTIL_VSERVER_LIB_INTERNAL_FILECFG_H diff --git a/util-vserver/lib_internal/util-canonify.c b/util-vserver/lib_internal/util-canonify.c new file mode 100644 index 0000000..159c70f --- /dev/null +++ b/util-vserver/lib_internal/util-canonify.c @@ -0,0 +1,40 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "util.h" + +size_t +canonifyVserverName(char *name) +{ + char *in = name; + char *out = name; + + while (*in) { + if ((*in>='a' && *in<='z') || + (*in>='A' && *in<='Z') || + (*in>='0' && *in<='9')) + *out++ = *in; + ++in; + } + *out = '\0'; + return out-name; +} diff --git a/util-vserver/lib_internal/util-lockfile.c b/util-vserver/lib_internal/util-lockfile.c new file mode 100644 index 0000000..b08f5a3 --- /dev/null +++ b/util-vserver/lib_internal/util-lockfile.c @@ -0,0 +1,86 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "util-lockfile.h" +#include "errinfo.h" + +#include +#include +#include +#include +#include +#include + +static volatile sig_atomic_t alarm_flag = 0; + +static void +alarmFunc(int UNUSED sig) +{ + alarm_flag = 1; + signal(SIGALRM, alarmFunc); +} + +bool +lockfile(int *fd, char const *filename, int op, long timeout, + struct ErrorInformation *err) +{ + char const *errstr = 0; + void (*old_sighandler)(int) = 0; + + errstr = "open()"; + *fd = open(filename, O_CREAT|O_RDONLY|O_NOFOLLOW|O_NONBLOCK, 0644); + if (*fd==-1) goto err; + + if (timeout!=-1) { + errstr = "siginterrupt()"; + if (siginterrupt(SIGALRM, 1)==-1) goto err; + + errstr = "signal()"; + old_sighandler = signal(SIGALRM, alarmFunc); + if (old_sighandler==SIG_ERR) goto err; + + alarm(timeout); + } + + errstr = "flock()"; + while (flock(*fd, op)==-1) { + if ((errno!=EINTR && errno!=EINTR) || alarm_flag) goto err; + } + + if (timeout!=-1 && old_sighandler!=0) + signal(SIGALRM, old_sighandler); + + errstr = "fcntl()"; + if (fcntl(*fd, F_SETFD, FD_CLOEXEC)==-1) goto err; + + return true; + + err: + if (err) { + err->pos = errstr; + err->id = errno; + } + if (timeout!=-1 && old_sighandler!=0) + signal(SIGALRM, old_sighandler); + if (*fd!=-1) close(*fd); + return false; +} diff --git a/util-vserver/lib_internal/util-lockfile.h b/util-vserver/lib_internal/util-lockfile.h new file mode 100644 index 0000000..d8ac09c --- /dev/null +++ b/util-vserver/lib_internal/util-lockfile.h @@ -0,0 +1,30 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifndef H_UTIL_VSERVER_LIB_INTERNAL_UTIL_LOCKFILE_H +#define H_UTIL_VSERVER_LIB_INTERNAL_UTIL_LOCKFILE_H + +#include + +struct ErrorInformation; + +bool lockfile(int *fd, char const *filename, int op, + long timeout, + struct ErrorInformation *err); + +#endif // H_UTIL_VSERVER_LIB_INTERNAL_UTIL_LOCKFILE_H -- 1.8.1.5