freewtp/lib/capwap.c

94 lines
1.6 KiB
C
Raw Normal View History

2013-05-01 14:52:55 +02:00
#include "capwap.h"
/* Helper exit */
void capwap_exit(int errorcode) {
exit(errorcode);
}
/* Init randon generator */
void capwap_init_rand(void) {
srand(time(NULL));
}
/* Get random number */
int capwap_get_rand(int max) {
if ((max < 0) || (max > RAND_MAX)) {
max = RAND_MAX;
}
return (rand() % max);
}
/* Duplicate string */
char* capwap_duplicate_string(const char* source) {
char* clone;
ASSERT(source != NULL);
2013-12-20 23:14:34 +01:00
clone = capwap_alloc(strlen(source) + 1);
2013-05-01 14:52:55 +02:00
strcpy(clone, source);
2013-05-01 14:52:55 +02:00
return clone;
}
/* Buffer clone */
2014-01-18 19:10:27 +01:00
void* capwap_clone(const void* buffer, int buffersize) {
2013-05-01 14:52:55 +02:00
void* bufferclone;
ASSERT(buffer != NULL);
ASSERT(buffersize > 0);
bufferclone = capwap_alloc(buffersize);
return memcpy(bufferclone, buffer, buffersize);
}
2013-06-16 12:09:57 +02:00
/* */
void capwap_daemon(void) {
int fd;
pid_t pid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
capwap_exit(CAPWAP_DAEMON_ERROR);
} else if (pid > 0) {
capwap_exit(CAPWAP_SUCCESSFUL);
}
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
if (setsid() < 0) {
capwap_exit(CAPWAP_DAEMON_ERROR);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
capwap_exit(CAPWAP_DAEMON_ERROR);
}
/* Redirect the standard file descriptors to /dev/null */
fd = open("/dev/null", 0);
if (fd == -1) {
capwap_exit(CAPWAP_DAEMON_ERROR);
}
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
/* */
char* capwap_itoa(int input, char* output) {
sprintf(output, "%d", input);
return output;
}
/* */
char* capwap_ltoa(long input, char* output) {
sprintf(output, "%ld", input);
return output;
}