2 * detail.c Process the detail file
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/detail.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/rad_assert.h>
32 #ifdef HAVE_SYS_STAT_H
44 #define USEC (1000000)
46 static FR_NAME_NUMBER state_names[] = {
47 { "unopened", STATE_UNOPENED },
48 { "unlocked", STATE_UNLOCKED },
49 { "header", STATE_HEADER },
50 { "reading", STATE_READING },
51 { "queued", STATE_QUEUED },
52 { "running", STATE_RUNNING },
53 { "no-reply", STATE_NO_REPLY },
54 { "replied", STATE_REPLIED },
61 * If we're limiting outstanding packets, then mark the response
64 int detail_send(rad_listen_t *listener, REQUEST *request)
66 #ifdef WITH_DETAIL_THREAD
69 listen_detail_t *data = listener->data;
71 rad_assert(request->listener == listener);
72 rad_assert(listener->send == detail_send);
75 * This request timed out. Remember that, and tell the
76 * caller it's OK to read more "detail" file stuff.
78 if (request->reply->code == 0) {
79 data->delay_time = data->retry_interval * USEC;
81 data->state = STATE_NO_REPLY;
83 RDEBUG("detail (%s): No response to request. Will retry in %d seconds",
84 data->name, data->retry_interval);
89 RDEBUG("detail (%s): Done %s packet.", data->name, fr_packet_codes[request->packet->code]);
92 * We call gettimeofday a lot. But it should be OK,
93 * because there's nothing else to do.
95 gettimeofday(&now, NULL);
98 * If we haven't sent a packet in the last second, reset
102 if (timercmp(&data->last_packet, &now, <)) {
103 data->has_rtt = false;
108 * Only one detail packet may be outstanding at a time,
109 * so it's safe to update some entries in the detail
112 * We keep smoothed round trip time (SRTT), but not round
113 * trip timeout (RTO). We use SRTT to calculate a rough
116 rtt = now.tv_sec - request->packet->timestamp.tv_sec;
119 rtt -= request->packet->timestamp.tv_usec;
122 * If we're proxying, the RTT is our processing time,
123 * plus the network delay there and back, plus the time
124 * on the other end to process the packet. Ideally, we
125 * should remove the network delays from the RTT, but we
126 * don't know what they are.
128 * So, to be safe, we over-estimate the total cost of
129 * processing the packet.
131 if (!data->has_rtt) {
132 data->has_rtt = true;
134 data->rttvar = rtt / 2;
137 data->rttvar -= data->rttvar >> 2;
138 data->rttvar += (data->srtt - rtt);
139 data->srtt -= data->srtt >> 3;
140 data->srtt += rtt >> 3;
144 * Calculate the time we wait before sending the next
147 * rtt / (rtt + delay) = load_factor / 100
149 data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
152 * Cap delay at no less than 4 packets/s. If the
153 * end system can't handle this, then it's very
156 if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
158 RDEBUG3("detail (%s): Received response for request %d. Will read the next packet in %d seconds",
159 data->name, request->number, data->delay_time / USEC);
161 data->last_packet = now;
163 data->state = STATE_REPLIED;
167 #ifdef WITH_DETAIL_THREAD
168 if (write(data->child_pipe[1], &c, 1) < 0) {
169 RERROR("detail (%s): Failed writing ack to reader thread: %s", data->name, fr_syserror(errno));
172 radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
180 * Open the detail file, if we can.
182 * FIXME: create it, if it's not already there, so that the main
183 * server select() will wake us up if there's anything to read.
185 static int detail_open(rad_listen_t *this)
188 listen_detail_t *data = this->data;
190 rad_assert(data->state == STATE_UNOPENED);
191 data->delay_time = USEC;
194 * Open detail.work first, so we don't lose
195 * accounting packets. It's probably better to
196 * duplicate them than to lose them.
198 * Note that we're not writing to the file, but
199 * we've got to open it for writing in order to
200 * establish the lock, to prevent rlm_detail from
203 * This also means that if we're doing globbing,
204 * this file will be read && processed before the
205 * file globbing is done.
208 data->work_fd = open(data->filename_work, O_RDWR);
211 * Couldn't open it for a reason OTHER than "it doesn't
212 * exist". Complain and tell the admin.
214 if ((data->work_fd < 0) && (errno != ENOENT)) {
215 ERROR("Failed opening detail file %s: %s",
216 data->filename_work, fr_syserror(errno));
221 * The file doesn't exist. Poll for it again.
223 if (data->work_fd < 0) {
230 char const *filename;
233 DEBUG2("detail (%s): Polling for detail file", data->name);
235 memset(&files, 0, sizeof(files));
236 if (glob(data->filename, 0, NULL, &files) != 0) {
243 * Loop over the glob'd files, looking for the
248 for (i = 0; i < files.gl_pathc; i++) {
249 if (stat(files.gl_pathv[i], &st) < 0) continue;
251 if ((i == 0) || (st.st_ctime < chtime)) {
252 chtime = st.st_ctime;
257 if (found < 0) goto noop;
260 * Rename detail to detail.work
262 filename = files.gl_pathv[found];
264 DEBUG("detail (%s): Renaming %s -> %s", data->name, filename, data->filename_work);
265 if (rename(filename, data->filename_work) < 0) {
266 ERROR("detail (%s): Failed renaming %s to %s: %s",
267 data->name, filename, data->filename_work, fr_syserror(errno));
271 globfree(&files); /* Shouldn't be using anything in files now */
274 * And try to open the filename.
276 data->work_fd = open(data->filename_work, O_RDWR);
277 if (data->work_fd < 0) {
278 ERROR("Failed opening detail file %s: %s",
279 data->filename_work, fr_syserror(errno));
283 } /* else detail.work existed, and we opened it */
285 rad_assert(data->vps == NULL);
286 rad_assert(data->fp == NULL);
288 data->state = STATE_UNLOCKED;
290 data->client_ip.af = AF_UNSPEC;
292 data->offset = data->last_offset = data->timestamp_offset = 0;
295 data->done_entry = false;
302 * FIXME: add a configuration "exit when done" so that the detail
303 * file reader can be used as a one-off tool to update stuff.
305 * The time sequence for reading from the detail file is:
307 * t_0 signalled that the server is idle, and we
308 * can read from the detail file.
310 * t_rtt the packet has been processed successfully,
311 * wait for t_delay to enforce load factor.
313 * t_rtt + t_delay wait for signal that the server is idle.
316 #ifndef WITH_DETAIL_THREAD
317 static RADIUS_PACKET *detail_poll(rad_listen_t *listener);
319 int detail_recv(rad_listen_t *listener)
321 RADIUS_PACKET *packet;
322 listen_detail_t *data = listener->data;
323 RAD_REQUEST_FUNP fun = NULL;
326 * We may be in the main thread. It needs to update the
327 * timers before we try to read from the file again.
329 if (data->signal) return 0;
331 packet = detail_poll(listener);
332 if (!packet) return -1;
334 if (DEBUG_ENABLED2) {
338 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
339 for (vp = fr_cursor_init(&cursor, &packet->vps);
341 vp = fr_cursor_next(&cursor)) {
346 switch (packet->code) {
347 case PW_CODE_ACCOUNTING_REQUEST:
348 fun = rad_accounting;
351 case PW_CODE_COA_REQUEST:
352 case PW_CODE_DISCONNECT_REQUEST:
358 data->state = STATE_REPLIED;
363 * Don't bother doing limit checks, etc.
365 if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
367 data->state = STATE_NO_REPLY; /* try again later */
374 int detail_recv(rad_listen_t *listener)
378 RADIUS_PACKET *packet;
379 listen_detail_t *data = listener->data;
380 RAD_REQUEST_FUNP fun = NULL;
383 * Block until there's a packet ready.
385 rcode = read(data->master_pipe[0], &packet, sizeof(packet));
386 if (rcode <= 0) return rcode;
388 if (DEBUG_ENABLED2) {
392 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
393 for (vp = fr_cursor_init(&cursor, &packet->vps);
395 vp = fr_cursor_next(&cursor)) {
399 rad_assert(packet != NULL);
401 switch (packet->code) {
402 case PW_CODE_ACCOUNTING_REQUEST:
403 fun = rad_accounting;
406 case PW_CODE_COA_REQUEST:
407 case PW_CODE_DISCONNECT_REQUEST:
412 data->state = STATE_REPLIED;
416 if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
417 data->state = STATE_NO_REPLY; /* try again later */
421 if (write(data->child_pipe[1], &c, 1) < 0) {
422 ERROR("detail (%s): Failed writing ack to reader thread: %s", data->name,
428 * Wait for the child thread to write an answer to the pipe
434 static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
437 char key[256], op[8], value[1024];
440 RADIUS_PACKET *packet;
442 listen_detail_t *data = listener->data;
444 switch (data->state) {
447 rad_assert(data->work_fd < 0);
449 if (!detail_open(listener)) return NULL;
451 rad_assert(data->state == STATE_UNLOCKED);
452 rad_assert(data->work_fd >= 0);
457 * Try to lock fd. If we can't, return.
458 * If we can, continue. This means that
459 * the server doesn't block while waiting
460 * for the lock to open...
464 * Note that we do NOT block waiting for
465 * the lock. We've re-named the file
466 * above, so we've already guaranteed
467 * that any *new* detail writer will not
468 * be opening this file. The only
469 * purpose of the lock is to catch a race
470 * condition where the execution
471 * "ping-pongs" between radiusd &
474 if (rad_lockfd_nonblock(data->work_fd, 0) < 0) {
476 * Close the FD. The main loop
477 * will wake up in a second and
480 close(data->work_fd);
483 data->state = STATE_UNOPENED;
488 * Only open for writing if we're
489 * marking requests as completed.
491 data->fp = fdopen(data->work_fd, data->track ? "r+" : "r");
493 ERROR("detail (%s): FATAL: Failed to re-open detail file: %s",
494 data->name, fr_syserror(errno));
499 * Look for the header
501 data->state = STATE_HEADER;
502 data->delay_time = USEC;
509 data->done_entry = false;
510 data->timestamp_offset = 0;
514 data->state = STATE_UNOPENED;
521 if (fstat(data->work_fd, &buf) < 0) {
522 ERROR("detail (%s): Failed to stat detail file: %s",
523 data->name, fr_syserror(errno));
527 if (((off_t) ftell(data->fp)) == buf.st_size) {
533 * End of file. Delete it, and re-set
536 if (feof(data->fp)) {
538 DEBUG("detail (%s): Unlinking %s", data->name, data->filename_work);
539 unlink(data->filename_work);
540 if (data->fp) fclose(data->fp);
543 data->state = STATE_UNOPENED;
544 rad_assert(data->vps == NULL);
546 if (data->one_shot) {
547 INFO("detail (%s): Finished reading \"one shot\" detail file - Exiting", data->name);
548 radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
555 * Else go read something.
557 if (!fgets(buffer, sizeof(buffer), data->fp)) {
558 DEBUG("detail (%s): Failed reading header from file - %s",
559 data->name, data->filename_work);
564 * Badly formatted file: delete it.
566 if (!strchr(buffer, '\n')) {
567 DEBUG("detail (%s): Invalid line without trailing LF - %s", data->name, buffer);
571 if (!sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
572 DEBUG("detail (%s): Failed reading detail file header in line - %s", data->name, buffer);
576 data->state = STATE_READING;
581 * Read more value-pair's, unless we're
582 * at EOF. In that case, queue whatever
586 rad_assert(data->fp != NULL);
588 fr_cursor_init(&cursor, &data->vps);
591 * Read a header, OR a value-pair.
593 while (fgets(buffer, sizeof(buffer), data->fp)) {
594 data->last_offset = data->offset;
595 data->offset = ftell(data->fp); /* for statistics */
598 * Badly formatted file: delete it.
600 if (!strchr(buffer, '\n')) {
601 WARN("detail (%s): Skipping line without trailing LF - %s", data->name, buffer);
602 fr_pair_list_free(&data->vps);
607 * We're reading VP's, and got a blank line.
608 * That indicates the end of an entry. Queue the
611 if (buffer[0] == '\n') {
612 data->state = STATE_QUEUED;
619 * We have a full "attribute = value" line.
620 * If it doesn't look reasonable, skip it.
622 * FIXME: print an error for badly formatted attributes?
624 if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
625 DEBUG("detail (%s): Skipping badly formatted line - %s", data->name, buffer);
630 * Should be =, :=, +=, ...
632 if (!strchr(op, '=')) {
633 DEBUG("detail (%s): Skipping line without operator - %s", data->name, buffer);
638 * Skip non-protocol attributes.
640 if (!strcasecmp(key, "Request-Authenticator")) continue;
643 * Set the original client IP address, based on
644 * what's in the detail file.
646 * Hmm... we don't set the server IP address.
649 if (!strcasecmp(key, "Client-IP-Address")) {
650 data->client_ip.af = AF_INET;
651 if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
652 DEBUG("detail (%s): Failed parsing Client-IP-Address", data->name);
653 fr_pair_list_free(&data->vps);
660 * The original time at which we received the
661 * packet. We need this to properly calculate
664 if (!strcasecmp(key, "Timestamp")) {
665 data->timestamp = atoi(value);
666 data->timestamp_offset = data->last_offset;
668 vp = fr_pair_afrom_num(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
670 vp->vp_date = (uint32_t) data->timestamp;
672 fr_cursor_insert(&cursor, vp);
677 if (!strcasecmp(key, "Donestamp")) {
678 data->timestamp = atoi(value);
679 data->done_entry = true;
683 DEBUG3("detail (%s): Trying to read VP from line - %s", data->name, buffer);
688 * FIXME: do we want to check for non-protocol
689 * attributes like radsqlrelay does?
692 if ((fr_pair_list_afrom_str(data, buffer, &vp) > 0) &&
694 fr_cursor_merge(&cursor, vp);
696 DEBUG("detail (%s): Failed reading VP from line - %s", data->name, buffer);
702 * The writer doesn't check that the
703 * record was completely written. If the
704 * disk is full, this can result in a
705 * truncated record which has no trailing
706 * blank line. When that happens, it's a
707 * bad record, and we ignore it.
709 if (feof(data->fp)) {
710 DEBUG("detail (%s): Truncated record: treating it as EOF for detail file %s",
711 data->name, data->filename_work);
712 fr_pair_list_free(&data->vps);
717 * Some kind of non-eof error.
719 * FIXME: Leave the file in-place, and warn the
722 DEBUG("detail (%s): Unknown error, deleting detail file %s",
723 data->name, data->filename_work);
730 * Periodically check what's going on.
731 * If the request is taking too long,
735 if (time(NULL) < (data->running + (int)data->retry_interval)) {
739 DEBUG("detail (%s): No response to detail request. Retrying", data->name);
743 * If there's no reply, keep
744 * retransmitting the current packet
748 data->state = STATE_QUEUED;
752 * We have a reply. Clean up the old
753 * request, and go read another one.
757 rad_assert(data->fp != NULL);
759 if (fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) {
760 DEBUG("detail (%s): Failed seeking to timestamp offset: %s",
761 data->name, fr_syserror(errno));
762 } else if (fwrite("\tDone", 1, 5, data->fp) < 5) {
763 DEBUG("detail (%s): Failed marking request as done: %s",
764 data->name, fr_syserror(errno));
765 } else if (fflush(data->fp) != 0) {
766 DEBUG("detail (%s): Failed flushing marked detail file to disk: %s",
767 data->name, fr_syserror(errno));
770 if (fseek(data->fp, data->offset, SEEK_SET) < 0) {
771 DEBUG("detail (%s): Failed seeking to next detail request: %s",
772 data->name, fr_syserror(errno));
776 fr_pair_list_free(&data->vps);
777 data->state = STATE_HEADER;
782 * Process the packet.
785 if (data->done_entry) {
786 DEBUG2("detail (%s): Skipping record for timestamp %lu", data->name, data->timestamp);
787 fr_pair_list_free(&data->vps);
788 data->state = STATE_HEADER;
795 * We're done reading the file, but we didn't read
796 * anything. Clean up, and don't return anything.
799 WARN("detail (%s): Read empty packet from file %s",
800 data->name, data->filename_work);
801 data->state = STATE_HEADER;
806 * Allocate the packet. If we fail, it's a serious
809 packet = rad_alloc(NULL, true);
811 ERROR("detail (%s): FATAL: Failed allocating memory for detail", data->name);
815 memset(packet, 0, sizeof(*packet));
817 packet->src_ipaddr.af = AF_INET;
818 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
821 * If everything's OK, this is a waste of memory.
822 * Otherwise, it lets us re-send the original packet
823 * contents, unmolested.
825 packet->vps = fr_pair_list_copy(packet, data->vps);
827 packet->code = PW_CODE_ACCOUNTING_REQUEST;
828 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
829 if (vp) packet->code = vp->vp_integer;
831 gettimeofday(&packet->timestamp, NULL);
834 * Remember where it came from, so that we don't
835 * proxy it to the place it came from...
837 if (data->client_ip.af != AF_UNSPEC) {
838 packet->src_ipaddr = data->client_ip;
841 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
843 packet->src_ipaddr.af = AF_INET;
844 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
845 packet->src_ipaddr.prefix = 32;
847 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
849 packet->src_ipaddr.af = AF_INET6;
850 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
851 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
852 packet->src_ipaddr.prefix = 128;
856 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
858 packet->dst_ipaddr.af = AF_INET;
859 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
860 packet->dst_ipaddr.prefix = 32;
862 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
864 packet->dst_ipaddr.af = AF_INET6;
865 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
866 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
867 packet->dst_ipaddr.prefix = 128;
872 * Generate packet ID, ports, IP via a counter.
874 packet->id = data->counter & 0xff;
875 packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
876 packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
878 packet->dst_ipaddr.af = AF_INET;
879 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
882 * Create / update accounting attributes.
884 if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
886 * Prefer the Event-Timestamp in the packet, if it
887 * exists. That is when the event occurred, whereas the
888 * "Timestamp" field is when we wrote the packet to the
889 * detail file, which could have been much later.
891 vp = fr_pair_find_by_num(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
893 data->timestamp = vp->vp_integer;
897 * Look for Acct-Delay-Time, and update
898 * based on Acct-Delay-Time += (time(NULL) - timestamp)
900 vp = fr_pair_find_by_num(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
902 vp = fr_pair_afrom_num(packet, PW_ACCT_DELAY_TIME, 0);
903 rad_assert(vp != NULL);
904 fr_pair_add(&packet->vps, vp);
906 if (data->timestamp != 0) {
907 vp->vp_integer += time(NULL) - data->timestamp;
912 * Set the transmission count.
914 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
916 vp = fr_pair_afrom_num(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
917 rad_assert(vp != NULL);
918 fr_pair_add(&packet->vps, vp);
920 vp->vp_integer = data->tries;
922 data->state = STATE_RUNNING;
923 data->running = packet->timestamp.tv_sec;
929 * Free detail-specific stuff.
931 void detail_free(rad_listen_t *this)
933 listen_detail_t *data = this->data;
935 #ifdef WITH_DETAIL_THREAD
941 * Mark the child pipes as unusable
943 close(data->child_pipe[0]);
944 close(data->child_pipe[1]);
945 data->child_pipe[0] = -1;
948 * Tell it to stop (interrupting its sleep)
950 pthread_kill(data->pthread_id, SIGTERM);
953 * Wait for it to acknowledge that it's stopped.
955 ret = read(data->master_pipe[0], &arg, sizeof(arg));
957 ERROR("detail (%s): Reader thread exited without informing the master: %s",
958 data->name, fr_syserror(errno));
959 } else if (ret != sizeof(arg)) {
960 ERROR("detail (%s): Invalid thread pointer received from reader thread during exit",
962 ERROR("detail (%s): Expected %zu bytes, got %zi bytes", data->name, sizeof(arg), ret);
965 close(data->master_pipe[0]);
966 close(data->master_pipe[1]);
968 if (arg) pthread_join(data->pthread_id, &arg);
972 if (data->fp != NULL) {
979 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
982 return snprintf(buffer, bufsize, "%s",
983 ((listen_detail_t *)(this->data))->filename);
986 return snprintf(buffer, bufsize, "detail file %s as server %s",
987 ((listen_detail_t *)(this->data))->filename,
993 * Delay while waiting for a file to be ready
995 static int detail_delay(listen_detail_t *data)
997 int delay = (data->poll_interval - 1) * USEC;
1000 * Add +/- 0.25s of jitter
1002 delay += (USEC * 3) / 4;
1003 delay += fr_rand() % (USEC / 2);
1005 DEBUG2("detail (%s): Detail listener state %s waiting %d.%06d sec",
1007 fr_int2str(state_names, data->state, "?"),
1008 (delay / USEC), delay % USEC);
1014 * Overloaded to return delay times.
1016 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1018 #ifdef WITH_DETAIL_THREAD
1021 listen_detail_t *data = this->data;
1024 * We haven't sent a packet... delay things a bit.
1026 if (!data->signal) return detail_delay(data);
1030 DEBUG2("detail (%s): Detail listener state %s signalled %d waiting %d.%06d sec",
1032 fr_int2str(state_names, data->state, "?"),
1034 data->delay_time / USEC,
1035 data->delay_time % USEC);
1037 return data->delay_time;
1042 * Overloaded to return "should we fix delay times"
1044 int detail_decode(rad_listen_t *this, REQUEST *request)
1046 #ifdef WITH_DETAIL_THREAD
1047 listen_detail_t *data = this->data;
1049 RDEBUG("Received %s from detail file %s",
1050 fr_packet_codes[request->packet->code], data->filename_work);
1052 rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1056 listen_detail_t *data = this->data;
1058 RDEBUG("Received %s from detail file %s",
1059 fr_packet_codes[request->packet->code], data->filename_work);
1061 rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1063 return data->signal;
1068 #ifdef WITH_DETAIL_THREAD
1069 static void *detail_handler_thread(void *arg)
1072 rad_listen_t *this = arg;
1073 listen_detail_t *data = this->data;
1076 RADIUS_PACKET *packet;
1078 while ((packet = detail_poll(this)) == NULL) {
1079 usleep(detail_delay(data));
1082 * If we're supposed to exit then tell
1083 * the master thread we've exited.
1085 if (data->child_pipe[0] < 0) {
1087 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1088 ERROR("detail (%s): Failed writing exit status to master: %s",
1089 data->name, fr_syserror(errno));
1096 * Keep retrying forever.
1098 * FIXME: cap the retries.
1101 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1102 ERROR("detail (%s): Failed passing detail packet pointer to master: %s",
1103 data->name, fr_syserror(errno));
1106 if (read(data->child_pipe[0], &c, 1) < 0) {
1107 ERROR("detail (%s): Failed getting detail packet ack from master: %s",
1108 data->name, fr_syserror(errno));
1112 if (data->delay_time > 0) usleep(data->delay_time);
1114 packet = detail_poll(this);
1116 } while (data->state != STATE_REPLIED);
1124 static const CONF_PARSER detail_config[] = {
1125 { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
1126 { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
1127 { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
1128 { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
1129 { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
1130 { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), "no" },
1131 { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), "no" },
1132 CONF_PARSER_TERMINATOR
1136 * Parse a detail section.
1138 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
1141 listen_detail_t *data;
1147 rcode = cf_section_parse(cs, data, detail_config);
1149 cf_log_err_cs(cs, "Failed parsing listen section");
1153 data->name = cf_section_name2(cs);
1154 if (!data->name) data->name = data->filename;
1157 * We don't do duplicate detection for "detail" sockets.
1160 this->synchronous = false;
1162 if (!data->filename) {
1163 cf_log_err_cs(cs, "No detail file specified in listen section");
1167 FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, >=, 1);
1168 FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, <=, 100);
1170 FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, >=, 1);
1171 FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, <=, 60);
1173 FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, >=, 4);
1174 FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, <=, 3600);
1177 * Only checking the config. Don't start threads or anything else.
1179 if (check_config) return 0;
1182 * If the filename is a glob, use "detail.work" as the
1185 if ((strchr(data->filename, '*') != NULL) ||
1186 (strchr(data->filename, '[') != NULL)) {
1190 WARN("detail (%s): File \"%s\" appears to use file globbing, but it is not supported on this system",
1191 data->name, data->filename);
1193 strlcpy(buffer, data->filename, sizeof(buffer));
1194 p = strrchr(buffer, FR_DIR_SEP);
1202 * Globbing cannot be done across directories.
1204 if ((strchr(buffer, '*') != NULL) ||
1205 (strchr(buffer, '[') != NULL)) {
1206 cf_log_err_cs(cs, "Wildcard directories are not supported");
1210 strlcat(buffer, "detail.work",
1211 sizeof(buffer) - strlen(buffer));
1214 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1217 data->filename_work = talloc_strdup(data, buffer);
1222 data->state = STATE_UNOPENED;
1223 data->delay_time = data->poll_interval * USEC;
1227 * Initialize the fake client.
1229 client = &data->detail_client;
1230 memset(client, 0, sizeof(*client));
1231 client->ipaddr.af = AF_INET;
1232 client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1233 client->ipaddr.prefix = 0;
1234 client->longname = client->shortname = data->filename;
1235 client->secret = client->shortname;
1236 client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1238 #ifdef WITH_DETAIL_THREAD
1240 * Create the communication pipes.
1242 if (pipe(data->master_pipe) < 0) {
1243 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1247 if (pipe(data->child_pipe) < 0) {
1248 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1252 pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1254 this->fd = data->master_pipe[0];