File: postpone.c

package info (click to toggle)
pkspxy 0.5-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 496 kB
  • ctags: 153
  • sloc: ansic: 1,777; sh: 452; makefile: 110
file content (132 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* handle the list of postponed requests */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <syslog.h>

#include <unistd.h>
#include <time.h>

#include "server.h"

void pkspxy_postponed_append (const char *req)
{
  struct pkspxy_strlist *p, **q;

  q = &PostponedQueries;
  p = PostponedQueries;

  while (p)
  {
    if (!strcmp (p->str, req))
      return;

    q = &p->next;
    p = p->next;
  }

  /* XXX - log an error - or panic? */

  if (!(*q = calloc (1, sizeof (struct pkspxy_strlist))))
  {
    syslog (LOG_ERR, "calloc failed (%m)");
    return;
  }

  if (!((*q)->str = strdup (req)))
  {
    syslog (LOG_ERR, "strdup failed (%m)");
    free (*q);
    *q = NULL;
    return;
  }
}

int pkspxy_is_postponed (const char *req)
{
  struct pkspxy_strlist *p;

  for (p = PostponedQueries; p; p = p->next)
  {
    if (p->str && !strcmp (p->str, req))
      return 1;
  }
  return 0;
}


void pkspxy_handle_postponed_requests (short force)
{
  struct pkspxy_strlist *p, *q;

  if (Online && (force || (time (NULL) - LastPostponedTimestamp) >= PostponedInterval))
  {
    if (PostponedQueries) 
      syslog (LOG_INFO, "Processing postponed requests");
    LastPostponedTimestamp = time (NULL);

    p = PostponedQueries;
    while (p)
    {
      syslog (LOG_INFO, "launching query for request `%s'", p->str);
      if (pkspxy_spawn_request (p->str, 600, NULL) == 0)
      {
	q = p->next;
	free (p->str);
	free (p);
	p = q;
      }
      else
	break;
    }

    PostponedQueries = p;
  }
}
	
void pkspxy_dump_postponed (void)
{
  char dumpfile[_POSIX_PATH_MAX];
  struct pkspxy_strlist *p;
  FILE *fp;
  
  snprintf (dumpfile, sizeof (dumpfile), "%s/pkspxy.postponed",
	    SpoolDir);
  
  if (!(fp = fopen (dumpfile, "w")))
    return;
  
  for (p = PostponedQueries; p; p = p->next)
    fprintf (fp, "%s\n", p->str);

  fclose (fp);
}


void pkspxy_restore_postponed (void)
{
  char dumpfile[_POSIX_PATH_MAX];
  char buff[1024];
  FILE *fp;
  char *s;
  
  snprintf (dumpfile, sizeof (dumpfile), "%s/pkspxy.postponed",
	    SpoolDir);

  if (!(fp = fopen (dumpfile, "r")))
    return;

  while (fgets (buff, sizeof (buff), fp))
  {
    if ((s = strchr (buff, '\n')))
      *s = '\0';
    
    pkspxy_postponed_append (buff);
  }
  
  fclose (fp);
}