Distinguish between single VP inserts, and list merges
[freeradius.git] / src / main / detail.c
1 /*
2  * detail.c     Process the detail file
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2007  The FreeRADIUS server project
21  * Copyright 2007  Alan DeKok <aland@deployingradius.com>
22  */
23
24 RCSID("$Id$")
25
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>
31
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 #ifdef HAVE_GLOB_H
37 #include <glob.h>
38 #endif
39
40 #include <fcntl.h>
41
42 #ifdef WITH_DETAIL
43
44 extern bool check_config;
45
46 #define USEC (1000000)
47
48 static FR_NAME_NUMBER state_names[] = {
49         { "unopened", STATE_UNOPENED },
50         { "unlocked", STATE_UNLOCKED },
51         { "header", STATE_HEADER },
52         { "reading", STATE_READING },
53         { "queued", STATE_QUEUED },
54         { "running", STATE_RUNNING },
55         { "no-reply", STATE_NO_REPLY },
56         { "replied", STATE_REPLIED },
57
58         { NULL, 0 }
59 };
60
61
62 /*
63  *      If we're limiting outstanding packets, then mark the response
64  *      as being sent.
65  */
66 int detail_send(rad_listen_t *listener, REQUEST *request)
67 {
68 #ifdef WITH_DETAIL_THREAD
69         char c = 0;
70 #endif
71         listen_detail_t *data = listener->data;
72
73         rad_assert(request->listener == listener);
74         rad_assert(listener->send == detail_send);
75
76         /*
77          *      This request timed out.  Remember that, and tell the
78          *      caller it's OK to read more "detail" file stuff.
79          */
80         if (request->reply->code == 0) {
81                 data->delay_time = data->retry_interval * USEC;
82                 data->signal = 1;
83                 data->state = STATE_NO_REPLY;
84
85                 RDEBUG("Detail - No response configured for request %d.  Will retry in %d seconds",
86                        request->number, data->retry_interval);
87         } else {
88                 int rtt;
89                 struct timeval now;
90                 /*
91                  *      We call gettimeofday a lot.  But it should be OK,
92                  *      because there's nothing else to do.
93                  */
94                 gettimeofday(&now, NULL);
95
96                 /*
97                  *      If we haven't sent a packet in the last second, reset
98                  *      the RTT.
99                  */
100                 now.tv_sec -= 1;
101                 if (timercmp(&data->last_packet, &now, <)) {
102                         data->has_rtt = false;
103                 }
104                 now.tv_sec += 1;
105
106                 /*
107                  *      Only one detail packet may be outstanding at a time,
108                  *      so it's safe to update some entries in the detail
109                  *      structure.
110                  *
111                  *      We keep smoothed round trip time (SRTT), but not round
112                  *      trip timeout (RTO).  We use SRTT to calculate a rough
113                  *      load factor.
114                  */
115                 rtt = now.tv_sec - request->packet->timestamp.tv_sec;
116                 rtt *= USEC;
117                 rtt += now.tv_usec;
118                 rtt -= request->packet->timestamp.tv_usec;
119
120                 /*
121                  *      If we're proxying, the RTT is our processing time,
122                  *      plus the network delay there and back, plus the time
123                  *      on the other end to process the packet.  Ideally, we
124                  *      should remove the network delays from the RTT, but we
125                  *      don't know what they are.
126                  *
127                  *      So, to be safe, we over-estimate the total cost of
128                  *      processing the packet.
129                  */
130                 if (!data->has_rtt) {
131                         data->has_rtt = true;
132                         data->srtt = rtt;
133                         data->rttvar = rtt / 2;
134
135                 } else {
136                         data->rttvar -= data->rttvar >> 2;
137                         data->rttvar += (data->srtt - rtt);
138                         data->srtt -= data->srtt >> 3;
139                         data->srtt += rtt >> 3;
140                 }
141
142                 /*
143                  *      Calculate the time we wait before sending the next
144                  *      packet.
145                  *
146                  *      rtt / (rtt + delay) = load_factor / 100
147                  */
148                 data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
149
150                 /*
151                  *      Cap delay at no less than 4 packets/s.  If the
152                  *      end system can't handle this, then it's very
153                  *      broken.
154                  */
155                 if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
156
157                 RDEBUG3("Received response for request %d.  Will read the next packet in %d seconds",
158                         request->number, data->delay_time / USEC);
159
160                 data->last_packet = now;
161                 data->signal = 1;
162                 data->state = STATE_REPLIED;
163                 data->counter++;
164         }
165
166 #ifdef WITH_DETAIL_THREAD
167         if (write(data->child_pipe[1], &c, 1) < 0) {
168                 ERROR("Failed writing ack to reader thread: %s", fr_syserror(errno));
169         }
170 #else
171         radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
172 #endif
173
174         return 0;
175 }
176
177
178 /*
179  *      Open the detail file, if we can.
180  *
181  *      FIXME: create it, if it's not already there, so that the main
182  *      server select() will wake us up if there's anything to read.
183  */
184 static int detail_open(rad_listen_t *this)
185 {
186         struct stat st;
187         listen_detail_t *data = this->data;
188
189         rad_assert(data->state == STATE_UNOPENED);
190         data->delay_time = USEC;
191
192         /*
193          *      Open detail.work first, so we don't lose
194          *      accounting packets.  It's probably better to
195          *      duplicate them than to lose them.
196          *
197          *      Note that we're not writing to the file, but
198          *      we've got to open it for writing in order to
199          *      establish the lock, to prevent rlm_detail from
200          *      writing to it.
201          *
202          *      This also means that if we're doing globbing,
203          *      this file will be read && processed before the
204          *      file globbing is done.
205          */
206         data->work_fd = open(data->filename_work, O_RDWR);
207         if (data->work_fd < 0) {
208 #ifndef HAVE_GLOB_H
209                 return 0;
210 #else
211                 unsigned int    i;
212                 int             found;
213                 time_t          chtime;
214                 char const      *filename;
215                 glob_t          files;
216
217                 DEBUG2("Polling for detail file %s", data->filename);
218
219                 memset(&files, 0, sizeof(files));
220                 if (glob(data->filename, 0, NULL, &files) != 0) {
221                 noop:
222                         globfree(&files);
223                         return 0;
224                 }
225
226                 /*
227                  *      Loop over the glob'd files, looking for the
228                  *      oldest one.
229                  */
230                 chtime = 0;
231                 found = -1;
232                 for (i = 0; i < files.gl_pathc; i++) {
233                         if (stat(files.gl_pathv[i], &st) < 0) continue;
234
235                         if ((i == 0) || (st.st_ctime < chtime)) {
236                                 chtime = st.st_ctime;
237                                 found = i;
238                         }
239                 }
240
241                 if (found < 0) goto noop;
242
243                 /*
244                  *      Rename detail to detail.work
245                  */
246                 filename = files.gl_pathv[found];
247
248                 DEBUG("Detail - Renaming %s -> %s", filename, data->filename_work);
249                 if (rename(filename, data->filename_work) < 0) {
250                         ERROR("Detail - Failed renaming %s to %s: %s",
251                               filename, data->filename_work, fr_syserror(errno));
252                         goto noop;
253                 }
254
255                 globfree(&files);       /* Shouldn't be using anything in files now */
256
257                 /*
258                  *      And try to open the filename.
259                  */
260                 data->work_fd = open(data->filename_work, O_RDWR);
261                 if (data->work_fd < 0) return 0;
262 #endif
263         } /* else detail.work existed, and we opened it */
264
265         rad_assert(data->vps == NULL);
266         rad_assert(data->fp == NULL);
267
268         data->state = STATE_UNLOCKED;
269
270         data->client_ip.af = AF_UNSPEC;
271         data->timestamp = 0;
272         data->offset = 0;
273         data->packets = 0;
274         data->tries = 0;
275
276         return 1;
277 }
278
279
280 /*
281  *      FIXME: add a configuration "exit when done" so that the detail
282  *      file reader can be used as a one-off tool to update stuff.
283  *
284  *      The time sequence for reading from the detail file is:
285  *
286  *      t_0             signalled that the server is idle, and we
287  *                      can read from the detail file.
288  *
289  *      t_rtt           the packet has been processed successfully,
290  *                      wait for t_delay to enforce load factor.
291  *
292  *      t_rtt + t_delay wait for signal that the server is idle.
293  *
294  */
295 #ifndef WITH_DETAIL_THREAD
296 static RADIUS_PACKET *detail_poll(rad_listen_t *listener);
297
298 int detail_recv(rad_listen_t *listener)
299 {
300         RADIUS_PACKET *packet;
301         listen_detail_t *data = listener->data;
302
303         /*
304          *      We may be in the main thread.  It needs to update the
305          *      timers before we try to read from the file again.
306          */
307         if (data->signal) return 0;
308
309         packet = detail_poll(listener);
310         if (!packet) return -1;
311
312         /*
313          *      Don't bother doing limit checks, etc.
314          */
315         if (!request_receive(listener, packet, &data->detail_client,
316                              rad_accounting)) {
317                 rad_free(&packet);
318                 data->state = STATE_NO_REPLY;   /* try again later */
319                 return 0;
320         }
321
322         return 1;
323 }
324 #else
325 int detail_recv(rad_listen_t *listener)
326 {
327         ssize_t rcode;
328         RADIUS_PACKET *packet;
329         listen_detail_t *data = listener->data;
330
331         /*
332          *      Block until there's a packet ready.
333          */
334         rcode = read(data->master_pipe[0], &packet, sizeof(packet));
335         if (rcode <= 0) return rcode;
336
337         rad_assert(packet != NULL);
338
339         if (!request_receive(listener, packet, &data->detail_client,
340                                      rad_accounting)) {
341                 char c = 0;
342                 rad_free(&packet);
343                 data->state = STATE_NO_REPLY;   /* try again later */
344                 if (write(data->child_pipe[1], &c, 1) < 0) {
345                         ERROR("Failed writing ack to reader thread: %s", fr_syserror(errno));
346                 }
347         }
348
349         /*
350          *      Wait for the child thread to write an answer to the pipe
351          */
352         return 0;
353 }
354 #endif
355
356 static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
357 {
358         char            key[256], op[8], value[1024];
359         vp_cursor_t     cursor;
360         VALUE_PAIR      *vp;
361         RADIUS_PACKET   *packet;
362         char            buffer[2048];
363         listen_detail_t *data = listener->data;
364
365         switch (data->state) {
366         case STATE_UNOPENED:
367 open_file:
368                 rad_assert(data->work_fd < 0);
369
370                 if (!detail_open(listener)) return NULL;
371
372                 rad_assert(data->state == STATE_UNLOCKED);
373                 rad_assert(data->work_fd >= 0);
374
375                 /* FALL-THROUGH */
376
377                 /*
378                  *      Try to lock fd.  If we can't, return.
379                  *      If we can, continue.  This means that
380                  *      the server doesn't block while waiting
381                  *      for the lock to open...
382                  */
383         case STATE_UNLOCKED:
384                 /*
385                  *      Note that we do NOT block waiting for
386                  *      the lock.  We've re-named the file
387                  *      above, so we've already guaranteed
388                  *      that any *new* detail writer will not
389                  *      be opening this file.  The only
390                  *      purpose of the lock is to catch a race
391                  *      condition where the execution
392                  *      "ping-pongs" between radiusd &
393                  *      radrelay.
394                  */
395                 if (rad_lockfd_nonblock(data->work_fd, 0) < 0) {
396                         /*
397                          *      Close the FD.  The main loop
398                          *      will wake up in a second and
399                          *      try again.
400                          */
401                         close(data->work_fd);
402                         data->work_fd = -1;
403                         data->state = STATE_UNOPENED;
404                         return NULL;
405                 }
406
407                 data->fp = fdopen(data->work_fd, "r");
408                 if (!data->fp) {
409                         ERROR("FATAL: Failed to re-open detail file %s: %s",
410                                data->filename, fr_syserror(errno));
411                         fr_exit(1);
412                 }
413
414                 /*
415                  *      Look for the header
416                  */
417                 data->state = STATE_HEADER;
418                 data->delay_time = USEC;
419                 data->vps = NULL;
420
421                 /* FALL-THROUGH */
422
423         case STATE_HEADER:
424         do_header:
425                 data->tries = 0;
426                 if (!data->fp) {
427                         data->state = STATE_UNOPENED;
428                         goto open_file;
429                 }
430
431                 {
432                         struct stat buf;
433
434                         if (fstat(data->work_fd, &buf) < 0) {
435                                 ERROR("Failed to stat "
436                                        "detail file %s: %s",
437                                         data->filename,
438                                         fr_syserror(errno));
439
440                                 goto cleanup;
441                         }
442                         if (((off_t) ftell(data->fp)) == buf.st_size) {
443                                 goto cleanup;
444                         }
445                 }
446
447                 /*
448                  *      End of file.  Delete it, and re-set
449                  *      everything.
450                  */
451                 if (feof(data->fp)) {
452                 cleanup:
453                         DEBUG("Detail - unlinking %s",
454                               data->filename_work);
455                         unlink(data->filename_work);
456                         if (data->fp) fclose(data->fp);
457                         data->fp = NULL;
458                         data->work_fd = -1;
459                         data->state = STATE_UNOPENED;
460                         rad_assert(data->vps == NULL);
461
462                         if (data->one_shot) {
463                                 INFO("Finished reading \"one shot\" detail file - Exiting");
464                                 radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
465                         }
466
467                         return NULL;
468                 }
469
470                 /*
471                  *      Else go read something.
472                  */
473                 break;
474
475                 /*
476                  *      Read more value-pair's, unless we're
477                  *      at EOF.  In that case, queue whatever
478                  *      we have.
479                  */
480         case STATE_READING:
481                 if (data->fp && !feof(data->fp)) break;
482                 data->state = STATE_QUEUED;
483
484                 /* FALL-THROUGH */
485
486         case STATE_QUEUED:
487                 goto alloc_packet;
488
489                 /*
490                  *      Periodically check what's going on.
491                  *      If the request is taking too long,
492                  *      retry it.
493                  */
494         case STATE_RUNNING:
495                 if (time(NULL) < (data->running + (int)data->retry_interval)) {
496                         return NULL;
497                 }
498
499                 DEBUG("No response to detail request.  Retrying");
500                 /* FALL-THROUGH */
501
502                 /*
503                  *      If there's no reply, keep
504                  *      retransmitting the current packet
505                  *      forever.
506                  */
507         case STATE_NO_REPLY:
508                 data->state = STATE_QUEUED;
509                 goto alloc_packet;
510
511                 /*
512                  *      We have a reply.  Clean up the old
513                  *      request, and go read another one.
514                  */
515         case STATE_REPLIED:
516                 pairfree(&data->vps);
517                 data->state = STATE_HEADER;
518                 goto do_header;
519         }
520
521         fr_cursor_init(&cursor, &data->vps);
522
523         /*
524          *      Read a header, OR a value-pair.
525          */
526         while (fgets(buffer, sizeof(buffer), data->fp)) {
527                 data->offset = ftell(data->fp); /* for statistics */
528
529                 /*
530                  *      Badly formatted file: delete it.
531                  *
532                  *      FIXME: Maybe flag an error?
533                  */
534                 if (!strchr(buffer, '\n')) {
535                         pairfree(&data->vps);
536                         goto cleanup;
537                 }
538
539                 /*
540                  *      We're reading VP's, and got a blank line.
541                  *      Queue the packet.
542                  */
543                 if ((data->state == STATE_READING) &&
544                     (buffer[0] == '\n')) {
545                         data->state = STATE_QUEUED;
546                         break;
547                 }
548
549                 /*
550                  *      Look for date/time header, and read VP's if
551                  *      found.  If not, keep reading lines until we
552                  *      find one.
553                  */
554                 if (data->state == STATE_HEADER) {
555                         int y;
556
557                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
558                                 data->state = STATE_READING;
559                         }
560                         continue;
561                 }
562
563                 /*
564                  *      We have a full "attribute = value" line.
565                  *      If it doesn't look reasonable, skip it.
566                  *
567                  *      FIXME: print an error for badly formatted attributes?
568                  */
569                 if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
570                         WARN("Skipping badly formatted line %s",
571                                buffer);
572                         continue;
573                 }
574
575                 /*
576                  *      Should be =, :=, +=, ...
577                  */
578                 if (!strchr(op, '=')) continue;
579
580                 /*
581                  *      Skip non-protocol attributes.
582                  */
583                 if (!strcasecmp(key, "Request-Authenticator")) continue;
584
585                 /*
586                  *      Set the original client IP address, based on
587                  *      what's in the detail file.
588                  *
589                  *      Hmm... we don't set the server IP address.
590                  *      or port.  Oh well.
591                  */
592                 if (!strcasecmp(key, "Client-IP-Address")) {
593                         data->client_ip.af = AF_INET;
594                         if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
595                                 ERROR("Failed parsing Client-IP-Address");
596
597                                 pairfree(&data->vps);
598                                 goto cleanup;
599                         }
600                         continue;
601                 }
602
603                 /*
604                  *      The original time at which we received the
605                  *      packet.  We need this to properly calculate
606                  *      Acct-Delay-Time.
607                  */
608                 if (!strcasecmp(key, "Timestamp")) {
609                         data->timestamp = atoi(value);
610
611                         vp = paircreate(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
612                         if (vp) {
613                                 vp->vp_date = (uint32_t) data->timestamp;
614                                 vp->type = VT_DATA;
615                                 fr_cursor_insert(&cursor, vp);
616                         }
617                         continue;
618                 }
619
620                 /*
621                  *      Read one VP.
622                  *
623                  *      FIXME: do we want to check for non-protocol
624                  *      attributes like radsqlrelay does?
625                  */
626                 vp = NULL;
627                 if ((userparse(data, buffer, &vp) > 0) &&
628                     (vp != NULL)) {
629                         fr_cursor_merge(&cursor, vp);
630                 }
631         }
632
633         /*
634          *      Some kind of error.
635          *
636          *      FIXME: Leave the file in-place, and warn the
637          *      administrator?
638          */
639         if (ferror(data->fp)) goto cleanup;
640
641         data->tries = 0;
642         data->packets++;
643
644         /*
645          *      Process the packet.
646          */
647  alloc_packet:
648         data->tries++;
649
650         /*
651          *      The writer doesn't check that the record was
652          *      completely written.  If the disk is full, this can
653          *      result in a truncated record.  When that happens,
654          *      treat it as EOF.
655          */
656         if (data->state != STATE_QUEUED) {
657                 ERROR("Truncated record: treating it as EOF for detail file %s", data->filename_work);
658                 goto cleanup;
659         }
660
661         /*
662          *      We're done reading the file, but we didn't read
663          *      anything.  Clean up, and don't return anything.
664          */
665         if (!data->vps) {
666                 data->state = STATE_HEADER;
667                 if (!data->fp || feof(data->fp)) goto cleanup;
668                 return NULL;
669         }
670
671         /*
672          *      Allocate the packet.  If we fail, it's a serious
673          *      problem.
674          */
675         packet = rad_alloc(NULL, true);
676         if (!packet) {
677                 ERROR("FATAL: Failed allocating memory for detail");
678                 fr_exit(1);
679         }
680
681         memset(packet, 0, sizeof(*packet));
682         packet->sockfd = -1;
683         packet->src_ipaddr.af = AF_INET;
684         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
685         packet->code = PW_CODE_ACCOUNTING_REQUEST;
686         gettimeofday(&packet->timestamp, NULL);
687
688         /*
689          *      Remember where it came from, so that we don't
690          *      proxy it to the place it came from...
691          */
692         if (data->client_ip.af != AF_UNSPEC) {
693                 packet->src_ipaddr = data->client_ip;
694         }
695
696         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
697         if (vp) {
698                 packet->src_ipaddr.af = AF_INET;
699                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
700         } else {
701                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
702                 if (vp) {
703                         packet->src_ipaddr.af = AF_INET6;
704                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
705                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
706                 }
707         }
708
709         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
710         if (vp) {
711                 packet->dst_ipaddr.af = AF_INET;
712                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
713         } else {
714                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
715                 if (vp) {
716                         packet->dst_ipaddr.af = AF_INET6;
717                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
718                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
719                 }
720         }
721
722         /*
723          *      Generate packet ID, ports, IP via a counter.
724          */
725         packet->id = data->counter & 0xff;
726         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
727         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
728
729         packet->dst_ipaddr.af = AF_INET;
730         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
731
732         /*
733          *      If everything's OK, this is a waste of memory.
734          *      Otherwise, it lets us re-send the original packet
735          *      contents, unmolested.
736          */
737         packet->vps = paircopy(packet, data->vps);
738
739         /*
740          *      Prefer the Event-Timestamp in the packet, if it
741          *      exists.  That is when the event occurred, whereas the
742          *      "Timestamp" field is when we wrote the packet to the
743          *      detail file, which could have been much later.
744          */
745         vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
746         if (vp) {
747                 data->timestamp = vp->vp_integer;
748         }
749
750         /*
751          *      Look for Acct-Delay-Time, and update
752          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
753          */
754         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
755         if (!vp) {
756                 vp = paircreate(packet, PW_ACCT_DELAY_TIME, 0);
757                 rad_assert(vp != NULL);
758                 pairadd(&packet->vps, vp);
759         }
760         if (data->timestamp != 0) {
761                 vp->vp_integer += time(NULL) - data->timestamp;
762         }
763
764         /*
765          *      Set the transmission count.
766          */
767         vp = pairfind(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
768         if (!vp) {
769                 vp = paircreate(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
770                 rad_assert(vp != NULL);
771                 pairadd(&packet->vps, vp);
772         }
773         vp->vp_integer = data->tries;
774
775         if (debug_flag) {
776                 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
777                 for (vp = fr_cursor_init(&cursor, &packet->vps);
778                      vp;
779                      vp = fr_cursor_next(&cursor)) {
780                         debug_pair(vp);
781                 }
782         }
783
784         data->state = STATE_RUNNING;
785         data->running = packet->timestamp.tv_sec;
786
787         return packet;
788 }
789
790 /*
791  *      Free detail-specific stuff.
792  */
793 void detail_free(rad_listen_t *this)
794 {
795         listen_detail_t *data = this->data;
796
797 #ifdef WITH_DETAIL_THREAD
798         if (!check_config) {
799                 ssize_t ret;
800                 void *arg = NULL;
801
802                 /*
803                  *      Mark the child pipes as unusable
804                  */
805                 close(data->child_pipe[0]);
806                 close(data->child_pipe[1]);
807                 data->child_pipe[0] = -1;
808
809                 /*
810                  *      Tell it to stop (interrupting it's sleep)
811                  */
812                 pthread_kill(data->pthread_id, SIGTERM);
813
814                 /*
815                  *      Wait for it to acknowledge that it's stopped.
816                  */
817                 ret = read(data->master_pipe[0], &arg, sizeof(arg));
818                 if (ret < 0) {
819                         ERROR("Reader thread exited without informing the master: %s", fr_syserror(errno));
820                 } else if (ret != sizeof(arg)) {
821                         ERROR("Invalid thread pointer received from reader thread during exit");
822                         ERROR("Expected %zu bytes, got %zi bytes", sizeof(arg), ret);
823                 }
824
825                 close(data->master_pipe[0]);
826                 close(data->master_pipe[1]);
827
828                 if (arg) pthread_join(data->pthread_id, &arg);
829         }
830 #endif
831
832         if (data->fp != NULL) {
833                 fclose(data->fp);
834                 data->fp = NULL;
835         }
836 }
837
838
839 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
840 {
841         if (!this->server) {
842                 return snprintf(buffer, bufsize, "%s",
843                                 ((listen_detail_t *)(this->data))->filename);
844         }
845
846         return snprintf(buffer, bufsize, "detail file %s as server %s",
847                         ((listen_detail_t *)(this->data))->filename,
848                         this->server);
849 }
850
851
852 /*
853  *      Delay while waiting for a file to be ready
854  */
855 static int detail_delay(listen_detail_t *data)
856 {
857         int delay = (data->poll_interval - 1) * USEC;
858
859         /*
860          *      Add +/- 0.25s of jitter
861          */
862         delay += (USEC * 3) / 4;
863         delay += fr_rand() % (USEC / 2);
864
865         DEBUG2("Detail listener %s state %s waiting %d.%06d sec",
866                data->filename,
867                fr_int2str(state_names, data->state, "?"),
868                (delay / USEC), delay % USEC);
869
870         return delay;
871 }
872
873 /*
874  *      Overloaded to return delay times.
875  */
876 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
877 {
878 #ifdef WITH_DETAIL_THREAD
879         return 0;
880 #else
881         listen_detail_t *data = this->data;
882
883         /*
884          *      We haven't sent a packet... delay things a bit.
885          */
886         if (!data->signal) return detail_delay(data);
887
888         data->signal = 0;
889
890         DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
891                data->filename, fr_int2str(state_names, data->state, "?"),
892                data->signal,
893                data->delay_time / USEC,
894                data->delay_time % USEC);
895
896         return data->delay_time;
897 #endif
898 }
899
900 /*
901  *      Overloaded to return "should we fix delay times"
902  */
903 int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
904 {
905 #ifdef WITH_DETAIL_THREAD
906         return 0;
907 #else
908         listen_detail_t *data = this->data;
909
910         return data->signal;
911 #endif
912 }
913
914
915 #ifdef WITH_DETAIL_THREAD
916 static void *detail_handler_thread(void *arg)
917 {
918         char c;
919         rad_listen_t *this = arg;
920         listen_detail_t *data = this->data;
921
922         while (true) {
923                 RADIUS_PACKET *packet;
924
925                 while ((packet = detail_poll(this)) == NULL) {
926                         usleep(detail_delay(data));
927
928                         /*
929                          *      If we're supposed to exit then tell
930                          *      the master thread we've exited.
931                          */
932                         if (data->child_pipe[0] < 0) {
933                                 packet = NULL;
934                                 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
935                                         ERROR("Failed writing exit status to master: %s", fr_syserror(errno));
936                                 }
937                                 return NULL;
938                         }
939                 }
940
941                 /*
942                  *      Keep retrying forever.
943                  *
944                  *      FIXME: cap the retries.
945                  */
946                 do {
947                         if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
948                                 ERROR("Failed passing detail packet pointer to master: %s", fr_syserror(errno));
949                         }
950
951                         if (read(data->child_pipe[0], &c, 1) < 0) {
952                                 ERROR("Failed getting detail packet ack from master: %s", fr_syserror(errno));
953                                 break;
954                         }
955
956                         if (data->delay_time > 0) usleep(data->delay_time);
957                 } while (data->state != STATE_REPLIED);
958         }
959
960         return NULL;
961 }
962 #endif
963
964
965 static const CONF_PARSER detail_config[] = {
966         { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
967         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
968         { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
969         { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
970         { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
971         { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), NULL },
972         { "max_outstanding", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), NULL },
973
974         { NULL, -1, 0, NULL, NULL }             /* end the list */
975 };
976
977 /*
978  *      Parse a detail section.
979  */
980 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
981 {
982         int             rcode;
983         listen_detail_t *data;
984         RADCLIENT       *client;
985         char buffer[2048];
986
987         data = this->data;
988
989         rcode = cf_section_parse(cs, data, detail_config);
990         if (rcode < 0) {
991                 cf_log_err_cs(cs, "Failed parsing listen section");
992                 return -1;
993         }
994
995         /*
996          *      We don't do duplicate detection for "detail" sockets.
997          */
998         this->nodup = true;
999         this->synchronous = false;
1000
1001         if (!data->filename) {
1002                 cf_log_err_cs(cs, "No detail file specified in listen section");
1003                 return -1;
1004         }
1005
1006         if ((data->load_factor < 1) || (data->load_factor > 100)) {
1007                 cf_log_err_cs(cs, "Load factor must be between 1 and 100");
1008                 return -1;
1009         }
1010
1011         if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
1012                 cf_log_err_cs(cs, "poll_interval must be between 1 and 20");
1013                 return -1;
1014         }
1015
1016         if (check_config) return 0;
1017
1018         if (data->max_outstanding == 0) data->max_outstanding = 1;
1019
1020         /*
1021          *      If the filename is a glob, use "detail.work" as the
1022          *      work file name.
1023          */
1024         if ((strchr(data->filename, '*') != NULL) ||
1025             (strchr(data->filename, '[') != NULL)) {
1026                 char *p;
1027
1028 #ifndef HAVE_GLOB_H
1029                 WARN("Detail file \"%s\" appears to use file globbing, but it is not supported on this system.",
1030                      data->filename);
1031 #endif
1032                 strlcpy(buffer, data->filename, sizeof(buffer));
1033                 p = strrchr(buffer, FR_DIR_SEP);
1034                 if (p) {
1035                         p[1] = '\0';
1036                 } else {
1037                         buffer[0] = '\0';
1038                 }
1039                 strlcat(buffer, "detail.work",
1040                         sizeof(buffer) - strlen(buffer));
1041
1042         } else {
1043                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1044         }
1045
1046         data->filename_work = talloc_strdup(data, buffer);
1047
1048         data->work_fd = -1;
1049         data->vps = NULL;
1050         data->fp = NULL;
1051         data->state = STATE_UNOPENED;
1052         data->delay_time = data->poll_interval * USEC;
1053         data->signal = 1;
1054
1055         /*
1056          *      Initialize the fake client.
1057          */
1058         client = &data->detail_client;
1059         memset(client, 0, sizeof(*client));
1060         client->ipaddr.af = AF_INET;
1061         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1062         client->ipaddr.prefix = 0;
1063         client->longname = client->shortname = data->filename;
1064         client->secret = client->shortname;
1065         client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1066
1067 #ifdef WITH_DETAIL_THREAD
1068         /*
1069          *      Create the communication pipes.
1070          */
1071         if (pipe(data->master_pipe) < 0) {
1072                 ERROR("radiusd: Error opening internal pipe: %s",
1073                       fr_syserror(errno));
1074                 fr_exit(1);
1075         }
1076
1077         if (pipe(data->child_pipe) < 0) {
1078                 ERROR("radiusd: Error opening internal pipe: %s",
1079                       fr_syserror(errno));
1080                 fr_exit(1);
1081         }
1082
1083         pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1084
1085         this->fd = data->master_pipe[0];
1086 #endif
1087
1088         return 0;
1089 }
1090 #endif