Project

General

Profile

Bug #755 ยป dnswatch.patch

Warren Baker, 07/20/2010 03:10 PM

View differences:

dnswatch.c 2010-07-20 21:06:27.000000000 +0200
44 44
#include <string.h>                                                                                                                                    
45 45
#include <fcntl.h>                                                                                                                                     
46 46
                                                                                                                                                       
47
                                                                                                                                                       
48
#define LINE_LENGTH 128                                                                                                                                
49
#define NUMHOSTS 128                                                                                                                                   
50
                                                                                                                                                       
47 51
/*                                                                                                                                                     
48 52
       Resolves each host name in a given list at regular intervals, and runs                                                                          
49 53
       a command whenever any of them resolves to a different IP address than                                                                          
......
92 96
       int interval;                                                                                                                                   
93 97
       char *file;                                                                                                                                     
94 98
       char *command;                                                                                                                                  
95
       properties list, props;                                                                                                                         
99
       char line[LINE_LENGTH];                                                                                                                         
100
       char *p;                                                                                                                                        
96 101
       struct in_addr *ips;                                                                                                                            
97
       int fd;                                                                                                                                         
98 102
       FILE *pidfd;                                                                                                                                    
103
       FILE *fp;                                                                                                                                       
99 104
                                                                                                                                                       
100 105
       if (argc > 5 || argc < 4)                                                                                                                       
101 106
               usage();
......
121 126
       }                                                                                                                                               
122 127
                                                                                                                                                       
123 128
       // Attempt to open configuration file which lists hosts                                                                                         
124
       fd = open(file, O_RDONLY);                                                                                                                      
125
       if (fd == -1) {                                                                                                                                 
129
       if ((fp=fopen(file, "r")) == NULL) {
126 130
               syslog(LOG_ERR, "unable to open configuration file '%s'", file);
127 131
               exit(1);
128 132
       }
129 133

  
130 134
       // Read hostnames in that are in the configuration file
131
       props = properties_read(fd);
132
       if (props == NULL) {
133
               syslog(LOG_ERR, "error reading configuration file");
134
               exit(1);
135
       int count = 0;
136
       char **hostlist = malloc(NUMHOSTS * sizeof(char *));
137
       if (hostlist == NULL) {
138
               fprintf(stderr, "malloc failed\n");
139
               exit(2);
135 140
       }
136
       
137
       // Close open file handle for dnswatch configuration
138
       close(fd);
139

  
140
       int hosts = 1;
141
       list = props;
142
       while (list != NULL) {
143
               list = list->next;
144
               hosts++;
141
       while(fgets(line, sizeof line, fp) != NULL) {                                                                                                   
142
               p = strchr(line, '\n');                                                                                                                 
143
               if (p) {
144
                       *p = '\0';
145
               }
146
               hostlist[count] = malloc(strlen(line));
147
               strcpy(hostlist[count], line);
148
               count++;
145 149
       }
146 150

  
151
       // Close open file handle for dnswatch configuration
152
       fclose(fp);
153

  
147 154
       // Create a place to hold resolved ip addresses
148
       ips = calloc(hosts, sizeof(struct in_addr));
155
       ips = calloc(count, sizeof(struct in_addr));
149 156
       if (ips == NULL) {
150 157
               fprintf(stderr, "calloc failed\n");
151 158
               exit(2);
152 159
       }
153 160

  
161

  
154 162
       // Loop forever checking to see if a hostname changes
155 163
       while (1) {
156 164
               int i = 0;
157 165
               int changes = 0;
158
               list = props;
159
               while (list != NULL) {
160
                       changes += check_hostname(list->name, &ips[i]);
161
                       list = list->next;
162
                       i++;
166
               for (i = 0; i<count; i++) {                                                                                                             
167
                       syslog(LOG_ERR, "Checking hostname '%s'", hostlist[i]);                                                                         
168
                       changes += check_hostname(hostlist[i], &ips[i]);
163 169
               }
164 170
               
165 171
               // If the hostname changed, run the specified command
......
171 177
       }
172 178

  
173 179
       // Release memory before exiting
174
       properties_free(props);
180
       free(hostlist);
175 181

  
176 182
       return 0;
177 183
}
    (1-1/1)