--- /dev/null
+// $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");
+}
--- /dev/null
+// $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
--- /dev/null
+// $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;
+}
--- /dev/null
+// $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;
+}
--- /dev/null
+// $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
--- /dev/null
+// $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;
+}
--- /dev/null
+// $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;
+}
--- /dev/null
+// $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