diff --git a/ncmlib/CMakeLists.txt b/ncmlib/CMakeLists.txt new file mode 100644 index 0000000..6afd004 --- /dev/null +++ b/ncmlib/CMakeLists.txt @@ -0,0 +1,12 @@ +set(NCMLIB_SRCS + malloc.c + chroot.c + pidfile.c + signals.c + strlist.c + strl.c + log.c + cap.c + ) + +add_library(ncmlib ${NCMLIB_SRCS}) \ No newline at end of file diff --git a/ncmlib/chroot.c b/ncmlib/chroot.c new file mode 100644 index 0000000..4add5e3 --- /dev/null +++ b/ncmlib/chroot.c @@ -0,0 +1,124 @@ +/* chroot.c - chroots ndyndns jobs + * Time-stamp: <2010-11-03 05:23:56 njk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include "defines.h" +#include "log.h" +#include "strl.h" + +static char chrootd[MAX_PATH_LENGTH] = "\0"; +static char chroot_modified; +static char chroot_enable = 1; + +void disable_chroot(void) +{ + chroot_enable = 0; +} + +int chroot_enabled(void) +{ + return chroot_enable; +} + +void update_chroot(const char *path) +{ + strlcpy(chrootd, path, sizeof chrootd); + chroot_modified = 1; +} + +int chroot_exists(void) +{ + return chroot_modified; +} + +char *get_chroot(void) +{ + return chrootd; +} + +void wipe_chroot(void) +{ + memset(chrootd, '\0', sizeof chrootd); +} + +void imprison(const char *path) +{ + int ret; + + if (path == NULL) + return; + + ret = chdir(path); + if (ret) { + log_line("Failed to chdir(%s). Not invoking job.", path); + exit(EXIT_FAILURE); + } + + if (chroot_enable) { + ret = chroot(path); + if (ret) { + log_line("Failed to chroot(%s). Not invoking job.", path); + exit(EXIT_FAILURE); + } + } +} + +void drop_root(uid_t uid, gid_t gid) +{ + if (uid == 0 || gid == 0) { + log_line("FATAL - drop_root: attempt to drop root to root?\n"); + exit(EXIT_FAILURE); + } + + if (getgid() == 0) { + if (setregid(gid, gid) == -1) { + log_line("FATAL - drop_root: failed to drop real gid == root!\n"); + exit(EXIT_FAILURE); + } + } + + if (getuid() == 0) { + if (setreuid(uid, uid) == -1) { + log_line("FATAL - drop_root: failed to drop real uid == root!\n"); + exit(EXIT_FAILURE); + } + } + + /* be absolutely sure */ + if (getgid() == 0 || getuid() == 0) { + log_line("FATAL - drop_root: tried to drop root, but still have root!\n"); + exit(EXIT_FAILURE); + } +} + diff --git a/ncmlib/chroot.h b/ncmlib/chroot.h new file mode 100644 index 0000000..b859f86 --- /dev/null +++ b/ncmlib/chroot.h @@ -0,0 +1,43 @@ +/* chroot.h - include file for chroot.c + * Time-stamp: <2010-11-03 05:24:09 njk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_CHROOT_H_ +#define NCM_CHROOT_H_ + +void disable_chroot(void); +int chroot_enabled(void); +void update_chroot(const char *path); +char *get_chroot(void); +int chroot_exists(void); +void wipe_chroot(void); +void imprison(const char *path); +void drop_root(uid_t uid, gid_t gid); + +#endif + diff --git a/ncmlib/defines.h b/ncmlib/defines.h new file mode 100644 index 0000000..a567f39 --- /dev/null +++ b/ncmlib/defines.h @@ -0,0 +1,38 @@ +/* defines.h - general #defines + * Time-stamp: <2010-11-02 22:27:10 njk> + * + * (c) 2004-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_DEFINES_H_ +#define NCM_DEFINES_H_ + +#define DEFAULT_PATH "/bin:/usr/bin:/usr/local/bin" +#define MAXLINE 1024 +#define MAX_ARGS 30 +#define MAX_PATH_LENGTH 1024 + +#endif diff --git a/ncmlib/log.c b/ncmlib/log.c new file mode 100644 index 0000000..1bef2d0 --- /dev/null +++ b/ncmlib/log.c @@ -0,0 +1,87 @@ +/* log.c - simple logging support + * Time-stamp: <2010-11-12 05:19:46 njk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +/* global logging flags */ +int gflags_quiet = 0; +int gflags_detach = 1; +char *gflags_log_name = NULL; + +void log_line_l(int level, const char *format, ...) +{ + va_list argp; + + if (format == NULL || gflags_quiet) + return; + + if (gflags_detach) { + openlog(gflags_log_name, LOG_PID, LOG_DAEMON); + va_start(argp, format); + vsyslog(level | LOG_DAEMON, format, argp); + va_end(argp); + closelog(); + } else { + va_start(argp, format); + vfprintf(stderr, format, argp); + fprintf(stderr, "\n"); + va_end(argp); + } + closelog(); +} + +void suicide(const char *format, ...) +{ + va_list argp; + + if (format == NULL || gflags_quiet) + goto out; + + if (gflags_detach) { + openlog(gflags_log_name, LOG_PID, LOG_DAEMON); + va_start(argp, format); + vsyslog(LOG_ERR | LOG_DAEMON, format, argp); + va_end(argp); + closelog(); + } else { + va_start(argp, format); + vfprintf(stderr, format, argp); + va_end(argp); + fprintf(stderr, "\n"); + perror(NULL); + } + closelog(); +out: + exit(EXIT_FAILURE); +} + diff --git a/ncmlib/log.h b/ncmlib/log.h new file mode 100644 index 0000000..fc155f3 --- /dev/null +++ b/ncmlib/log.h @@ -0,0 +1,47 @@ +/* log.h - simple logging support + * Time-stamp: <2010-11-12 05:25:04 njk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_LOG_H_ +#define NCM_LOG_H_ 1 + +#include + +extern int gflags_quiet; +extern int gflags_detach; +extern char *gflags_log_name; + +#define log_line(...) log_line_l(LOG_INFO, __VA_ARGS__) +#define log_warning(...) log_line_l(LOG_WARNING, __VA_ARGS__) +#define log_error(...) log_line_l(LOG_ERR, __VA_ARGS__) + +void log_line_l(int level, const char *format, ...); +void suicide(const char *format, ...); + +#endif + diff --git a/ncmlib/malloc.c b/ncmlib/malloc.c new file mode 100644 index 0000000..e598527 --- /dev/null +++ b/ncmlib/malloc.c @@ -0,0 +1,51 @@ +/* + * malloc.c - memory allocation functions + * Time-stamp: <2010-11-02 03:17:31 nk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "log.h" + +void *xmalloc(size_t size) { + void *ret; + + ret = malloc(size); + if (ret == NULL) + suicide("FATAL - malloc() failed\n"); + return ret; +} + +void *xrealloc(void *ptr, size_t size) +{ + void *ret; + + ret = realloc(ptr, size); + if (size && ret == NULL) + suicide("FATAL - realloc() failed\n"); + return ret; +} diff --git a/ncmlib/malloc.h b/ncmlib/malloc.h new file mode 100644 index 0000000..d82b7f9 --- /dev/null +++ b/ncmlib/malloc.h @@ -0,0 +1,37 @@ +/* + * malloc.h - memory allocation functions + * Time-stamp: <2010-11-02 03:17:44 nk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_MALLOC_H_ +#define NCM_MALLOC_H_ + +void *xmalloc(size_t size); +void *xrealloc(void *ptr, size_t size); + +#endif diff --git a/ncmlib/pidfile.c b/ncmlib/pidfile.c new file mode 100644 index 0000000..ff88b8b --- /dev/null +++ b/ncmlib/pidfile.c @@ -0,0 +1,79 @@ +/* pidfile.c - process id file functions + * Time-stamp: <2010-11-03 05:19:23 nk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include "defines.h" +#include "log.h" + +void write_pid(const char *file) { + FILE *f; + size_t written, len; + char buf[MAXLINE]; + + f = fopen(file, "w"); + if (f == NULL) { + log_line("FATAL - failed to open pid file \"%s\"!\n", file); + exit(EXIT_FAILURE); + } + + snprintf(buf, sizeof buf - 1, "%i", (unsigned int)getpid()); + len = strlen(buf); + written = 0; + while (written < len) + written = fwrite(buf + written, sizeof (char), len - written, f); + + if (fclose(f) != 0) { + log_line("FATAL - failed to close pid file \"%s\"!\n", file); + exit(EXIT_FAILURE); + } +} + +/* Return 0 on success, -1 on failure. */ +int file_exists(const char *file, const char *mode) { + FILE *f; + + if (file == NULL || mode == NULL) { + log_line("file_exists: FATAL - coding bug: NULL passed\n"); + return -1; + } + + f = fopen(file, mode); + if (f == NULL) { + log_line("file_exists: FATAL - can't open file %s with mode %s!\n", + file, mode); + return -1; + } + fclose(f); + return 0; +} diff --git a/ncmlib/pidfile.h b/ncmlib/pidfile.h new file mode 100644 index 0000000..a9828a4 --- /dev/null +++ b/ncmlib/pidfile.h @@ -0,0 +1,37 @@ +/* pidfile.h - process id file functions + * Time-stamp: <2010-11-03 05:19:41 nk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_PIDFILE_H_ +#define NCM_PIDFILE_H_ 1 + +void write_pid(const char *file); +int file_exists(const char *file, const char *mode); + +#endif + diff --git a/ncmlib/signals.c b/ncmlib/signals.c new file mode 100644 index 0000000..dd8223d --- /dev/null +++ b/ncmlib/signals.c @@ -0,0 +1,59 @@ +/* signals.c - abstracts signal handling + * Time-stamp: <2010-11-01 17:25:41 nk> + * + * (c) 2004-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include "log.h" + +void hook_signal(int signum, void (*fn)(int), int flags) { + struct sigaction new_action; + + new_action.sa_handler = fn; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = flags; + + if (sigaction(signum, &new_action, NULL)) { + log_line("FATAL - failed to hook signal %i\n", signum); + exit(EXIT_FAILURE); + } +} + +void disable_signal(int signum) { + struct sigaction new_action; + + new_action.sa_handler = SIG_IGN; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = 0; + + if (sigaction(signum, &new_action, NULL)) { + log_line("FATAL - failed to ignore signal %i\n", signum); + exit(EXIT_FAILURE); + } +} diff --git a/ncmlib/signals.h b/ncmlib/signals.h new file mode 100644 index 0000000..9a0a876 --- /dev/null +++ b/ncmlib/signals.h @@ -0,0 +1,36 @@ +/* signals.h - abstracts signal handling + * Time-stamp: <2010-11-01 17:26:11 nk> + * + * (c) 2004-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_SIGNALS_H_ +#define NCM_SIGNALS_H_ 1 + +void hook_signal(int signum, void (*fn)(int), int flags); +void disable_signal(int signum); + +#endif diff --git a/ncmlib/strl.c b/ncmlib/strl.c new file mode 100644 index 0000000..3fd53c1 --- /dev/null +++ b/ncmlib/strl.c @@ -0,0 +1,60 @@ +/* strl.c - strlcpy/strlcat implementation + * Time-stamp: <2010-11-03 05:25:02 njk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "strl.h" + +#ifndef HAVE_STRLCPY +size_t strlcpy (char *dest, const char *src, size_t size) +{ + register unsigned int i = 0; + + if (size > 0) { + size--; + for (i=0; size > 0 && src[i] != '\0'; ++i, size--) + dest[i] = src[i]; + + dest[i] = '\0'; + } + while (src[i++]); + + return i; +} +#endif /* HAVE_STRLCPY */ + +#ifndef HAVE_STRLCAT +size_t strlcat (char *dest, const char *src, size_t size) +{ + register char *d = dest; + + for (; size > 0 && *d != '\0'; size--, d++); + return (d - dest) + strlcpy(d, src, size); +} +#endif /* HAVE_STRLCAT */ + diff --git a/ncmlib/strl.h b/ncmlib/strl.h new file mode 100644 index 0000000..8f114f1 --- /dev/null +++ b/ncmlib/strl.h @@ -0,0 +1,42 @@ +/* strl.h - header file for strlcpy/strlcat implementation + * Time-stamp: <2010-11-03 05:24:52 njk> + * + * (c) 2003-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_STRL_H_ +#define NCM_STRL_H_ 1 + +#ifndef HAVE_STRLCPY +size_t strlcpy (char *dest, const char *src, size_t size); +#endif /* HAVE_STRLCPY */ + +#ifndef HAVE_STRLCAT +size_t strlcat (char *dest, const char *src, size_t size); +#endif /* HAVE_STRLCAT */ + +#endif + diff --git a/ncmlib/strlist.c b/ncmlib/strlist.c new file mode 100644 index 0000000..e9d7ea4 --- /dev/null +++ b/ncmlib/strlist.c @@ -0,0 +1,108 @@ +/* strlist.c - string list functions + * Time-stamp: <2010-11-02 02:39:06 nk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "strl.h" +#include "malloc.h" +#include "strlist.h" + +void add_to_strlist(strlist_t **list, char *name) +{ + strlist_t *item, *t; + char *s; + unsigned int len; + + if (!list || !name) return; + + len = strlen(name) + 1; + if (len == 1) return; + s = xmalloc(len); + strlcpy(s, name, len); + + item = xmalloc(sizeof (strlist_t)); + item->str = s; + item->next = NULL; + + if (!*list) { + *list = item; + return; + } + + t = *list; + while (t) { + if (t->next == NULL) { + t->next = item; + return; + } + t = t->next; + } + + free(item); /* should be impossible, but hey */ + free(s); + return; +} + +void free_strlist(strlist_t *head) +{ + strlist_t *p = head, *q = NULL; + + while (p != NULL) { + free(p->str); + q = p; + p = q->next; + free(q); + } +} + +void free_stritem(strlist_t **p) +{ + strlist_t *q; + + if (!p) return; + if (!*p) return; + + q = (*p)->next; + free((*p)->str); + free(*p); + *p = q; +} + +int get_strlist_arity(strlist_t *list) +{ + int i; + strlist_t *c; + + for (c = list, i = 0; c != NULL; c = c->next, ++i); + return i; +} + + diff --git a/ncmlib/strlist.h b/ncmlib/strlist.h new file mode 100644 index 0000000..f30e4ba --- /dev/null +++ b/ncmlib/strlist.h @@ -0,0 +1,43 @@ +/* strlist.h - string list functions + * Time-stamp: <2010-11-02 02:39:23 nk> + * + * (c) 2005-2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_STRLIST_H_ +#define NCM_STRLIST_H_ + +typedef struct { + char *str; + void *next; +} strlist_t; + +void add_to_strlist(strlist_t **list, char *name); +void free_strlist(strlist_t *head); +void free_stritem(strlist_t **p); +int get_strlist_arity(strlist_t *list); + +#endif