File: config_parser.c

package info (click to toggle)
cpufreqd 1.2.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,648 kB
  • ctags: 210
  • sloc: sh: 8,665; ansic: 1,648; makefile: 120
file content (621 lines) | stat: -rw-r--r-- 17,866 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
 *                                George Staikos <staikos@0wned.org>
 *
 *  2004.08.22
 *  - added percentage/absolute frequency translation based on a patch submitted
 *    by Herv� Eychenne
 *
 *  2003.16.08
 *  - added support for cpu monitoring, base code by Dietz Proepper and minor
 *    fixes by Mattia Dongili
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "cpufreqd.h"
#include "main.h"
#include "config_parser.h"

/*
 * Parse configuration file.
 *
 * As long as the config file is quite simple let's keep also a veryvery simple parser.
 */
int parse_config (general *g) {
  FILE *config;
  profile *last_profile = 0L, *p_iter = 0L;
  rule *last_rule = 0L, *r_iter = 0L;
  char buf[256];

  /* configuration file */
  cp_log(LOG_INFO, "parse_config(): reading configuration file %s\n", g->config_file);
  config = fopen(g->config_file, "r");
  if (!config) {
    cp_log(LOG_ERR, "parse_config(): %s: %s\n", g->config_file, strerror(errno));
    return -1;
  }

  while (!feof(config)) {
    char *clean = 0L;

    if (!fgets(buf, 255, config))
      break;

    clean = clean_config_line(buf);

    if (!clean[0]) /* returned an empty line */
      continue;

    clean = strip_comments_line(clean);

    if (!clean[0]) /* returned an empty line */
      continue;

    /* if General scan general options */
    if (strstr(clean,"[General]")) {
      if (parse_config_general (config, g) < 0) {
        return -1;
      }
      continue;
    }

    /* if Profile scan profile options */
    if (strstr(clean,"[Profile]")) {

      profile *prof = (profile *)malloc(sizeof(profile));
      memset(prof, 0, sizeof(profile));
      if (parse_config_profile (config, prof, g) < 0) {
        cp_log(LOG_CRIT, "parse_config(): [Profile] error parsing %s, see logs for details.\n", g->config_file);
        fclose(config);
        return -1;
      }
      if (!g->profiles) {
        g->profiles = last_profile = prof;
      } else {
        p_iter = g->profiles;
        /* checks duplicate names */
        while (p_iter) {
          if (strcmp(p_iter->name, prof->name) == 0) {
            cp_log(LOG_CRIT, "parse_config(): [Profile] name \"%s\" already exists.\n", prof->name);
            fclose(config);
            return -1;
          }
          p_iter = p_iter->next;
        }
        last_profile->next = prof;
        last_profile = last_profile->next;
      }
      continue;
    }

    /* if Rule scan rules options */
    if (strstr(clean,"[Rule]")) {
      rule *rul = (rule *)malloc(sizeof(rule));
      memset(rul, 0, sizeof(rule));
      if (parse_config_rule (config, rul) < 0) {
        cp_log(LOG_CRIT, "parse_config(): [Rule] error parsing %s, see logs for details.\n", g->config_file);
        return -1;
      }
      if (!g->rules) {
        g->rules = last_rule = rul;
      } else {
        r_iter = g->rules;
        /* check duplicate names */
        while (r_iter != NULL) {
          if (strcmp(r_iter->name, rul->name) == 0) {
            cp_log(LOG_CRIT, "parse_config(): [Rule] name \"%s\" already exists.\n", rul->name);
            return -1;
          }
          r_iter = r_iter->next;
        }
        last_rule->next = rul;
        last_rule = last_rule->next;
      }

      continue;
    }
  }
  fclose(config);

  /* did I read something? 
   * chek if I read at least one rule, otherwise exit
   */
  if (g->rules == NULL) {
    cp_log(LOG_ERR, "parse_config(): No rules found!\n");
    return -1;
  }
  
  /*
   * associate rules->profiles
   * go through rules and associate to the proper profile
   */
  r_iter = g->rules;
  while (r_iter != NULL) {
    int profile_found = 0;

    p_iter = g->profiles;
    /* go through profiles */
    while (p_iter != NULL) {
      if (strcmp(r_iter->profile_name, p_iter->name)==0) {
        /* a profile is allowed to be addressed by more than 1 rule */
        r_iter->prof = p_iter;
        profile_found = 1;
        break;
      }
      p_iter = p_iter->next;
    }

    if (!profile_found) {
      cp_log(LOG_CRIT, "parse_config(): Syntax error: no Profile section found for Rule \"%s\" \
                        (requested Profile \"%s\")\n",
                       r_iter->name,
                       r_iter->profile_name);
      return -1;
    }
    r_iter = r_iter->next;
  }

  r_iter = g->rules;
  while (r_iter != NULL) {
     cp_log(LOG_NOTICE, "parse_config(): Rule \"%s\" has Profile \"%s\"\n", r_iter->name, r_iter->prof->name);
     r_iter = r_iter->next;
  }

  return 0;
}

/*
 * parse the [General] section
 *
 * Returns -1 if required properties are missing, 0 otherwise
 */
int parse_config_general (FILE *config, general *g) {
char buf[256];

  while (!feof(config)) {
    char *clean;
    char *name;
    char *value;

    if (!fgets(buf, 255, config))
      break;

    clean = strip_comments_line(buf);

    if (!clean[0]) /* returned an empty line */
      continue;

    clean = clean_config_line(clean);

    if (!clean[0]) /* returned an empty line */
      break;

    name = strtok(clean, "=");
    value = strtok(NULL, "");

    if (strcmp(name,"poll_interval") == 0) {
      if (value != NULL) {
        g->poll_interval = atoi (value);
      }
      /* validate */
      if (g->poll_interval < 1) {
        cp_log(LOG_WARNING, "WARNING: [General] poll_interval has invalid value, using default.\n");
        g->poll_interval = DEFAULT_POLL;
      }
    } else if (strcmp(name,"verbosity") == 0) {
      if (!g->log_level_overridden) {
        if (value != NULL) {
          g->log_level = atoi (value);
          /* validate */
          if (g->log_level < 0 || g->log_level > 7) {
            cp_log(LOG_WARNING, "WARNING: [General] verbosity has invalid value, using default (%d).\n", DEFAULT_VERBOSITY);
            g->log_level = DEFAULT_VERBOSITY;
          }
        } else {
          g->log_level = DEFAULT_VERBOSITY;
          cp_log(LOG_WARNING, "WARNING: [General] verbosity has empty value, using default (%d).\n", DEFAULT_VERBOSITY);
        }
      } else {
          cp_log(LOG_DEBUG, "parse_config_general(): skipping \"verbosity\", overridden in the command line.\n");
      }
    } else if (strcmp(name,"pm_type") == 0) {
      if (value != NULL) {
        strncpy(g->pm_plugin, value, 101);
      } else {
        cp_log(LOG_WARNING, "parse_config_general(): empty \"pm_type\", using default "DEFAULT_PMPLUGIN".\n");
        strncpy(g->pm_plugin, DEFAULT_PMPLUGIN, 101);
      }
      g->pm_plugin[100] = 0;
    } else if (strcmp(name,"pidfile") == 0) {
      if (value != NULL) {
        strncpy(g->pidfile, value, 512);
      } else {
        cp_log(LOG_WARNING, "parse_config_general(): empty \"pidfile\", using default "CPUFREQD_PIDFILE".\n");
        strncpy(g->pidfile, CPUFREQD_PIDFILE, 512);
      }
      g->pidfile[511] = 0;
    } else if (strcmp(name,"acpi_workaround") == 0) {
      if (value != NULL) {
        g->acpi_workaround = atoi (value);
        cp_log(LOG_WARNING, "parse_config_general(): ACPI workaround enabled.\n");
      }
    } else {
      cp_log(LOG_WARNING, "WARNING: [General] skipping unknown config option \"%s\"\n", name);
    }
  }

  return 0;
}

/*
 * parse a [Profile] section
 *
 * Returns -1 if required properties are missing, 0 otherwise
 */
int parse_config_profile (FILE *config, profile *p, general *g) {
#define HAS_NAME    1
#define HAS_MIN     2
#define HAS_MAX     4
#define HAS_POLICY  8
int state = 0;
char buf[256];

  while (!feof(config)) {
    char *clean;
    char *name;
    char *value;

    if (!fgets(buf, 255, config))
      break;

    clean = strip_comments_line(buf);

    if (!clean[0]) /* returned an empty line */
      continue;

    clean = clean_config_line(clean);

    if (!clean[0]) /* returned an empty line */
      break;

    name = strtok(clean, "=");
    value = strtok(NULL, "");
    
    /* empty value: skip */
    if (value == NULL)
      continue;
    
    if (strcmp(name,"name")==0) {
      strncpy(p->name, value, 255);
      p->name[254] = 0;
      state |= HAS_NAME;
    } else if (strcmp(name,"minfreq")==0) {
      if (strstr(value, "%") != NULL) {
        /* set separator (useful for 2.4 kernels only) */
        if (p->sep != ':') {
          p->sep = '%';
        } else {
          cp_log(LOG_CRIT, "parse_config_profile(): cannot mix percentage and absolute kHz values in a single profile.\n");
          return -1;
        }
      } else {
        /* set separator (useful for 2.4 kernels only) */
        if (p->sep != '%') {
          p->sep = ':';
        } else {
          cp_log(LOG_CRIT, "parse_config_profile(): cannot mix percentage and absolute kHz values in a single profile.\n");
          return -1;
        }
      }
      p->min_freq = atol(value);
      state |= HAS_MIN;
      
    } else if (strcmp(name,"maxfreq") == 0) {
      
      if (strstr(value, "%") != NULL) {
        /* set separator (useful for 2.4 kernels only) */
        if (p->sep != ':') {
          p->sep = '%';
        } else {
          cp_log(LOG_CRIT, "parse_config_profile(): cannot mix percentage and absolute kHz values in a single profile.\n");
          return -1;
        }
      } else {
        /* set separator (useful for 2.4 kernels only) */
        if (p->sep != '%') {
          p->sep = ':';
        } else {
          cp_log(LOG_CRIT, "parse_config_profile(): cannot mix percentage and absolute kHz values in a single profile.\n");
          return -1;
        }
      }
      p->max_freq = atol(value);
      state |= HAS_MAX;
    
    } else if (strcmp(name,"policy") == 0) {
      
      strncpy(p->policy_name, value, 255);
      p->policy_name[254] = 0;
      state |= HAS_POLICY;
    
    } else {
      cp_log(LOG_WARNING, "WARNING: [Profile] skipping unknown config option \"%s\"\n", name);
    }
  }

  if (!(state & HAS_NAME)) {
    cp_log(LOG_ERR, "parse_config_profile(): [Profile] missing required property \"name\".\n");
    return -1;
  }
  if (!(state & HAS_MIN)) {
    cp_log(LOG_ERR, "parse_config_profile(): [Profile] \"%s\" missing required property \"minfreq\".\n", p->name);
    return -1;
  }
  if (!(state & HAS_MAX)) {
    cp_log(LOG_ERR, "parse_config_profile(): [Profile] \"%s\" missing required property \"maxfreq\".\n", p->name);
    return -1;
  }
  if (!(state & HAS_POLICY)) {
    cp_log(LOG_ERR, "parse_config_profile(): [Profile] \"%s\" missing required property \"policy\".\n", p->name);
    return -1;
  }
  
  /* translate max_freq to absolute value if necessary */
  if (g->has_sysfs && p->sep == '%') {
    p->max_freq = g->cpu_max_freq * ((float)p->max_freq / 100);
    cp_log(LOG_INFO, "parse_config_profile(): [Profile] \"%s\" translated max frequency to %d\n", p->name, p->max_freq);
    /* normalize if necessary */
    if (p->max_freq > g->cpu_max_freq) {
      cp_log(LOG_NOTICE, "parse_config_profile(): [Profile] \"%s\" max frequency %d out of range\n", p->name, p->max_freq);
      p->max_freq = g->cpu_max_freq;
    }
  }
  /* translate min_freq to absolute value if necessary */
  if (g->has_sysfs && p->sep == '%') {
    p->min_freq = g->cpu_max_freq * ((float)p->min_freq / 100);
    cp_log(LOG_INFO, "parse_config_profile(): [Profile] \"%s\" translated min frequency to %d\n", p->name, p->min_freq);
    /* normalize if necessary */
    if (p->min_freq < g->cpu_min_freq) {
      cp_log(LOG_NOTICE, "parse_config_profile(): [Profile] \"%s\" min frequency %d out of range\n", p->name, p->min_freq);
      p->min_freq = g->cpu_min_freq;
    }
  }

  return 0;
}

/*
 * parses a [Rule] section
 *
 * Returns -1 if required properties are missing, 0 otherwise
 */
int parse_config_rule (FILE *config, rule *r) {
#define HAS_NAME    1 
#define HAS_PROFILE 2
int state = 0;
char buf[256];

  /* reset profile ref */
  r->prof = 0;

  /* initialize battery_interval */
  r->bat = (battery_interval *) malloc(sizeof(battery_interval));
  r->bat->min = 101;
  r->bat->max = -1;
  
  /* initialize cpu_interval */
  r->cpu = (cpu_interval *) malloc(sizeof(cpu_interval));
  r->cpu->min = 101;    /* so we won't ever trigger them... */
  r->cpu->max = -1;

  /* initialize cpu_nice_scale to DEFAULT_NICE_SCALE */
  r->cpu_nice_scale = DEFAULT_NICE_SCALE;
  
  while (!feof(config)) {
    char *clean;
    char *name;
    char *value;

    if (!fgets(buf, 255, config))
      break;

    clean = strip_comments_line(buf);

    if (!clean[0]) /* returned an empty line */
      continue;

    clean = clean_config_line(clean);

    if (!clean[0]) /* returned an empty line */
      break;

    name = strtok(clean, "=");
    value = strtok(NULL, "");

    /* empty value: skip */
    if (value == NULL)
      continue;

    if (strcmp(name,"name") == 0) {
      strncpy(r->name, value, 255);
      r->name[254] = 0;
      state |= HAS_NAME;
    } else if (strcmp(name,"ac") == 0) {
      r->ac = strcmp(value,"on") == 0 ? 1:0;
    } else if (strcmp(name,"battery_interval") == 0) {
      r->bat->min = atoi(strtok(value,"-"));
      r->bat->max = atoi(strtok(NULL,""));
    } else if (strcmp(name,"cpu_interval") == 0) {
      r->cpu->min = atoi(strtok(value,"-"));
      r->cpu->max = atoi(strtok(NULL,""));
    } else if (strcmp(name,"cpu_nice_scale") == 0) {
      r->cpu_nice_scale = atof(value);
      if (r->cpu_nice_scale<=0) {
        cp_log(LOG_ERR, "parse_config_rule(): [Rule] invalid \"cpu_nice_scale\" value: %f.\n", r->cpu_nice_scale);
        return -1;
      }
    } else if (strcmp(name,"delay_cycles") == 0) {
      r->delay_cycles = atoi(value);
      if (r->delay_cycles<0) {
        cp_log(LOG_ERR, "parse_config_rule(): [Rule] invalid \"delay_cycles\" value: %d.\n", r->delay_cycles);
        return -1;
      }
    } else if (strcmp(name,"programs") == 0) {
      /* create program list */
      char *t_prog;
      r->program_list = string_list_new();
      t_prog = strtok(value,",");
      do {
        struct string_node *temp;
        
        if (t_prog == NULL)
          continue;
        
        temp = string_node_new();
        strncpy(temp->string, t_prog, 255);
        temp->string[254] = 0;

        string_list_append(r->program_list, temp);
        cp_log(LOG_DEBUG, "parse_config_rule(): read program: %s\n", temp->string);
      } while ((t_prog = strtok(NULL,",")) != NULL);

    } else if (strcmp(name, "profile") == 0) {
      strncpy(r->profile_name, value, 255);
      r->profile_name[254] = 0;
      state |= HAS_PROFILE;
    } else {
      cp_log(LOG_WARNING, "WARNING: [Rule] skipping unknown config option \"%s\"\n", name);
    }
  }

  if (!(state & HAS_NAME)) {
    cp_log(LOG_ERR, "parse_config_rule(): [Rule] missing required property \"name\".\n");
    return -1;
  }

  if (!(state & HAS_PROFILE)) {
    cp_log(LOG_ERR, "parse_config_rule(): [Rule] \"%s\" missing required property \"profile\".\n", 
                 r->name);
    return -1;
  }

  return 0;
}

/* char *clean_config_line (char *str)
 *
 * Removes trailing blanks and CR off the string str
 *
 * Returns a pointer to the cleaned string.
 * WARNING: it modifies the input string!
 * 
 */
char *clean_config_line (char *str) {
int i = 0;

  /* remove white spaces at the beginning */
  while (isspace(str[0])) {
    str++;
  }

  /* remove end line white space */
  i = strlen(str) - 1;
  while (i >= 0 && isspace(str[i])) {
    str[i] = 0;
    i--;
  }

  return str;
}

/* char *strip_comments_line (char *str)
 *
 * Removes comments off the string str
 *
 * Returns a pointer to the cleaned string.
 * WARNING: it modifies the input string!
 * 
 */
char *strip_comments_line (char *str) {
int i;

  /* remove comment */
  for (i = strlen(str); i >= 0; i--) {
    if (str[i] == '#') {
      str[i] = 0;
    }
  }

  return str;
}

/* void free_config(general *g)
 *
 * Frees the structures allocated.
 *
 */
void free_config(general *g) {
  profile *p_iter = 0L, *p_temp = 0L;
  rule *r_iter = 0L, *r_temp = 0L;
  
  /* cleanup config structs */
  r_iter = g->rules;
  while (r_iter != NULL) {
    r_temp = r_iter;
    r_iter = r_iter->next;
    
      cp_log(LOG_DEBUG, "free_config(): freeing rule %s.\n", r_temp->name);
      cp_log(LOG_DEBUG, "free_config(): freeing battery interval %d-%d.\n", r_temp->bat->min, r_temp->bat->max);
    
    free(r_temp->bat);
    
      cp_log(LOG_DEBUG, "free_config(): freeing cpu interval %d-%d.\n", r_temp->cpu->min, r_temp->cpu->max);
    
    free(r_temp->cpu);
    r_temp->bat = 0L;
    r_temp->cpu = 0L;
    
      cp_log(LOG_DEBUG, "free_config(): freeing program list.\n");
    
    if (r_temp->program_list) {
      string_list_free_sublist(r_temp->program_list, r_temp->program_list->first);
      free(r_temp->program_list);
    }
    
    free(r_temp);
  }
  g->rules = 0L;
  
  p_iter = g->profiles;
  while (p_iter != NULL) {
    p_temp = p_iter;
    p_iter = p_iter->next;
    cp_log(LOG_DEBUG, "free_config(): freeing profile %s.\n", p_temp->name);
    free(p_temp);
  }
  g->profiles = 0L;

  /* clean other values */
  g->poll_interval = DEFAULT_POLL;
  g->has_sysfs = 0;
  g->acpi_workaround = 0;
  g->cpu_min_freq = 0;
  g->cpu_max_freq = 0;
  
  if (!g->log_level_overridden)
    g->log_level = DEFAULT_VERBOSITY;
  
}