-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
156 lines (137 loc) · 3.42 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* iothnamed: a domain name server/forwarder/proxy for the ioth
* Copyright 2021 Renzo Davoli - Federico De Marchi
* Virtualsquare & University of Bologna
*
* utils.c: miscellaneous utility functions
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <syslog.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <sys/signal.h>
#include <errno.h>
#include <utils.h>
static int logok=0;
static char *progname;
static pid_t mypid;
static int leave;
void startlog(char *prog, int use_syslog) {
progname = prog;
if (use_syslog) {
openlog(progname, LOG_PID, 0);
printlog(LOG_INFO, "%s started", progname);
logok=1;
}
}
void printlog(int priority, const char *format, ...)
{
va_list arg;
va_start (arg, format);
if (logok)
vsyslog(priority, format, arg);
else {
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, format, arg);
fprintf(stderr, "\n");
}
va_end (arg);
}
int alive(void) {
return !leave;
}
static void terminate(int signum) {
pid_t pid = getpid();
if (pid == mypid) {
printlog(LOG_INFO, "(%d) leaving on signal %d", pid, signum);
leave = 1;
}
}
void setsignals(void) {
mypid = getpid();
struct sigaction action = {
.sa_handler = terminate
};
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
}
void save_pidfile(char *pidfile, char *cwd)
{
char pidfile_path[PATH_MAX];
if(pidfile[0] != '/')
snprintf(pidfile_path, PATH_MAX, "%s/%s", cwd, pidfile);
else
snprintf(pidfile_path, PATH_MAX, "%s", pidfile);
int fd = open(pidfile_path,
O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
FILE *f;
if(fd == -1) {
printlog(LOG_ERR, "Error in pidfile creation: %s", strerror(errno));
exit(1);
}
if((f = fdopen(fd, "w")) == NULL) {
printlog(LOG_ERR, "Error in FILE* construction: %s", strerror(errno));
exit(1);
}
if(fprintf(f, "%ld\n", (long int)getpid()) <= 0) {
printlog(LOG_ERR, "Error in writing pidfile");
exit(1);
}
fclose(f);
}
uint64_t simple_stringhash(const char *str) {
uint64_t hash = 5381;
for (; *str; str++)
hash = ((hash << 5) + hash) + *str; /* hash * 33 + c */
return hash;
}
void *memdup(const void *src, size_t n) {
void *dest;
if (src == NULL) return NULL;
dest = malloc(n);
if (dest != NULL)
memcpy(dest, src, n);
return dest;
}
void packetdump(FILE *f, void *arg,ssize_t len) {
unsigned char *buf=arg;
ssize_t lines=(len+15)>>4;
ssize_t line;
for (line=0; line<lines; line++) {
ssize_t i;
for (i=0; i<16; i++) {
int n=line<<4 | i;
if (n<len)
fprintf(f, "%02x ",buf[n]);
else
fprintf(f, " ");
}
fprintf(f, " | ");
for (i=0; i<16; i++) {
int n=line<<4 | i;
if (n<len)
fprintf(f, "%c", isprint(buf[n])?buf[n]:'.');
}
fprintf(f, "\n");
}
}