| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
#ifdef HAVE_INOTIFY |
| 19 |
|
| 20 |
#include <sys/inotify.h> |
| 21 |
#include <sys/param.h> /* For MAXSYMLINKS */ |
| 22 |
|
| 23 |
/* the strategy is to set an inotify on the directories containing |
| 24 |
resolv files, for any files in the directory which are close-write |
| 25 |
or moved into the directory. |
| 26 |
|
| 27 |
When either of those happen, we look to see if the file involved |
| 28 |
is actually a resolv-file, and if so, call poll-resolv with |
| 29 |
the "force" argument, to ensure it's read. |
| 30 |
|
| 31 |
This adds one new error condition: the directories containing |
| 32 |
all specified resolv-files must exist at start-up, even if the actual |
| 33 |
files don't. |
| 34 |
*/ |
| 35 |
|
| 36 |
static char *inotify_buffer; |
| 37 |
#define INOTIFY_SZ (sizeof(struct inotify_event) + NAME_MAX + 1) |
| 38 |
|
| 39 |
/* If path is a symbolic link, return the path it |
| 40 |
points to, made absolute if relative. |
| 41 |
If path doesn't exist or is not a symlink, return NULL. |
| 42 |
Return value is malloc'ed */ |
| 43 |
static char *my_readlink(int errfd, char *path) |
| 44 |
{ |
| 45 |
ssize_t rc, size = 64; |
| 46 |
char *buf; |
| 47 |
|
| 48 |
while (1) |
| 49 |
{ |
| 50 |
buf = safe_malloc(size); |
| 51 |
rc = readlink(path, buf, (size_t)size); |
| 52 |
|
| 53 |
if (rc == -1) |
| 54 |
{ |
| 55 |
/* Not link or doesn't exist. */ |
| 56 |
if (errno == EINVAL || errno == ENOENT) |
| 57 |
{ |
| 58 |
free(buf); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
send_event(errfd, EVENT_LINK_ERR, errno, path); |
| 64 |
_exit(0); |
| 65 |
} |
| 66 |
} |
| 67 |
else if (rc < size-1) |
| 68 |
{ |
| 69 |
char *d; |
| 70 |
|
| 71 |
buf[rc] = 0; |
| 72 |
if (buf[0] != '/' && ((d = strrchr(path, '/')))) |
| 73 |
{ |
| 74 |
/* Add path to relative link */ |
| 75 |
char *new_buf = safe_malloc((d - path) + strlen(buf) + 2); |
| 76 |
*(d+1) = 0; |
| 77 |
strcpy(new_buf, path); |
| 78 |
strcat(new_buf, buf); |
| 79 |
free(buf); |
| 80 |
buf = new_buf; |
| 81 |
} |
| 82 |
return buf; |
| 83 |
} |
| 84 |
|
| 85 |
/* Buffer too small, increase and retry */ |
| 86 |
size += 64; |
| 87 |
free(buf); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void inotify_dnsmasq_init(int errfd) |
| 92 |
{ |
| 93 |
struct resolvc *res; |
| 94 |
inotify_buffer = safe_malloc(INOTIFY_SZ); |
| 95 |
daemon->inotifyfd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 96 |
|
| 97 |
if (daemon->inotifyfd == -1) |
| 98 |
{ |
| 99 |
send_event(errfd, EVENT_INOTFY_ERR, errno, NULL); |
| 100 |
_exit(0); |
| 101 |
} |
| 102 |
|
| 103 |
if (daemon->port == 0 || option_bool(OPT_NO_RESOLV)) |
| 104 |
return; |
| 105 |
|
| 106 |
for (res = daemon->resolv_files; res; res = res->next) |
| 107 |
{ |
| 108 |
char *d, *new_path, *path = safe_malloc(strlen(res->name) + 1); |
| 109 |
int links = MAXSYMLINKS; |
| 110 |
|
| 111 |
strcpy(path, res->name); |
| 112 |
|
| 113 |
/* Follow symlinks until we reach a non-symlink, or a non-existent file. */ |
| 114 |
while ((new_path = my_readlink(errfd, path))) |
| 115 |
{ |
| 116 |
if (links-- == 0) |
| 117 |
{ |
| 118 |
send_event(errfd, EVENT_TMSL_ERR, 0, res->name); |
| 119 |
_exit(0); |
| 120 |
} |
| 121 |
free(path); |
| 122 |
path = new_path; |
| 123 |
} |
| 124 |
|
| 125 |
res->wd = -1; |
| 126 |
|
| 127 |
if ((d = strrchr(path, '/'))) |
| 128 |
{ |
| 129 |
*d = 0; /* make path just directory */ |
| 130 |
res->wd = inotify_add_watch(daemon->inotifyfd, path, IN_CLOSE_WRITE | IN_MOVED_TO); |
| 131 |
|
| 132 |
res->file = d+1; /* pointer to filename */ |
| 133 |
*d = '/'; |
| 134 |
|
| 135 |
if (res->wd == -1 && errno == ENOENT) |
| 136 |
{ |
| 137 |
send_event(errfd, EVENT_RESOLV_ERR, 0, res->name); |
| 138 |
_exit(0); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (res->wd == -1) |
| 143 |
{ |
| 144 |
send_event(errfd, EVENT_IFILE_ERR, errno, res->name); |
| 145 |
_exit(0); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static struct hostsfile *dyndir_addhosts(struct dyndir *dd, char *file) |
| 152 |
{ |
| 153 |
/* Check if this file is already known in dd->files */ |
| 154 |
struct hostsfile *ah; |
| 155 |
size_t dirlen = strlen(dd->dname); |
| 156 |
|
| 157 |
/* ah->fname always starts with the string in dd->dname */ |
| 158 |
for (ah = dd->files; ah; ah = ah->next) |
| 159 |
if (ah->fname[dirlen] == '/' && |
| 160 |
strcmp(&ah->fname[dirlen+1], file) == 0) |
| 161 |
return ah; |
| 162 |
|
| 163 |
/* Not known, create new hostsfile record for this dyndir */ |
| 164 |
if ((ah = whine_malloc(sizeof(struct hostsfile)))) |
| 165 |
{ |
| 166 |
char *path; |
| 167 |
|
| 168 |
if (!(path = whine_malloc(dirlen + strlen(file) + 2))) |
| 169 |
{ |
| 170 |
free(ah); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
strcpy(path, dd->dname); |
| 175 |
strcat(path, "/"); |
| 176 |
strcat(path, file); |
| 177 |
|
| 178 |
/* Add this file to the tip of the linked list */ |
| 179 |
ah->next = dd->files; |
| 180 |
dd->files = ah; |
| 181 |
|
| 182 |
/* Copy flags, set index and the full file path */ |
| 183 |
ah->flags = dd->flags; |
| 184 |
ah->index = daemon->host_index++; |
| 185 |
ah->fname = path; |
| 186 |
} |
| 187 |
|
| 188 |
return ah; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/* initialisation for dynamic-dir. Set inotify watch for each directory, and read pre-existing files */ |
| 193 |
void set_dynamic_inotify(int flag, int total_size, struct crec **rhash, int revhashsz) |
| 194 |
{ |
| 195 |
struct dyndir *dd; |
| 196 |
|
| 197 |
for (dd = daemon->dynamic_dirs; dd; dd = dd->next) |
| 198 |
{ |
| 199 |
DIR *dir_stream = NULL; |
| 200 |
struct dirent *ent; |
| 201 |
struct stat buf; |
| 202 |
|
| 203 |
if (!(dd->flags & flag)) |
| 204 |
continue; |
| 205 |
|
| 206 |
if (stat(dd->dname, &buf) == -1) |
| 207 |
{ |
| 208 |
my_syslog(LOG_ERR, _("bad dynamic directory %s: %s"), |
| 209 |
dd->dname, strerror(errno)); |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
if (!(S_ISDIR(buf.st_mode))) |
| 214 |
{ |
| 215 |
my_syslog(LOG_ERR, _("bad dynamic directory %s: %s"), |
| 216 |
dd->dname, _("not a directory")); |
| 217 |
continue; |
| 218 |
} |
| 219 |
|
| 220 |
if (!(dd->flags & AH_WD_DONE)) |
| 221 |
{ |
| 222 |
dd->wd = inotify_add_watch(daemon->inotifyfd, dd->dname, IN_CLOSE_WRITE | IN_MOVED_TO | IN_DELETE); |
| 223 |
dd->flags |= AH_WD_DONE; |
| 224 |
} |
| 225 |
|
| 226 |
/* Read contents of dir _after_ calling add_watch, in the hope of avoiding |
| 227 |
a race which misses files being added as we start */ |
| 228 |
if (dd->wd == -1 || !(dir_stream = opendir(dd->dname))) |
| 229 |
{ |
| 230 |
my_syslog(LOG_ERR, _("failed to create inotify for %s: %s"), |
| 231 |
dd->dname, strerror(errno)); |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
while ((ent = readdir(dir_stream))) |
| 236 |
{ |
| 237 |
size_t lenfile = strlen(ent->d_name); |
| 238 |
|
| 239 |
/* ignore emacs backups and dotfiles */ |
| 240 |
if (lenfile == 0 || |
| 241 |
ent->d_name[lenfile - 1] == '~' || |
| 242 |
(ent->d_name[0] == '#' && ent->d_name[lenfile - 1] == '#') || |
| 243 |
ent->d_name[0] == '.') |
| 244 |
continue; |
| 245 |
|
| 246 |
if (dd->flags & AH_HOSTS) |
| 247 |
{ |
| 248 |
struct hostsfile *ah; |
| 249 |
|
| 250 |
/* ignore non-regular files */ |
| 251 |
if ((ah = dyndir_addhosts(dd, ent->d_name)) && |
| 252 |
stat(ah->fname, &buf) != -1 && S_ISREG(buf.st_mode)) |
| 253 |
total_size = read_hostsfile(ah->fname, ah->index, total_size, rhash, revhashsz); |
| 254 |
} |
| 255 |
#ifdef HAVE_DHCP |
| 256 |
else if (dd->flags & (AH_DHCP_HST | AH_DHCP_OPT)) |
| 257 |
{ |
| 258 |
char *path; |
| 259 |
|
| 260 |
if ((path = whine_malloc(strlen(dd->dname) + lenfile + 2))) |
| 261 |
{ |
| 262 |
strcpy(path, dd->dname); |
| 263 |
strcat(path, "/"); |
| 264 |
strcat(path, ent->d_name); |
| 265 |
|
| 266 |
/* ignore non-regular files */ |
| 267 |
if (stat(path, &buf) != -1 && S_ISREG(buf.st_mode)) |
| 268 |
option_read_dynfile(path, dd->flags); |
| 269 |
|
| 270 |
free(path); |
| 271 |
} |
| 272 |
} |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
closedir(dir_stream); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
int inotify_check(time_t now) |
| 281 |
{ |
| 282 |
int hit = 0; |
| 283 |
struct dyndir *dd; |
| 284 |
|
| 285 |
(void)now; |
| 286 |
|
| 287 |
while (1) |
| 288 |
{ |
| 289 |
int rc; |
| 290 |
char *p; |
| 291 |
struct resolvc *res; |
| 292 |
struct inotify_event *in; |
| 293 |
|
| 294 |
while ((rc = read(daemon->inotifyfd, inotify_buffer, INOTIFY_SZ)) == -1 && errno == EINTR); |
| 295 |
|
| 296 |
if (rc <= 0) |
| 297 |
break; |
| 298 |
|
| 299 |
for (p = inotify_buffer; rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event); p += sizeof(struct inotify_event) + in->len) |
| 300 |
{ |
| 301 |
size_t namelen; |
| 302 |
|
| 303 |
in = (struct inotify_event*)p; |
| 304 |
|
| 305 |
/* ignore emacs backups and dotfiles */ |
| 306 |
if (in->len == 0 || (namelen = strlen(in->name)) == 0 || |
| 307 |
in->name[namelen - 1] == '~' || |
| 308 |
(in->name[0] == '#' && in->name[namelen - 1] == '#') || |
| 309 |
in->name[0] == '.') |
| 310 |
continue; |
| 311 |
|
| 312 |
for (res = daemon->resolv_files; res; res = res->next) |
| 313 |
if (res->wd == in->wd && strcmp(res->file, in->name) == 0) |
| 314 |
hit = 1; |
| 315 |
|
| 316 |
for (dd = daemon->dynamic_dirs; dd; dd = dd->next) |
| 317 |
if (dd->wd == in->wd) |
| 318 |
{ |
| 319 |
if (dd->flags & AH_HOSTS) |
| 320 |
{ |
| 321 |
struct hostsfile *ah; |
| 322 |
if ((ah = dyndir_addhosts(dd, in->name))) |
| 323 |
{ |
| 324 |
const unsigned int removed = cache_remove_uid(ah->index); |
| 325 |
|
| 326 |
/* Is this is a deletion event? */ |
| 327 |
if (in->mask & IN_DELETE) |
| 328 |
my_syslog(LOG_INFO, _("inotify: %s removed"), ah->fname); |
| 329 |
else |
| 330 |
my_syslog(LOG_INFO, _("inotify: %s new or modified"), ah->fname); |
| 331 |
|
| 332 |
if (removed > 0) |
| 333 |
my_syslog(LOG_INFO, _("inotify: flushed %u names read from %s"), removed, ah->fname); |
| 334 |
|
| 335 |
/* (Re-)load hostsfile only if this event isn't triggered by deletion */ |
| 336 |
if (!(in->mask & IN_DELETE)) |
| 337 |
read_hostsfile(ah->fname, ah->index, 0, NULL, 0); |
| 338 |
#ifdef HAVE_DHCP |
| 339 |
if (daemon->dhcp || daemon->doing_dhcp6) |
| 340 |
{ |
| 341 |
/* Propagate the consequences of loading a new dhcp-host */ |
| 342 |
dhcp_update_configs(daemon->dhcp_conf); |
| 343 |
lease_update_from_configs(); |
| 344 |
lease_update_file(now); |
| 345 |
lease_update_dns(1); |
| 346 |
} |
| 347 |
#endif |
| 348 |
} |
| 349 |
} |
| 350 |
#ifdef HAVE_DHCP |
| 351 |
else if (!(in->mask & IN_DELETE)) |
| 352 |
{ |
| 353 |
char *path; |
| 354 |
|
| 355 |
if ((path = whine_malloc(strlen(dd->dname) + in->len + 2))) |
| 356 |
{ |
| 357 |
strcpy(path, dd->dname); |
| 358 |
strcat(path, "/"); |
| 359 |
strcat(path, in->name); |
| 360 |
|
| 361 |
my_syslog(LOG_INFO, _("inotify: %s new or modified"), path); |
| 362 |
|
| 363 |
if ((dd->flags & AH_DHCP_HST) && option_read_dynfile(path, AH_DHCP_HST)) |
| 364 |
{ |
| 365 |
/* Propagate the consequences of loading a new dhcp-host */ |
| 366 |
dhcp_update_configs(daemon->dhcp_conf); |
| 367 |
lease_update_from_configs(); |
| 368 |
lease_update_file(now); |
| 369 |
lease_update_dns(1); |
| 370 |
} |
| 371 |
|
| 372 |
if (dd->flags & AH_DHCP_OPT) |
| 373 |
option_read_dynfile(path, AH_DHCP_OPT); |
| 374 |
|
| 375 |
free(path); |
| 376 |
} |
| 377 |
} |
| 378 |
#endif |
| 379 |
|
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return hit; |
| 385 |
} |
| 386 |
|
| 387 |
#endif /* INOTIFY */ |