Somehow not all of ncmlib was version controlled. Fix.

This commit is contained in:
Nicholas J. Kain 2010-11-12 18:46:45 -05:00
parent 5b7a3e43e0
commit 955031bce1
16 changed files with 903 additions and 0 deletions

12
ncmlib/CMakeLists.txt Normal file
View File

@ -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})

124
ncmlib/chroot.c Normal file
View File

@ -0,0 +1,124 @@
/* chroot.c - chroots ndyndns jobs
* Time-stamp: <2010-11-03 05:23:56 njk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#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);
}
}

43
ncmlib/chroot.h Normal file
View File

@ -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 <njkain at gmail dot com>
* 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

38
ncmlib/defines.h Normal file
View File

@ -0,0 +1,38 @@
/* defines.h - general #defines
* Time-stamp: <2010-11-02 22:27:10 njk>
*
* (c) 2004-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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

87
ncmlib/log.c Normal file
View File

@ -0,0 +1,87 @@
/* log.c - simple logging support
* Time-stamp: <2010-11-12 05:19:46 njk>
*
* (c) 2003-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdlib.h>
/* 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);
}

47
ncmlib/log.h Normal file
View File

@ -0,0 +1,47 @@
/* log.h - simple logging support
* Time-stamp: <2010-11-12 05:25:04 njk>
*
* (c) 2003-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <syslog.h>
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

51
ncmlib/malloc.c Normal file
View File

@ -0,0 +1,51 @@
/*
* malloc.c - memory allocation functions
* Time-stamp: <2010-11-02 03:17:31 nk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <stdlib.h>
#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;
}

37
ncmlib/malloc.h Normal file
View File

@ -0,0 +1,37 @@
/*
* malloc.h - memory allocation functions
* Time-stamp: <2010-11-02 03:17:44 nk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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

79
ncmlib/pidfile.c Normal file
View File

@ -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 <njkain at gmail dot com>
* 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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#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;
}

37
ncmlib/pidfile.h Normal file
View File

@ -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 <njkain at gmail dot com>
* 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

59
ncmlib/signals.c Normal file
View File

@ -0,0 +1,59 @@
/* signals.c - abstracts signal handling
* Time-stamp: <2010-11-01 17:25:41 nk>
*
* (c) 2004-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <unistd.h>
#include <signal.h>
#include <stdlib.h>
#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);
}
}

36
ncmlib/signals.h Normal file
View File

@ -0,0 +1,36 @@
/* signals.h - abstracts signal handling
* Time-stamp: <2010-11-01 17:26:11 nk>
*
* (c) 2004-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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

60
ncmlib/strl.c Normal file
View File

@ -0,0 +1,60 @@
/* strl.c - strlcpy/strlcat implementation
* Time-stamp: <2010-11-03 05:25:02 njk>
*
* (c) 2003-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <unistd.h>
#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 */

42
ncmlib/strl.h Normal file
View File

@ -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 <njkain at gmail dot com>
* 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

108
ncmlib/strlist.c Normal file
View File

@ -0,0 +1,108 @@
/* strlist.c - string list functions
* Time-stamp: <2010-11-02 02:39:06 nk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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 <unistd.h>
#include <stdlib.h>
#include <string.h>
#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;
}

43
ncmlib/strlist.h Normal file
View File

@ -0,0 +1,43 @@
/* strlist.h - string list functions
* Time-stamp: <2010-11-02 02:39:23 nk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* 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