The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
proto_detail_file.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 7d4cd7b01e086211c12726c517d2471464b7c4cd $
19 * @file proto_detail_file.c
20 * @brief Detail handler for files
21 *
22 * @copyright 2017 The FreeRADIUS server project.
23 * @copyright 2017 Alan DeKok (aland@deployingradius.com)
24 */
25
26#include <freeradius-devel/io/application.h>
27#include <freeradius-devel/io/listen.h>
28#include <freeradius-devel/io/schedule.h>
29
30#include <freeradius-devel/server/main_config.h>
31
32#include <freeradius-devel/util/syserror.h>
33#include <freeradius-devel/util/misc.h>
34#include <freeradius-devel/util/file.h>
35
36#include "proto_detail.h"
37
38#include <netdb.h>
39
40#include <fcntl.h>
41#include <sys/stat.h>
42
43#ifdef HAVE_GLOB_H
44#include <glob.h>
45#else
46#error proto_detail_file requires <glob.h>
47#endif
48
49DIAG_OFF(unused-macros)
50#if 0
51/*
52 * When we want detailed debugging here, without detailed server
53 * debugging.
54 */
55#define MPRINT DEBUG
56#else
57#define MPRINT DEBUG3
58#endif
59DIAG_ON(unused-macros)
60
61/*
62 * For talloc names, ".name = detail_file", and dl.c prepends "proto_", and appends "_t".
63 */
65
66/*
67 * @todo - this should really now be a different data structure
68 */
70
71static void work_init(proto_detail_file_thread_t *thread, bool triggered_by_delete);
72static void mod_vnode_delete(fr_event_list_t *el, int fd, UNUSED int fflags, void *ctx);
73
76
77 { FR_CONF_OFFSET("poll_interval", proto_detail_file_t, poll_interval), .dflt = "5" },
78
79 { FR_CONF_OFFSET("immediate", proto_detail_file_t, immediate) },
80
81 { FR_CONF_OFFSET("create_dir", proto_detail_file_t, create_dir), .dflt = "yes" },
82
84};
85
86
87/*
88 * All of the decoding is done by proto_detail and proto_detail_work
89 */
90static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
91{
93
94 return inst->parent->work_io->decode(inst->parent->work_io_instance, request, data, data_len);
95}
96
97#if 0
99 UNUSED uint8_t *buffer, UNUSED size_t buffer_len, UNUSED size_t written)
100{
101#if 1
102 fr_assert(0);
103
104 return -1;
105#else
106 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
107
108 return thread->li->listen->app_io->write(thread->li, packet_ctx, request_time, buffer, buffer_len,
109 written);
110#endif
111}
112#endif
113
115{
116 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
117
118 bool has_worker = false;
119
120 pthread_mutex_lock(&thread->worker_mutex);
121 has_worker = (thread->num_workers != 0);
122 pthread_mutex_unlock(&thread->worker_mutex);
123
124 if (has_worker) return;
125
126 FR_TIMER_DISARM(thread->ev);
127
128 work_init(thread, false);
129}
130
131/** Open a detail listener
132 *
133 */
135{
138 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
139
140 if (inst->poll_interval == 0) {
141 int oflag;
142
143#ifdef O_EVTONLY
144 oflag = O_EVTONLY;
145#else
146 oflag = O_RDONLY;
147#endif
148 li->fd = thread->fd = open(inst->directory, oflag);
149 if (thread->fd < 0) {
150 if ((errno == ENOENT) && inst->create_dir) {
151 if (fr_mkdir(&thread->fd, inst->directory, -1, 0700, NULL, NULL) < 0) {
152 cf_log_err(inst->cs, "Failed creating %s: %s", inst->directory, fr_syserror(errno));
153 return -1;
154 } else {
155 li->fd = thread->fd;
156 goto opened;
157 }
158 }
159 cf_log_err(inst->cs, "Failed opening %s: %s", inst->directory, fr_syserror(errno));
160 return -1;
161 }
162 } else {
163 li->fd = thread->fd = -1;
164 li->listen->non_socket_listener = true;
165 li->listen->needs_full_setup = true;
166 }
167opened:
168 thread->inst = inst;
169 thread->name = talloc_typed_asprintf(thread, "detail_file which will read files matching %s", inst->filename);
170 thread->vnode_fd = -1;
171 pthread_mutex_init(&thread->worker_mutex, NULL);
172
173 li->listen->no_write_callback = true;
174
175 /*
176 * The I/O path for the work files. Each one read is a
177 * socket of this one listener.
178 */
179 MEM(thread->work_listen = fr_listen_alloc(thread, inst->parent->work_io_conf, inst->parent->server_cs,
180 inst->parent->self, inst->parent,
181 inst->parent->work_io, inst->parent->work_io_instance));
182
183 return 0;
184}
185
186/*
187 * The "detail.work" file doesn't exist. Let's see if we can rename one.
188 */
190{
191 proto_detail_file_t const *inst = thread->inst;
192 unsigned int i;
193 int found;
194 time_t chtime;
195 char const *filename;
196 glob_t files;
197 struct stat st;
198
199 DEBUG3("proto_detail (%s): polling for detail files in %s",
200 thread->name, inst->directory);
201
202 memset(&files, 0, sizeof(files));
203 if (glob(inst->filename, 0, NULL, &files) != 0) {
204 noop:
205 DEBUG3("proto_detail (%s): no matching files for %s",
206 thread->name, inst->filename);
207 globfree(&files);
208 return -1;
209 }
210
211 /*
212 * Loop over the glob'd files, looking for the
213 * oldest one.
214 */
215 chtime = 0;
216 found = -1;
217 for (i = 0; i < files.gl_pathc; i++) {
218 if (stat(files.gl_pathv[i], &st) < 0) continue;
219
220 if ((i == 0) || (st.st_ctime < chtime)) {
221 chtime = st.st_ctime;
222 found = i;
223 }
224 }
225
226 /*
227 * No matching files, reset the timer and continue.
228 */
229 if (found < 0) goto noop;
230
231 /*
232 * Rename detail to detail.work
233 */
234 filename = files.gl_pathv[found];
235
236 DEBUG("proto_detail (%s): Renaming %s -> %s", thread->name, filename, inst->filename_work);
237 if (rename(filename, inst->filename_work) < 0) {
238 ERROR("detail (%s): Failed renaming %s to %s: %s",
239 thread->name, filename, inst->filename_work, fr_syserror(errno));
240 goto noop;
241 }
242
243 globfree(&files); /* Shouldn't be using anything in files now */
244
245 /*
246 * The file should now exist, return the open'd FD.
247 */
248 return open(inst->filename_work, inst->mode);
249}
250
251/*
252 * Start polling again after a timeout.
253 */
254static void work_retry_timer(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
255{
256 proto_detail_file_thread_t *thread = talloc_get_type_abort(uctx, proto_detail_file_thread_t);
257
258 work_init(thread, false);
259}
260
261/*
262 * The "detail.work" file exists, and is open in the 'fd'.
263 */
264static int work_exists(proto_detail_file_thread_t *thread, int fd)
265{
266 proto_detail_file_t const *inst = thread->inst;
267 bool opened = false;
269 fr_listen_instance_t *li = NULL;
270 struct stat st;
271
273
274 DEBUG3("proto_detail (%s): Trying to lock %s", thread->name, inst->filename_work);
275
276 /*
277 * "detail.work" exists, try to lock it.
278 */
279 if (rad_lockfd_nonblock(fd, 0) < 0) {
280 fr_time_delta_t delay;
281
282 DEBUG3("proto_detail (%s): Failed locking %s: %s",
283 thread->name, inst->filename_work, fr_syserror(errno));
284
285 close(fd);
286
287 delay = thread->lock_interval;
288
289 /*
290 * Set the next interval, and ensure that we
291 * don't do massive busy-polling.
292 */
298 }
299
300 DEBUG3("proto_detail (%s): Waiting %.6fs for lock on file %s",
301 thread->name, fr_time_delta_to_sec_double(delay), inst->filename_work);
302
303 if (fr_timer_in(thread, thread->el->tl, &thread->ev, delay,
304 false, work_retry_timer, thread) < 0) {
305 ERROR("Failed inserting retry timer for %s", inst->filename_work);
306 }
307 return 0;
308 }
309
310 DEBUG3("proto_detail (%s): Obtained lock and starting to process file %s",
311 thread->name, inst->filename_work);
312
313 /*
314 * Ignore empty files.
315 */
316 if (fstat(fd, &st) < 0) {
317 ERROR("Failed opening %s: %s", inst->filename_work,
318 fr_syserror(errno));
319 unlink(inst->filename_work);
320 close(fd);
321 return 1;
322 }
323
324 if (!st.st_size) {
325 DEBUG3("proto_detail (%s): %s file is empty, ignoring it.",
326 thread->name, inst->filename_work);
327 unlink(inst->filename_work);
328 close(fd);
329 return 1;
330 }
331
332 /*
333 * This listener is allocated in a thread-specific
334 * context, so it doesn't need a destructor,
335 */
336 MEM(li = fr_listen_instance_alloc(NULL, thread->work_listen));
337
338 /*
339 * A socket of the work file listener, inserted into the
340 * scheduler. Shamelessly copied from proto_detail.c
341 * mod_open(), with changes.
342 *
343 * This socket is parented from the worker. So that
344 * when the worker goes away, so does the socket.
345 */
346 MEM(li->thread_instance = work = talloc_zero(li, proto_detail_work_thread_t));
347
348 work->inst = li->listen->app_io_instance;
349 work->file_parent = thread;
350 work->ev = NULL;
351
352 li->fd = work->fd = dup(fd);
353 if (work->fd < 0) {
354 DEBUG("proto_detail (%s): Failed opening %s: %s",
355 thread->name, inst->filename_work, fr_syserror(errno));
356
357 close(fd);
358 talloc_free(li);
359 return -1;
360 }
361
362 /*
363 * Don't do anything until the file has been deleted.
364 *
365 * @todo - ensure that proto_detail_work is done the file...
366 * maybe by creating a new instance?
367 */
368#ifdef __linux__
369 if (fr_event_file_watch_insert(thread, &thread->ew, thread->el, fd, inst->filename_work,
370 &funcs, thread) < 0) {
371#else
372 if (fr_event_filter_insert(thread, NULL, thread->el, fd, FR_EVENT_FILTER_VNODE,
373 &funcs, NULL, thread) < 0) {
374#endif
375 PERROR("Failed adding work socket to event loop");
376 close(li->fd);
377 close(fd);
378 talloc_free(li);
379 return -1;
380 }
381
382 /*
383 * Remember this for later.
384 */
385 thread->vnode_fd = fd;
386
387 /*
388 * For us, this is the worker listener.
389 * For the worker, this is it's own parent
390 */
391 thread->li = li;
392
393 work->filename_work = talloc_strdup(work, inst->filename_work);
394
395 /*
396 * Set configurable parameters for message ring buffer.
397 */
398 li->listen->default_message_size = inst->parent->max_packet_size;
399 li->listen->num_messages = inst->parent->num_messages;
400
401 /*
402 * Open the detail.work file.
403 */
404 if (li->listen->app_io->open(li) < 0) {
405 ERROR("Failed opening %s", li->listen->app_io->common.name);
406 goto error;
407 }
408 opened = true;
409
411 li->name = li->listen->app_io->get_name(li);
412
413 if (!fr_schedule_listen_add(inst->parent->sc, li)) {
414 error:
415#ifdef __linux__
416 if (fr_event_file_watch_delete(thread->ew) < 0) {
417#else
418 if (fr_event_fd_delete(thread->el, thread->vnode_fd, FR_EVENT_FILTER_VNODE) < 0) {
419#endif
420 PERROR("Failed removing DELETE callback when opening work file");
421 }
422 close(thread->vnode_fd);
423 thread->vnode_fd = -1;
424 thread->ew = NULL;
425
426 if (opened) {
427 (void) li->listen->app_io->close(li);
428 thread->li = NULL;
429 li = NULL;
430 } else {
431 close(li->fd);
432 }
433
434 talloc_free(li);
435 return -1;
436 }
437
438 /*
439 * Tell the worker to clean itself up.
440 */
441 work->li = li;
442
443 pthread_mutex_lock(&thread->worker_mutex);
444 thread->num_workers++;
445 pthread_mutex_unlock(&thread->worker_mutex);
446
447 return 0;
448}
449
450CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private fr_event_list_t trips --fsanitize=function*/
451static void mod_vnode_delete(LINUX_UNUSED fr_event_list_t *el, int fd, UNUSED int fflags, void *ctx)
452{
453 proto_detail_file_thread_t *thread = talloc_get_type_abort(ctx, proto_detail_file_thread_t);
454 proto_detail_file_t const *inst = thread->inst;
455
456 DEBUG("proto_detail (%s): Deleted %s", thread->name, inst->filename_work);
457
458 /*
459 * Silently ignore notifications from the directory. We
460 * didn't ask for them, but libkqueue delivers them to
461 * us.
462 */
463 if (fd == thread->fd) return;
464
465 if (fd != thread->vnode_fd) {
466 ERROR("Received DELETE for FD %d, when we were expecting one on FD %d - ignoring it",
467 fd, thread->vnode_fd);
468 return;
469 }
470
471#ifdef __linux__
472 if (fr_event_file_watch_delete(thread->ew) < 0) {
473#else
475#endif
476 PERROR("Failed removing DELETE callback after deletion");
477 }
478 close(fd);
479 thread->vnode_fd = -1;
480 thread->ew = NULL;
481
482 if (unlikely(fr_network_exiting(thread->nr))) return;
483
484 /*
485 * Re-initialize the state machine.
486 *
487 * Note that a "delete" may be the result of an atomic
488 * "move", which both deletes the old file, and creates
489 * the new one.
490 */
491 work_init(thread, true);
492}
493
494
495/** Start processing a new work file
496 *
497 * @param[in] thread the thread instance.
498 * @param[in] triggered_by_delete true if this was triggered by a vnode_delete.
499 * When a new file is moved over a workfile
500 * vnode delete can serve as an indication
501 * that new data is available.
502 * It can also mean however, that the file
503 * has just been deleted, so we shouldn't
504 * treat this failure to open the new file
505 * as a fatal error.
506 */
507static void work_init(proto_detail_file_thread_t *thread, bool triggered_by_delete)
508{
509 proto_detail_file_t const *inst = thread->inst;
510 int fd, rcode;
511 bool has_worker;
512
513 pthread_mutex_lock(&thread->worker_mutex);
514 has_worker = (thread->num_workers != 0);
515 pthread_mutex_unlock(&thread->worker_mutex);
516
517 /*
518 * The worker is still processing the file, poll until
519 * it's done.
520 */
521 if (has_worker) {
522 DEBUG3("proto_detail (%s): worker %s is still alive, waiting for it to finish.",
523 thread->name, inst->filename_work);
524 if (!inst->poll_interval) return;
525 goto delay;
526 }
527
528 /*
529 * It is possible for the "extend" event to fire before the "delete"
530 * in which case thread->vnode_fd is still populated, even though
531 * the worker has completed.
532 */
533 if (!inst->poll_interval && (thread->vnode_fd >= 0)) {
534 DEBUG3("proto_detail (%s): delete event for %s has not yet run", thread->name, inst->filename_work);
535 return;
536 }
537
538 fr_assert(thread->vnode_fd < 0);
539
540 /*
541 * See if there is a "detail.work" file. If not, try to
542 * rename an existing file to "detail.work".
543 */
544 DEBUG3("Trying to open %s", inst->filename_work);
545 fd = open(inst->filename_work, inst->mode);
546
547 /*
548 * If the work file didn't exist, try to rename detail* ->
549 * detail.work, and return the newly opened file.
550 */
551 if (fd < 0) {
552 if (errno != ENOENT) {
553 DEBUG("proto_detail (%s): Failed opening %s: %s",
554 thread->name, inst->filename_work,
555 fr_syserror(errno));
556 goto delay;
557 }
558
559retry:
560 fd = work_rename(thread);
561 }
562
563 /*
564 * The work file still doesn't exist. Go set up timers,
565 * or wait for an event which signals us that something
566 * in the directory changed.
567 */
568 if (fd < 0) {
569 /*
570 * Wait for the directory to change before
571 * looking for another "detail" file.
572 */
573 if (!inst->poll_interval) return;
574
575 /*
576 * File really was deleted, and didn't have
577 * anything moved over the top.
578 */
579 if (inst->immediate && triggered_by_delete) return;
580
581delay:
582 /*
583 * If we're processing one file and exiting, then the input file must exist.
584 */
585 if (inst->immediate && inst->parent->exit_when_done) {
586 ERROR("Input file does not exist");
587 fr_exit(EXIT_FAILURE);
588 }
589
590 /*
591 * Check every N seconds.
592 */
593 DEBUG3("Waiting %" PRIu32 ".000000s for new files in %s", inst->poll_interval ? inst->poll_interval : 1,
594 thread->name);
595
596 if (fr_timer_in(thread, thread->el->tl, &thread->ev,
597 fr_time_delta_from_sec(inst->poll_interval ? inst->poll_interval : 1),
598 false, work_retry_timer, thread) < 0) {
599 ERROR("Failed inserting poll timer for %s", inst->filename_work);
600 }
601 return;
602 }
603
605
606 /*
607 * It exists, go process it!
608 *
609 * We will get back to the main loop when the
610 * "detail.work" file is deleted.
611 */
612 rcode = work_exists(thread, fd);
613 if (rcode < 0) goto delay;
614
615 /*
616 * The file was empty, so we try to get another one.
617 */
618 if (rcode == 1) goto retry;
619
620 /*
621 * Otherwise the child is successfully processing the
622 * file.
623 */
624}
625
626
627/** Set the event list for a new IO instance
628 *
629 * @param[in] li the listener
630 * @param[in] el the event list
631 * @param[in] nr context from the network side
632 */
634{
637 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
638
639 thread->el = el;
640 thread->nr = nr;
641
642 if (inst->immediate) {
643 work_init(thread, false);
644 return;
645 }
646
647 /*
648 * Delay for a bit, before reading the detail files.
649 * This gives the server time to call
650 * rad_suid_down_permanent(), and for /proc/PID to
651 * therefore change permissions, so that libkqueue can
652 * read it.
653 */
654 if (fr_timer_in(thread, thread->el->tl, &thread->ev,
655 fr_time_delta_from_sec(1), false, work_retry_timer, thread) < 0) {
656 ERROR("Failed inserting poll timer for %s", inst->filename_work);
657 }
658}
659
660
661static char const *mod_name(fr_listen_instance_t *li)
662{
663 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
664
665 return thread->name;
666}
667
668static char const *mod_filename(fr_listen_instance_t *li)
669{
670 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
671
672 return thread->inst->directory;
673}
674
676static pthread_mutex_t detail_file_mutex = PTHREAD_MUTEX_INITIALIZER;
677
678/** Compare two thread instances based on node pointer
679 *
680 * @param[in] one First thread specific xlat expansion instance.
681 * @param[in] two Second thread specific xlat expansion instance.
682 * @return CMP(one, two)
683 */
684static fr_cmp_ret_t _detail_file_cmp(void const *one, void const *two)
685{
686 proto_detail_file_t const *a = one, *b = two;
687
688 return CMP(strcmp(a->filename, b->filename), 0);
689}
690
691/*
692 * Check for multiple readers on the same set of files.
693 */
694static int mod_instantiate(module_inst_ctx_t const *mctx)
695{
696 proto_detail_file_t *inst = talloc_get_type_abort(mctx->mi->data, proto_detail_file_t);
697 CONF_SECTION *conf = mctx->mi->conf, *work_cs;
698 CONF_PAIR *work_cp;
699
700 module_instance_t const *mi;
701 char *p;
702
703 /*
704 * Find the module_instance_t holding our instance data
705 * so we can find out what the parent of our instance
706 * was.
707 */
708 mi = mctx->mi;
709
710#ifndef __linux__
711 /*
712 * Linux inotify works. So we allow poll_interval==0
713 */
714 FR_INTEGER_BOUND_CHECK("poll_interval", inst->poll_interval, >=, 1);
715#endif
716 FR_INTEGER_BOUND_CHECK("poll_interval", inst->poll_interval, <=, 3600);
717
718 inst->parent = talloc_get_type_abort(mi->parent->data, proto_detail_t);
719 inst->cs = conf;
720
721 inst->directory = p = talloc_strdup(inst, inst->filename);
722
723 p = strrchr(p, '/');
724 if (!p) {
725 cf_log_err(conf, "Filename must contain '/'");
726 return -1;
727 }
728
729 *p = '\0';
730
731 /*
732 * Fetch work filename from "work" conf section
733 * These need to align for the proto_detail_file and proto_detail_work
734 * to behave correctly with each other.
735 */
736 work_cs = cf_section_find(cf_item_to_section(cf_parent(conf)), "work", NULL);
737 if (!work_cs) {
738 cf_log_err(cf_parent(conf), "Missing \"work\" section");
739 return -1;
740 }
741
742 work_cp = cf_pair_find(work_cs, "filename");
743 if (!work_cp) {
744 cf_log_err(work_cs, "Missing \"filename\"");
745 return -1;
746 }
747
748 inst->filename_work = cf_pair_value(work_cp);
749
750 /*
751 * We need this for the lock.
752 */
753 inst->mode = O_RDWR;
754
755 if (inst->parent->exit_when_done && !inst->immediate) {
756 cf_log_warn(conf, "Ignoring 'exit_when_done' due to 'immediate' flag not being set");
757 inst->parent->exit_when_done = false;
758 }
759
760 pthread_mutex_lock(&detail_file_mutex);
761 if (!detail_file_tree) {
763 if (!detail_file_tree) {
764 pthread_mutex_unlock(&detail_file_mutex);
765 cf_log_err(conf, "Failed initializing detail_file");
766 return -1;
767 }
768 }
769
772
773 fr_rb_find((void **)&old, detail_file_tree, inst);
774 fr_assert(old);
775
776 pthread_mutex_unlock(&detail_file_mutex);
777 cf_log_err(conf, "Duplicate detail listener %s", inst->filename);
778 cf_log_err(conf, "Original detail listener is in virtual server %s", cf_section_name2(old->parent->server_cs));
779 return -1;
780 }
781 pthread_mutex_unlock(&detail_file_mutex);
782
783 return 0;
784}
785
786
788{
791 proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t);
792
793 if (thread->nr) {
794 if (unlikely(fr_network_listen_delete(thread->nr, inst->parent->listen) < 0)) {
795 PERROR("Failed removing listener from network on detach");
796 }
797 }
798
799 /*
800 * Remove any timer event
801 */
802 if (thread->ev) (void) fr_timer_delete(&thread->ev);
803
804 /*
805 * @todo - have our OWN event loop for timers, and a
806 * "copy timer from -> to, which means we only have to
807 * delete our child event loop from the parent on close.
808 */
809 if (thread->fd >= 0) close(thread->fd);
810
811 if (thread->vnode_fd >= 0) {
812 if (thread->nr) {
813 if (unlikely(fr_network_listen_delete(thread->nr, inst->parent->listen) < 0)) {
814 PERROR("Failed removing listener from network on detach");
815 }
816 } else {
817#ifdef __linux__
818 if (fr_event_file_watch_delete(thread->ew) < 0) {
819#else
820 if (fr_event_fd_delete(thread->el, thread->vnode_fd, FR_EVENT_FILTER_VNODE) < 0) {
821#endif
822 PERROR("Failed removing DELETE callback on detach");
823 }
824 }
825 close(thread->vnode_fd);
826 thread->vnode_fd = -1;
827 }
828
829 pthread_mutex_destroy(&thread->worker_mutex);
830
831 return 0;
832}
833
834
835/** Private interface for use by proto_detail_file
836 *
837 */
840 .common = {
841 .magic = MODULE_MAGIC_INIT,
842 .name = "detail_file",
844 .inst_size = sizeof(proto_detail_file_t),
845 .thread_inst_size = sizeof(proto_detail_file_thread_t),
846 .instantiate = mod_instantiate,
847 },
848 .default_message_size = 65536,
849 .default_reply_size = 32,
850
851 .open = mod_open,
852 .close = mod_close,
853 .vnode = mod_vnode_extend,
854 .decode = mod_decode,
855// .write = mod_write,
856 .event_list_set = mod_event_list_set,
857 .get_name = mod_name,
858 .get_filename = mod_filename,
859};
static int const char char buffer[256]
Definition acutest.h:641
fr_io_close_t close
Close the transport.
Definition app_io.h:60
fr_io_open_t open
Open a new socket for listening, or accept/connect a new connection.
Definition app_io.h:43
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
fr_io_data_write_t write
Write from a data buffer to a socket.
Definition app_io.h:48
fr_io_name_t get_name
get the socket name
Definition app_io.h:70
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
#define DIAG_ON(_x)
Definition build.h:582
#define LINUX_UNUSED
Definition build.h:434
#define CC_NO_UBSAN(_sanitize)
Definition build.h:540
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:130
#define unlikely(_x)
Definition build.h:492
#define UNUSED
Definition build.h:415
#define DIAG_OFF(_x)
Definition build.h:581
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:572
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:466
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1405
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1247
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:671
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1640
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1758
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
#define cf_parent(_cf)
Definition cf_util.h:115
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:341
#define fr_exit(_x)
Exit, producing a log message in debug builds.
Definition debug.h:266
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define fr_event_filter_insert(...)
Definition event.h:238
@ FR_EVENT_FILTER_VNODE
Filter for vnode subfilters.
Definition event.h:88
fr_event_fd_cb_t delete
The file was deleted.
Definition event.h:200
Callbacks for the FR_EVENT_FILTER_VNODE filter.
Definition event.h:199
talloc_free(hp)
bool fr_network_exiting(fr_network_t const *nr)
Definition network.c:2198
int fr_network_listen_delete(fr_network_t *nr, fr_listen_instance_t *li)
Delete a socket from a network.
Definition network.c:231
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1244
Stores all information relating to an event list.
Definition event.c:415
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition file.c:218
fr_listen_t * fr_listen_alloc(TALLOC_CTX *ctx, CONF_SECTION *listen_cs, CONF_SECTION *server_cs, fr_app_t const *app, void const *app_instance, fr_app_io_t const *app_io, void const *app_io_instance)
Allocate a listener, and give it its id.
Definition listen.c:78
fr_listen_instance_t * fr_listen_instance_alloc(TALLOC_CTX *ctx, fr_listen_t *listen)
Allocate a socket of a listener.
Definition listen.c:111
size_t num_messages
for the message ring buffer
Definition listen.h:54
char const * name
printable name for this socket - set by open
Definition listen.h:68
fr_listen_t * listen
the listener this is a socket of.
Definition listen.h:65
bool needs_full_setup
Set to true to avoid the short cut when adding the listener.
Definition listen.h:45
bool no_write_callback
sometimes we don't need to do writes
Definition listen.h:43
int fd
file descriptor for this socket - set by open
Definition listen.h:67
fr_app_io_t const * app_io
I/O path functions.
Definition listen.h:33
void const * app_io_instance
I/O path configuration context.
Definition listen.h:34
void * thread_instance
thread / socket context
Definition listen.h:70
bool non_socket_listener
special internal listener that does not use sockets.
Definition listen.h:44
size_t default_message_size
copied from app_io, but may be changed
Definition listen.h:53
static ssize_t mod_write(fr_listen_instance_t *li, void *packet_ctx, fr_time_t request_time, uint8_t *buffer, size_t buffer_len, size_t written)
Definition master.c:2723
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
int rad_lockfd_nonblock(int fd, int lock_len)
Definition misc.c:129
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Detail master protocol handler.
proto_detail_t * parent
The module that spawned us!
proto_detail_work_thread_t * file_parent
thread instance of the directory reader that spawned us
fr_listen_t * work_listen
I/O path, every work file read is a socket of it.
char const * directory
containing the file below
fr_time_delta_t lock_interval
interval between trying the locks.
char const * filename
file name, usually with wildcards
CONF_SECTION * server_cs
server CS for this listener
char const * name
debug name for printing
fr_event_file_watch_t * ew
event for file deletes
int num_workers
number of workers
fr_timer_t * ev
for detail file timers.
fr_event_list_t * el
for various timers
pthread_mutex_t worker_mutex
for the workers
char const * filename_work
work file name
fr_listen_instance_t * li
talloc_parent() is slow
proto_detail_work_t const * inst
instance data
int fd
file descriptor
int vnode_fd
file descriptor for vnode_delete
fr_network_t * nr
for Linux-specific callbacks
static fr_cmp_ret_t _detail_file_cmp(void const *one, void const *two)
Compare two thread instances based on node pointer.
static const conf_parser_t file_listen_config[]
static void work_retry_timer(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
fr_app_io_t proto_detail_file
Private interface for use by proto_detail_file.
static void mod_vnode_extend(fr_listen_instance_t *li, UNUSED uint32_t fflags)
struct proto_detail_work_s proto_detail_file_t
static pthread_mutex_t detail_file_mutex
static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
static char const * mod_filename(fr_listen_instance_t *li)
static int mod_open(fr_listen_instance_t *li)
Open a detail listener.
static int work_rename(proto_detail_file_thread_t *thread)
static int mod_close(fr_listen_instance_t *li)
static void mod_event_list_set(fr_listen_instance_t *li, fr_event_list_t *el, void *nr)
Set the event list for a new IO instance.
static char const * mod_name(fr_listen_instance_t *li)
static fr_rb_tree_t * detail_file_tree
static int mod_instantiate(module_inst_ctx_t const *mctx)
static void work_init(proto_detail_file_thread_t *thread, bool triggered_by_delete)
Start processing a new work file.
static void mod_vnode_delete(fr_event_list_t *el, int fd, UNUSED int fflags, void *ctx)
static int work_exists(proto_detail_file_thread_t *thread, int fd)
#define fr_assert(_expr)
Definition rad_assert.h:37
static rs_t * conf
Definition radsniff.c:52
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
The main red black tree structure.
Definition rb.h:71
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_instance_t *li)
Add a fr_listen_instance_t to a scheduler.
Definition schedule.c:786
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:355
void * data
Module's instance data.
Definition module.h:297
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:363
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:207
Module instance data.
Definition module.h:291
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:621
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
Definition time.h:584
static fr_time_delta_t fr_time_delta_add(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:255
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_delta_wrap(_time)
Definition time.h:152
static double fr_time_delta_to_sec_double(fr_time_delta_t delta)
Seconds as a double, for printing and for arithmetic which need not be exact.
Definition time.h:663
static fr_time_delta_t fr_time_delta_div(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:267
#define fr_time_delta_gt(_a, _b)
Definition time.h:283
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
int fr_timer_delete(fr_timer_t **ev_p)
Delete a timer event and free its memory.
Definition timer.c:744
An event timer list.
Definition timer.c:65
#define fr_timer_in(...)
Definition timer.h:87
#define FR_TIMER_DISARM(_ev)
Definition timer.h:91
static fr_event_list_t * el
static fr_slen_t data
Definition value.h:1366