newvector should be a bool
[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_insert(&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                 _exit(1);
680         }
681
682         memset(packet, 0, sizeof(*packet));
683         packet->sockfd = -1;
684         packet->src_ipaddr.af = AF_INET;
685         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
686         packet->code = PW_CODE_ACCOUNTING_REQUEST;
687         gettimeofday(&packet->timestamp, NULL);
688
689         /*
690          *      Remember where it came from, so that we don't
691          *      proxy it to the place it came from...
692          */
693         if (data->client_ip.af != AF_UNSPEC) {
694                 packet->src_ipaddr = data->client_ip;
695         }
696
697         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
698         if (vp) {
699                 packet->src_ipaddr.af = AF_INET;
700                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
701         } else {
702                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
703                 if (vp) {
704                         packet->src_ipaddr.af = AF_INET6;
705                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
706                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
707                 }
708         }
709
710         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
711         if (vp) {
712                 packet->dst_ipaddr.af = AF_INET;
713                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
714         } else {
715                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
716                 if (vp) {
717                         packet->dst_ipaddr.af = AF_INET6;
718                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
719                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
720                 }
721         }
722
723         /*
724          *      Generate packet ID, ports, IP via a counter.
725          */
726         packet->id = data->counter & 0xff;
727         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
728         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
729
730         packet->dst_ipaddr.af = AF_INET;
731         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
732
733         /*
734          *      If everything's OK, this is a waste of memory.
735          *      Otherwise, it lets us re-send the original packet
736          *      contents, unmolested.
737          */
738         packet->vps = paircopy(packet, data->vps);
739
740         /*
741          *      Prefer the Event-Timestamp in the packet, if it
742          *      exists.  That is when the event occurred, whereas the
743          *      "Timestamp" field is when we wrote the packet to the
744          *      detail file, which could have been much later.
745          */
746         vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
747         if (vp) {
748                 data->timestamp = vp->vp_integer;
749         }
750
751         /*
752          *      Look for Acct-Delay-Time, and update
753          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
754          */
755         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
756         if (!vp) {
757                 vp = paircreate(packet, PW_ACCT_DELAY_TIME, 0);
758                 rad_assert(vp != NULL);
759                 pairadd(&packet->vps, vp);
760         }
761         if (data->timestamp != 0) {
762                 vp->vp_integer += time(NULL) - data->timestamp;
763         }
764
765         /*
766          *      Set the transmission count.
767          */
768         vp = pairfind(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
769         if (!vp) {
770                 vp = paircreate(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
771                 rad_assert(vp != NULL);
772                 pairadd(&packet->vps, vp);
773         }
774         vp->vp_integer = data->tries;
775
776         if (debug_flag) {
777                 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
778                 for (vp = fr_cursor_init(&cursor, &packet->vps);
779                      vp;
780                      vp = fr_cursor_next(&cursor)) {
781                         debug_pair(vp);
782                 }
783         }
784
785         data->state = STATE_RUNNING;
786         data->running = packet->timestamp.tv_sec;
787
788         return packet;
789 }
790
791 /*
792  *      Free detail-specific stuff.
793  */
794 void detail_free(rad_listen_t *this)
795 {
796         listen_detail_t *data = this->data;
797
798 #ifdef WITH_DETAIL_THREAD
799         if (!check_config) {
800                 ssize_t ret;
801                 void *arg = NULL;
802
803                 /*
804                  *      Mark the child pipes as unusable
805                  */
806                 close(data->child_pipe[0]);
807                 close(data->child_pipe[1]);
808                 data->child_pipe[0] = -1;
809
810                 /*
811                  *      Tell it to stop (interrupting it's sleep)
812                  */
813                 pthread_kill(data->pthread_id, SIGTERM);
814
815                 /*
816                  *      Wait for it to acknowledge that it's stopped.
817                  */
818                 ret = read(data->master_pipe[0], &arg, sizeof(arg));
819                 if (ret < 0) {
820                         ERROR("Reader thread exited without informing the master: %s", fr_syserror(errno));
821                 } else if (ret != sizeof(arg)) {
822                         ERROR("Invalid thread pointer received from reader thread during exit");
823                         ERROR("Expected %zu bytes, got %zi bytes", sizeof(arg), ret);
824                 }
825
826                 close(data->master_pipe[0]);
827                 close(data->master_pipe[1]);
828
829                 if (arg) pthread_join(data->pthread_id, &arg);
830         }
831 #endif
832
833         if (data->fp != NULL) {
834                 fclose(data->fp);
835                 data->fp = NULL;
836         }
837 }
838
839
840 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
841 {
842         if (!this->server) {
843                 return snprintf(buffer, bufsize, "%s",
844                                 ((listen_detail_t *)(this->data))->filename);
845         }
846
847         return snprintf(buffer, bufsize, "detail file %s as server %s",
848                         ((listen_detail_t *)(this->data))->filename,
849                         this->server);
850 }
851
852
853 /*
854  *      Delay while waiting for a file to be ready
855  */
856 static int detail_delay(listen_detail_t *data)
857 {
858         int delay = (data->poll_interval - 1) * USEC;
859
860         /*
861          *      Add +/- 0.25s of jitter
862          */
863         delay += (USEC * 3) / 4;
864         delay += fr_rand() % (USEC / 2);
865
866         DEBUG2("Detail listener %s state %s waiting %d.%06d sec",
867                data->filename,
868                fr_int2str(state_names, data->state, "?"),
869                (delay / USEC), delay % USEC);
870
871         return delay;
872 }
873
874 /*
875  *      Overloaded to return delay times.
876  */
877 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
878 {
879 #ifdef WITH_DETAIL_THREAD
880         return 0;
881 #else
882         listen_detail_t *data = this->data;
883
884         /*
885          *      We haven't sent a packet... delay things a bit.
886          */
887         if (!data->signal) return detail_delay(data);
888
889         data->signal = 0;
890
891         DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
892                data->filename, fr_int2str(state_names, data->state, "?"),
893                data->signal,
894                data->delay_time / USEC,
895                data->delay_time % USEC);
896
897         return data->delay_time;
898 #endif
899 }
900
901 /*
902  *      Overloaded to return "should we fix delay times"
903  */
904 int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
905 {
906 #ifdef WITH_DETAIL_THREAD
907         return 0;
908 #else
909         listen_detail_t *data = this->data;
910
911         return data->signal;
912 #endif
913 }
914
915
916 #ifdef WITH_DETAIL_THREAD
917 static void *detail_handler_thread(void *arg)
918 {
919         char c;
920         rad_listen_t *this = arg;
921         listen_detail_t *data = this->data;
922
923         while (true) {
924                 RADIUS_PACKET *packet;
925
926                 while ((packet = detail_poll(this)) == NULL) {
927                         usleep(detail_delay(data));
928
929                         /*
930                          *      If we're supposed to exit then tell
931                          *      the master thread we've exited.
932                          */
933                         if (data->child_pipe[0] < 0) {
934                                 packet = NULL;
935                                 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
936                                         ERROR("Failed writing exit status to master: %s", fr_syserror(errno));
937                                 }
938                                 return NULL;
939                         }
940                 }
941
942                 /*
943                  *      Keep retrying forever.
944                  *
945                  *      FIXME: cap the retries.
946                  */
947                 do {
948                         if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
949                                 ERROR("Failed passing detail packet pointer to master: %s", fr_syserror(errno));
950                         }
951
952                         if (read(data->child_pipe[0], &c, 1) < 0) {
953                                 ERROR("Failed getting detail packet ack from master: %s", fr_syserror(errno));
954                                 break;
955                         }
956
957                         if (data->delay_time > 0) usleep(data->delay_time);
958                 } while (data->state != STATE_REPLIED);
959         }
960
961         return NULL;
962 }
963 #endif
964
965
966 static const CONF_PARSER detail_config[] = {
967         { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
968         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
969         { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
970         { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
971         { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
972         { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), NULL },
973         { "max_outstanding", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), NULL },
974
975         { NULL, -1, 0, NULL, NULL }             /* end the list */
976 };
977
978 /*
979  *      Parse a detail section.
980  */
981 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
982 {
983         int             rcode;
984         listen_detail_t *data;
985         RADCLIENT       *client;
986         char buffer[2048];
987
988         data = this->data;
989
990         rcode = cf_section_parse(cs, data, detail_config);
991         if (rcode < 0) {
992                 cf_log_err_cs(cs, "Failed parsing listen section");
993                 return -1;
994         }
995
996         /*
997          *      We don't do duplicate detection for "detail" sockets.
998          */
999         this->nodup = true;
1000         this->synchronous = false;
1001
1002         if (!data->filename) {
1003                 cf_log_err_cs(cs, "No detail file specified in listen section");
1004                 return -1;
1005         }
1006
1007         if ((data->load_factor < 1) || (data->load_factor > 100)) {
1008                 cf_log_err_cs(cs, "Load factor must be between 1 and 100");
1009                 return -1;
1010         }
1011
1012         if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
1013                 cf_log_err_cs(cs, "poll_interval must be between 1 and 20");
1014                 return -1;
1015         }
1016
1017         if (check_config) return 0;
1018
1019         if (data->max_outstanding == 0) data->max_outstanding = 1;
1020
1021         /*
1022          *      If the filename is a glob, use "detail.work" as the
1023          *      work file name.
1024          */
1025         if ((strchr(data->filename, '*') != NULL) ||
1026             (strchr(data->filename, '[') != NULL)) {
1027                 char *p;
1028
1029 #ifndef HAVE_GLOB_H
1030                 WARN("Detail file \"%s\" appears to use file globbing, but it is not supported on this system.",
1031                      data->filename);
1032 #endif
1033                 strlcpy(buffer, data->filename, sizeof(buffer));
1034                 p = strrchr(buffer, FR_DIR_SEP);
1035                 if (p) {
1036                         p[1] = '\0';
1037                 } else {
1038                         buffer[0] = '\0';
1039                 }
1040                 strlcat(buffer, "detail.work",
1041                         sizeof(buffer) - strlen(buffer));
1042
1043         } else {
1044                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1045         }
1046
1047         data->filename_work = talloc_strdup(data, buffer);
1048
1049         data->work_fd = -1;
1050         data->vps = NULL;
1051         data->fp = NULL;
1052         data->state = STATE_UNOPENED;
1053         data->delay_time = data->poll_interval * USEC;
1054         data->signal = 1;
1055
1056         /*
1057          *      Initialize the fake client.
1058          */
1059         client = &data->detail_client;
1060         memset(client, 0, sizeof(*client));
1061         client->ipaddr.af = AF_INET;
1062         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1063         client->ipaddr.prefix = 0;
1064         client->longname = client->shortname = data->filename;
1065         client->secret = client->shortname;
1066         client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1067
1068 #ifdef WITH_DETAIL_THREAD
1069         /*
1070          *      Create the communication pipes.
1071          */
1072         if (pipe(data->master_pipe) < 0) {
1073                 ERROR("radiusd: Error opening internal pipe: %s",
1074                       fr_syserror(errno));
1075                 fr_exit(1);
1076         }
1077
1078         if (pipe(data->child_pipe) < 0) {
1079                 ERROR("radiusd: Error opening internal pipe: %s",
1080                       fr_syserror(errno));
1081                 fr_exit(1);
1082         }
1083
1084         pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1085
1086         this->fd = data->master_pipe[0];
1087 #endif
1088
1089         return 0;
1090 }
1091 #endif