26#include <freeradius-devel/io/application.h>
27#include <freeradius-devel/io/listen.h>
28#include <freeradius-devel/io/schedule.h>
30#include <freeradius-devel/server/main_config.h>
32#include <freeradius-devel/util/syserror.h>
33#include <freeradius-devel/util/misc.h>
34#include <freeradius-devel/util/file.h>
46#error proto_detail_file requires <glob.h>
94 return inst->parent->work_io->decode(
inst->parent->work_io_instance,
request,
data, data_len);
118 bool has_worker =
false;
124 if (has_worker)
return;
140 if (
inst->poll_interval == 0) {
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) {
163 li->
fd = thread->
fd = -1;
181 inst->parent->work_io,
inst->parent->work_io_instance));
195 char const *filename;
199 DEBUG3(
"proto_detail (%s): polling for detail files in %s",
202 memset(&files, 0,
sizeof(files));
203 if (glob(
inst->filename, 0, NULL, &files) != 0) {
205 DEBUG3(
"proto_detail (%s): no matching files for %s",
217 for (i = 0; i < files.gl_pathc; i++) {
218 if (stat(files.gl_pathv[i], &st) < 0)
continue;
220 if ((i == 0) || (st.st_ctime < chtime)) {
221 chtime = st.st_ctime;
229 if (found < 0)
goto noop;
234 filename = files.gl_pathv[found];
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",
248 return open(
inst->filename_work,
inst->mode);
274 DEBUG3(
"proto_detail (%s): Trying to lock %s", thread->
name,
inst->filename_work);
282 DEBUG3(
"proto_detail (%s): Failed locking %s: %s",
300 DEBUG3(
"proto_detail (%s): Waiting %.6fs for lock on file %s",
305 ERROR(
"Failed inserting retry timer for %s",
inst->filename_work);
310 DEBUG3(
"proto_detail (%s): Obtained lock and starting to process file %s",
316 if (fstat(fd, &st) < 0) {
317 ERROR(
"Failed opening %s: %s",
inst->filename_work,
319 unlink(
inst->filename_work);
325 DEBUG3(
"proto_detail (%s): %s file is empty, ignoring it.",
327 unlink(
inst->filename_work);
352 li->
fd = work->
fd = dup(fd);
354 DEBUG(
"proto_detail (%s): Failed opening %s: %s",
369 if (fr_event_file_watch_insert(thread, &thread->
ew, thread->
el, fd,
inst->filename_work,
370 &funcs, thread) < 0) {
373 &funcs, NULL, thread) < 0) {
375 PERROR(
"Failed adding work socket to event loop");
416 if (fr_event_file_watch_delete(thread->
ew) < 0) {
420 PERROR(
"Failed removing DELETE callback when opening work file");
456 DEBUG(
"proto_detail (%s): Deleted %s", thread->
name,
inst->filename_work);
463 if (fd == thread->
fd)
return;
466 ERROR(
"Received DELETE for FD %d, when we were expecting one on FD %d - ignoring it",
472 if (fr_event_file_watch_delete(thread->
ew) < 0) {
476 PERROR(
"Failed removing DELETE callback after deletion");
522 DEBUG3(
"proto_detail (%s): worker %s is still alive, waiting for it to finish.",
524 if (!
inst->poll_interval)
return;
534 DEBUG3(
"proto_detail (%s): delete event for %s has not yet run", thread->
name,
inst->filename_work);
544 DEBUG3(
"Trying to open %s",
inst->filename_work);
545 fd = open(
inst->filename_work,
inst->mode);
552 if (errno != ENOENT) {
553 DEBUG(
"proto_detail (%s): Failed opening %s: %s",
573 if (!
inst->poll_interval)
return;
579 if (
inst->immediate && triggered_by_delete)
return;
585 if (
inst->immediate &&
inst->parent->exit_when_done) {
586 ERROR(
"Input file does not exist");
593 DEBUG3(
"Waiting %" PRIu32
".000000s for new files in %s",
inst->poll_interval ?
inst->poll_interval : 1,
599 ERROR(
"Failed inserting poll timer for %s",
inst->filename_work);
613 if (rcode < 0)
goto delay;
618 if (rcode == 1)
goto retry;
642 if (
inst->immediate) {
656 ERROR(
"Failed inserting poll timer for %s",
inst->filename_work);
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;
795 PERROR(
"Failed removing listener from network on detach");
809 if (thread->
fd >= 0) close(thread->
fd);
814 PERROR(
"Failed removing listener from network on detach");
818 if (fr_event_file_watch_delete(thread->
ew) < 0) {
822 PERROR(
"Failed removing DELETE callback on detach");
842 .name =
"detail_file",
848 .default_message_size = 65536,
849 .default_reply_size = 32,
static int const char char buffer[256]
fr_io_close_t close
Close the transport.
fr_io_open_t open
Open a new socket for listening, or accept/connect a new connection.
module_t common
Common fields to all loadable modules.
fr_io_data_write_t write
Write from a data buffer to a socket.
fr_io_name_t get_name
get the socket name
Public structure describing an I/O path for a protocol.
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
#define CC_NO_UBSAN(_sanitize)
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
fr_cmp_ret_t
Result of an ordering comparison.
#define CONF_PARSER_TERMINATOR
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
#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
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
#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
Defines a CONF_PAIR to C data type mapping.
Configuration AVP similar to a fr_pair_t.
A section grouping multiple CONF_PAIR.
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
#define cf_log_err(_cf, _fmt,...)
#define cf_log_warn(_cf, _fmt,...)
#define fr_exit(_x)
Exit, producing a log message in debug builds.
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
#define fr_event_filter_insert(...)
@ FR_EVENT_FILTER_VNODE
Filter for vnode subfilters.
fr_event_fd_cb_t delete
The file was deleted.
Callbacks for the FR_EVENT_FILTER_VNODE filter.
bool fr_network_exiting(fr_network_t const *nr)
int fr_network_listen_delete(fr_network_t *nr, fr_listen_instance_t *li)
Delete a socket from a network.
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.
Stores all information relating to an event list.
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.
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.
fr_listen_instance_t * fr_listen_instance_alloc(TALLOC_CTX *ctx, fr_listen_t *listen)
Allocate a socket of a listener.
size_t num_messages
for the message ring buffer
char const * name
printable name for this socket - set by open
fr_listen_t * listen
the listener this is a socket of.
bool needs_full_setup
Set to true to avoid the short cut when adding the listener.
bool no_write_callback
sometimes we don't need to do writes
int fd
file descriptor for this socket - set by open
fr_app_io_t const * app_io
I/O path functions.
void const * app_io_instance
I/O path configuration context.
void * thread_instance
thread / socket context
bool non_socket_listener
special internal listener that does not use sockets.
size_t default_message_size
copied from app_io, but may be changed
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)
int rad_lockfd_nonblock(int fd, int lock_len)
module_instance_t * mi
Instance of the module being instantiated.
Temporary structure to hold arguments for instantiation calls.
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 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)
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.
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
#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.
The main red black tree structure.
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.
CONF_SECTION * conf
Module's instance configuration.
void * data
Module's instance data.
module_instance_t const * parent
Parent module's instance (if any).
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
#define talloc_get_type_abort_const
#define talloc_strdup(_ctx, _str)
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
static fr_time_delta_t fr_time_delta_add(fr_time_delta_t a, fr_time_delta_t b)
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
#define fr_time_delta_wrap(_time)
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.
static fr_time_delta_t fr_time_delta_div(fr_time_delta_t a, fr_time_delta_t b)
#define fr_time_delta_gt(_a, _b)
A time delta, a difference in time measured in nanoseconds.
int fr_timer_delete(fr_timer_t **ev_p)
Delete a timer event and free its memory.
#define FR_TIMER_DISARM(_ev)
static fr_event_list_t * el