utils: add a function for checking if a process given by pid is still alive
[project/netifd.git] / utils.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include "utils.h"
17
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20
21 #ifdef __APPLE__
22 #include <libproc.h>
23 #endif
24
25 void
26 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
27 {
28 INIT_LIST_HEAD(&tree->list);
29 tree->version = 1;
30 tree->head_offset = offset;
31 }
32
33 void
34 vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
35 {
36 char *ptr;
37
38 list_del(&node->list);
39 ptr = (char *) node - tree->head_offset;
40 free(ptr);
41 }
42
43 void
44 vlist_simple_flush(struct vlist_simple_tree *tree)
45 {
46 struct vlist_simple_node *n, *tmp;
47
48 list_for_each_entry_safe(n, tmp, &tree->list, list) {
49 if ((n->version == tree->version || n->version == -1) &&
50 tree->version != -1)
51 continue;
52
53 vlist_simple_delete(tree, n);
54 }
55 }
56
57 void
58 vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
59 {
60 struct vlist_simple_node *n, *tmp;
61
62 vlist_simple_update(dest);
63 list_for_each_entry_safe(n, tmp, &old->list, list) {
64 list_del(&n->list);
65 vlist_simple_add(dest, n);
66 }
67 vlist_simple_flush(dest);
68 }
69
70 void
71 vlist_simple_flush_all(struct vlist_simple_tree *tree)
72 {
73 tree->version = -1;
74 vlist_simple_flush(tree);
75 }
76
77 unsigned int
78 parse_netmask_string(const char *str, bool v6)
79 {
80 struct in_addr addr;
81 unsigned int ret;
82 char *err = NULL;
83
84 if (!strchr(str, '.')) {
85 ret = strtoul(str, &err, 0);
86 if (err && *err)
87 goto error;
88
89 return ret;
90 }
91
92 if (v6)
93 goto error;
94
95 if (inet_aton(str, &addr) != 1)
96 goto error;
97
98 return 32 - fls(~(ntohl(addr.s_addr)));
99
100 error:
101 return ~0;
102 }
103
104 bool
105 split_netmask(char *str, unsigned int *netmask, bool v6)
106 {
107 char *delim = strchr(str, '/');
108
109 if (delim) {
110 *(delim++) = 0;
111
112 *netmask = parse_netmask_string(delim, v6);
113 }
114 return true;
115 }
116
117 int
118 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
119 {
120 char *astr = alloca(strlen(str) + 1);
121
122 strcpy(astr, str);
123 if (!split_netmask(astr, netmask, af == AF_INET6))
124 return 0;
125
126 if (af == AF_INET6) {
127 if (*netmask > 128)
128 return 0;
129 } else {
130 if (*netmask > 32)
131 return 0;
132 }
133
134 return inet_pton(af, astr, addr);
135 }
136
137 char *
138 format_macaddr(uint8_t *mac)
139 {
140 static char str[sizeof("ff:ff:ff:ff:ff:ff ")];
141
142 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
143 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
144
145 return str;
146 }
147
148 uint32_t
149 crc32_file(FILE *fp)
150 {
151 static uint32_t *crcvals = NULL;
152 if (!crcvals) {
153 crcvals = malloc(sizeof(*crcvals) * 256);
154
155 for (size_t i = 0; i < 256; ++i) {
156 uint32_t c = i;
157 for (size_t j = 0; j < 8; ++j)
158 c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
159 crcvals[i] = c;
160 }
161 }
162
163 uint8_t buf[1024];
164 size_t len;
165 uint32_t c = 0xFFFFFFFF;
166
167 do {
168 len = fread(buf, 1, sizeof(buf), fp);
169 for (size_t i = 0; i < len; ++i)
170 c = crcvals[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
171 } while (len == sizeof(buf));
172
173 return c ^ 0xFFFFFFFF;
174 }
175
176 bool check_pid_path(int pid, const char *exe)
177 {
178 int proc_exe_len;
179 int exe_len = strlen(exe);
180
181 #ifdef __APPLE__
182 char proc_exe_buf[PROC_PIDPATHINFO_SIZE];
183
184 proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf));
185 #else
186 char proc_exe[32];
187 char *proc_exe_buf = alloca(exe_len);
188
189 sprintf(proc_exe, "/proc/%d/exe", pid);
190 proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len);
191 #endif
192
193 if (proc_exe_len != exe_len)
194 return false;
195
196 return !memcmp(exe, proc_exe_buf, exe_len);
197 }