ncmlib should be tracked as an external repo.
This commit is contained in:
parent
3d6e3a2610
commit
6654ad1300
@ -1,3 +0,0 @@
|
||||
file(GLOB NCMLIB_SRCS "*.c")
|
||||
|
||||
add_library(ncmlib ${NCMLIB_SRCS})
|
63
ncmlib/cap.c
63
ncmlib/cap.c
@ -1,63 +0,0 @@
|
||||
/* cap.c - POSIX capability support
|
||||
* Time-stamp: <2010-11-12 09:01:07 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.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/capability.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
void set_cap(uid_t uid, gid_t gid, char *captxt)
|
||||
{
|
||||
cap_t caps;
|
||||
|
||||
if (!captxt)
|
||||
suicide("FATAL - set_cap: captxt == NULL");
|
||||
|
||||
if (prctl(PR_SET_KEEPCAPS, 1))
|
||||
suicide("FATAL - set_cap: prctl() failed");
|
||||
|
||||
if (setgroups(0, NULL) == -1)
|
||||
suicide("FATAL - set_cap: setgroups() failed");
|
||||
|
||||
if (setegid(gid) == -1 || seteuid(uid) == -1)
|
||||
suicide("FATAL - set_cap: seteuid() failed");
|
||||
|
||||
caps = cap_from_text(captxt);
|
||||
if (!caps)
|
||||
suicide("FATAL - set_cap: cap_from_text() failed");
|
||||
|
||||
if (cap_set_proc(caps) == -1)
|
||||
suicide("FATAL - set_cap: cap_set_proc() failed");
|
||||
|
||||
cap_free(caps);
|
||||
}
|
35
ncmlib/cap.h
35
ncmlib/cap.h
@ -1,35 +0,0 @@
|
||||
/* cap.h - POSIX capability support
|
||||
* Time-stamp: <2010-11-12 08:59:46 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_CAP_H
|
||||
#define NCM_CAP_H
|
||||
|
||||
void set_cap(uid_t uid, gid_t gid, char *captxt);
|
||||
|
||||
#endif /* NCM_CAP_H */
|
115
ncmlib/chroot.c
115
ncmlib/chroot.c
@ -1,115 +0,0 @@
|
||||
/* chroot.c - chroots jobs and drops privs
|
||||
*
|
||||
* (c) 2005-2013 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)
|
||||
{
|
||||
strnkcpy(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 *chroot_dir)
|
||||
{
|
||||
if (chdir(chroot_dir)) {
|
||||
log_line("Failed to chdir(%s)!", chroot_dir);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!chroot_enable)
|
||||
return;
|
||||
if (chroot(chroot_dir)) {
|
||||
log_line("Failed to chroot(%s)!", chroot_dir);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
/* 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
|
||||
|
@ -1,38 +0,0 @@
|
||||
/* 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
|
90
ncmlib/io.c
90
ncmlib/io.c
@ -1,90 +0,0 @@
|
||||
/* io.c - light wrappers for POSIX i/o functions
|
||||
* Time-stamp: <2011-06-10 13:51:03 njk>
|
||||
*
|
||||
* (c) 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 <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* returns -1 on error, >= 0 and equal to # chars read on success */
|
||||
int safe_read(int fd, char *buf, int len)
|
||||
{
|
||||
int r, s = 0;
|
||||
while (s < len) {
|
||||
r = read(fd, buf + s, len - s);
|
||||
if (r == 0)
|
||||
break;
|
||||
if (r == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else if ((errno == EAGAIN || errno == EWOULDBLOCK) && s > 0)
|
||||
return s;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
s += r;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/* returns -1 on error, >= 0 and equal to # chars written on success */
|
||||
int safe_write(int fd, const char *buf, int len)
|
||||
{
|
||||
int r, s = 0;
|
||||
while (s < len) {
|
||||
r = write(fd, buf + s, len - s);
|
||||
if (r == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
s += r;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/* returns -1 on error, >= 0 and equal to # chars written on success */
|
||||
int safe_sendto(int fd, const char *buf, int len, int flags,
|
||||
const struct sockaddr *dest_addr, socklen_t addrlen)
|
||||
{
|
||||
int r, s = 0;
|
||||
while (s < len) {
|
||||
r = sendto(fd, buf + s, len - s, flags, dest_addr, addrlen);
|
||||
if (r == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
s += r;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
40
ncmlib/io.h
40
ncmlib/io.h
@ -1,40 +0,0 @@
|
||||
/* io.h - light wrappers for POSIX i/o functions
|
||||
* Time-stamp: <2010-11-15 19:45:32 njk>
|
||||
*
|
||||
* (c) 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_IO_H_
|
||||
#define NCM_IO_H_
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
int safe_read(int fd, char *buf, int len);
|
||||
int safe_write(int fd, const char *buf, int len);
|
||||
int safe_sendto(int fd, const char *buf, int len, int flags,
|
||||
const struct sockaddr *dest_addr, socklen_t addrlen);
|
||||
|
||||
#endif /* NCM_IO_H_ */
|
86
ncmlib/log.c
86
ncmlib/log.c
@ -1,86 +0,0 @@
|
||||
/* 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;
|
||||
|
||||
#define log_syslog(level) do { \
|
||||
openlog(gflags_log_name, LOG_PID, LOG_DAEMON); \
|
||||
va_start(argp, format); \
|
||||
vsyslog(level | LOG_DAEMON, format, argp); \
|
||||
va_end(argp); \
|
||||
closelog(); } while(0)
|
||||
|
||||
#define log_stdio() do { \
|
||||
va_start(argp, format); \
|
||||
vfprintf(stderr, format, argp); \
|
||||
fprintf(stderr, "\n"); \
|
||||
va_end(argp); } while(0)
|
||||
|
||||
void log_line_l(int level, const char *format, ...)
|
||||
{
|
||||
va_list argp;
|
||||
|
||||
if (gflags_quiet)
|
||||
return;
|
||||
|
||||
if (gflags_detach)
|
||||
log_syslog(level);
|
||||
else
|
||||
log_stdio();
|
||||
}
|
||||
|
||||
void suicide(const char *format, ...)
|
||||
{
|
||||
va_list argp;
|
||||
|
||||
if (gflags_quiet)
|
||||
goto out;
|
||||
|
||||
if (gflags_detach)
|
||||
log_syslog(LOG_ERR);
|
||||
else {
|
||||
log_stdio();
|
||||
perror(NULL);
|
||||
}
|
||||
out:
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#undef log_syslog
|
||||
#undef log_stdio
|
||||
|
47
ncmlib/log.h
47
ncmlib/log.h
@ -1,47 +0,0 @@
|
||||
/* 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
|
||||
|
@ -1,51 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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
|
@ -1,79 +0,0 @@
|
||||
/* 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;
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/* 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
|
||||
|
@ -1,38 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "log.h"
|
||||
#include "io.h"
|
||||
|
||||
// Generate a 32-bit pseudorandom number using libc rand()
|
||||
uint32_t libc_random_u32(void)
|
||||
{
|
||||
static int initialized;
|
||||
if (initialized)
|
||||
return rand();
|
||||
|
||||
uint32_t seed;
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd != -1) {
|
||||
int r = safe_read(fd, (char *)&seed, sizeof seed);
|
||||
if (r == -1) {
|
||||
log_warning("Could not read /dev/urandom: %s", strerror(errno));
|
||||
close(fd);
|
||||
seed = time(0);
|
||||
}
|
||||
} else {
|
||||
log_warning("Could not open /dev/urandom: %s", strerror(errno));
|
||||
seed = time(0);
|
||||
}
|
||||
srand(seed);
|
||||
initialized = 1;
|
||||
return rand();
|
||||
}
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
#ifndef NCMLIB_RANDOM__
|
||||
#define NCMLIB_RANDOM__
|
||||
uint32_t libc_random_u32(void);
|
||||
|
||||
#endif
|
||||
|
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* seccomp example for x86 (32-bit and 64-bit) with BPF macros
|
||||
*
|
||||
* Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
|
||||
* Authors:
|
||||
* Will Drewry <wad@chromium.org>
|
||||
* Kees Cook <keescook@chromium.org>
|
||||
*
|
||||
* The code may be used by anyone for any purpose, and can serve as a
|
||||
* starting point for developing applications using mode 2 seccomp.
|
||||
*/
|
||||
#ifndef _SECCOMP_BPF_H_
|
||||
#define _SECCOMP_BPF_H_
|
||||
|
||||
#define _GNU_SOURCE 1
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/prctl.h>
|
||||
#ifndef PR_SET_NO_NEW_PRIVS
|
||||
# define PR_SET_NO_NEW_PRIVS 38
|
||||
#endif
|
||||
|
||||
#include <linux/unistd.h>
|
||||
#include <linux/audit.h>
|
||||
#include <linux/filter.h>
|
||||
#ifdef HAVE_LINUX_SECCOMP_H
|
||||
# include <linux/seccomp.h>
|
||||
#endif
|
||||
#ifndef SECCOMP_MODE_FILTER
|
||||
# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
|
||||
# define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */
|
||||
# define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
|
||||
# define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
|
||||
struct seccomp_data {
|
||||
int nr;
|
||||
__u32 arch;
|
||||
__u64 instruction_pointer;
|
||||
__u64 args[6];
|
||||
};
|
||||
#endif
|
||||
#ifndef SYS_SECCOMP
|
||||
# define SYS_SECCOMP 1
|
||||
#endif
|
||||
|
||||
#define syscall_nr (offsetof(struct seccomp_data, nr))
|
||||
#define arch_nr (offsetof(struct seccomp_data, arch))
|
||||
|
||||
#if defined(__i386__)
|
||||
# define REG_SYSCALL REG_EAX
|
||||
# define ARCH_NR AUDIT_ARCH_I386
|
||||
#elif defined(__x86_64__)
|
||||
# define REG_SYSCALL REG_RAX
|
||||
# define ARCH_NR AUDIT_ARCH_X86_64
|
||||
#else
|
||||
# warning "Platform does not support seccomp filter yet"
|
||||
# define REG_SYSCALL 0
|
||||
# define ARCH_NR 0
|
||||
#endif
|
||||
|
||||
#define VALIDATE_ARCHITECTURE \
|
||||
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, arch_nr), \
|
||||
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, 1, 0), \
|
||||
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
|
||||
|
||||
#define EXAMINE_SYSCALL \
|
||||
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr)
|
||||
|
||||
#define ALLOW_SYSCALL(name) \
|
||||
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##name, 0, 1), \
|
||||
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
|
||||
|
||||
#define KILL_PROCESS \
|
||||
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
|
||||
|
||||
#endif /* _SECCOMP_BPF_H_ */
|
@ -1,59 +0,0 @@
|
||||
/* 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);
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/* 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
|
@ -1,52 +0,0 @@
|
||||
/* strl.c - strnkcpy/strnkcat implementation
|
||||
*
|
||||
* (c) 2003-2013 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"
|
||||
|
||||
// true == truncated, false == no truncation
|
||||
bool strnkcpy (char *dest, const char *src, size_t size)
|
||||
{
|
||||
if (size > 0) {
|
||||
size_t i = 0;
|
||||
size--;
|
||||
for (; size > 0 && src[i] != '\0'; ++i, size--)
|
||||
dest[i] = src[i];
|
||||
dest[i] = '\0';
|
||||
return size ? false : true;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
|
||||
// true == truncated, false == no truncation
|
||||
bool strnkcat (char *dest, const char *src, size_t size)
|
||||
{
|
||||
for (; size > 0 && *dest != '\0'; size--, dest++);
|
||||
return strnkcpy(dest, src, size);
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
/* strl.h - strnkcpy/strnkcat implementation
|
||||
*
|
||||
* (c) 2003-2013 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
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool strnkcpy (char *dest, const char *src, size_t size);
|
||||
bool strnkcat (char *dest, const char *src, size_t size);
|
||||
|
||||
#endif
|
||||
|
108
ncmlib/strlist.c
108
ncmlib/strlist.c
@ -1,108 +0,0 @@
|
||||
/* 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);
|
||||
strnkcpy(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;
|
||||
}
|
||||
|
||||
|
@ -1,43 +0,0 @@
|
||||
/* 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
|
Loading…
Reference in New Issue
Block a user