initial checkin
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Fri, 2 Jul 2004 23:34:52 +0000 (23:34 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Fri, 2 Jul 2004 23:34:52 +0000 (23:34 +0000)
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 [new file with mode: 0644]
util-vserver/lib_internal/errinfo.h [new file with mode: 0644]
util-vserver/lib_internal/filecfg-readentryflag.c [new file with mode: 0644]
util-vserver/lib_internal/filecfg-readentrystr.c [new file with mode: 0644]
util-vserver/lib_internal/filecfg.h [new file with mode: 0644]
util-vserver/lib_internal/util-canonify.c [new file with mode: 0644]
util-vserver/lib_internal/util-lockfile.c [new file with mode: 0644]
util-vserver/lib_internal/util-lockfile.h [new file with mode: 0644]

diff --git a/util-vserver/lib_internal/errinfo-writeerrno.c b/util-vserver/lib_internal/errinfo-writeerrno.c
new file mode 100644 (file)
index 0000000..ae9d947
--- /dev/null
@@ -0,0 +1,44 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#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 (file)
index 0000000..10046a0
--- /dev/null
@@ -0,0 +1,31 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 (file)
index 0000000..38d7a8d
--- /dev/null
@@ -0,0 +1,39 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+
+#include "filecfg.h"
+
+#include <sys/stat.h>
+#include <unistd.h>
+#include <string.h>
+
+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 (file)
index 0000000..43627ac
--- /dev/null
@@ -0,0 +1,74 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+
+#include "filecfg.h"
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+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 && sz<FILECFG_MAX_FILESIZE) {
+    char               buf[sz+1];
+    
+    if (read(fd, buf, sz+1)!=sz) goto err;
+
+    if (!allow_multiline) {
+      char *           pos;
+      
+      buf[sz] = '\0';
+      pos     = strchr(buf, '\n');
+      if (pos) *pos = '\0';
+    }
+    else {
+      while (sz>0 && 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 (file)
index 0000000..1c7f70b
--- /dev/null
@@ -0,0 +1,31 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <stdbool.h>
+
+// 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 (file)
index 0000000..159c70f
--- /dev/null
@@ -0,0 +1,40 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#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 (file)
index 0000000..b08f5a3
--- /dev/null
@@ -0,0 +1,86 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <config.h>
+#endif
+
+#include "util-lockfile.h"
+#include "errinfo.h"
+
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <errno.h>
+
+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 (file)
index 0000000..d8ac09c
--- /dev/null
@@ -0,0 +1,30 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// 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 <stdbool.h>
+
+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