-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
executable file
·139 lines (113 loc) · 4.58 KB
/
utils.c
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
#include "utils.h"
extern struct config g_config;
extern char *g_config_file;
int config_parser(struct config *c)
{
dictionary *ini;
char *host;
char errors[1024] = "";
ini = iniparser_load(g_config_file);
if (ini == NULL)
{
fprintf(stderr, "Error parsing ini file\n");
return -1;
}
if (iniparser_getstr(ini, "myscd:pid_file") == NULL)
strcat(errors, "* pid_file\n");
if (iniparser_getstr(ini, "myscd:bind_address") == NULL)
strcat(errors, "* bind_address\n");
if (iniparser_getstr(ini, "myscd:bind_port") == NULL)
strcat(errors, "* bind_port\n");
if (iniparser_getstr(ini, "myscd:hosts") == NULL)
strcat(errors, "* hosts\n");
if (iniparser_getstr(ini, "myscd:host_check_interval") == NULL)
strcat(errors, "* host_check_interval\n");
if (iniparser_getstr(ini, "myscd:host_check_interval") == NULL)
strcat(errors, "* max_clients\n");
if (iniparser_getstr(ini, "myscd:syslog_identity") == NULL)
strcat(errors, "* syslog_identity\n");
if (strlen(errors))
{
fprintf(stderr, "required config elements:\n\n%s", errors);
exit(-1);
}
c->pid_file = (char *) malloc(1 + strlen(iniparser_getstr(ini, "myscd:pid_file")));
c->bind_address = (char *) malloc(1 + strlen(iniparser_getstr(ini, "myscd:bind_address")));
c->hosts = (char *) malloc(1 + strlen(iniparser_getstr(ini, "myscd:hosts")));
c->syslog_identity = (char *) malloc(1 + strlen(iniparser_getstr(ini, "myscd:syslog_identity")));
strcpy(c->pid_file , iniparser_getstr(ini, "myscd:pid_file"));
strcpy(c->bind_address , iniparser_getstr(ini, "myscd:bind_address"));
strcpy(c->hosts , iniparser_getstr(ini, "myscd:hosts"));
strcpy(c->syslog_identity, iniparser_getstr(ini, "myscd:syslog_identity"));
c->bind_port = iniparser_getint(ini, "myscd:bind_port", 22000);
c->host_check_interval = iniparser_getint(ini, "myscd:host_check_interval", 10);
c->max_clients = iniparser_getint(ini, "myscd:max_clinets", 10);
host = strtok(c->hosts, ",");
while (host != NULL)
{
char *section_name = (char *) malloc(1024);
c->host_infos[c->host_count] = (struct host_info *) malloc(sizeof(struct host_info));
/* host name */
strcpy(section_name, host);
strcat(section_name, ":");
strcat(section_name, "host_name");
c->host_infos[c->host_count]->host_name = (char *) malloc(1 + strlen(iniparser_getstr(ini, section_name)));
strcpy(c->host_infos[c->host_count]->host_name, iniparser_getstr(ini, section_name));
/* user name */
strcpy(section_name, host);
strcat(section_name, ":");
strcat(section_name, "user_name");
c->host_infos[c->host_count]->user_name = (char *) malloc(1 + strlen(iniparser_getstr(ini, section_name)));
strcpy(c->host_infos[c->host_count]->user_name, iniparser_getstr(ini, section_name));
/* password */
strcpy(section_name, host);
strcat(section_name, ":");
strcat(section_name, "password");
c->host_infos[c->host_count]->password = (char *) malloc(1 + strlen(iniparser_getstr(ini, section_name)));
strcpy(c->host_infos[c->host_count]->password, iniparser_getstr(ini, section_name));
/* port */
strcpy(section_name, host);
strcat(section_name, ":");
strcat(section_name, "port");
c->host_infos[c->host_count]->port = iniparser_getint(ini, section_name, 3306);
host = strtok(NULL, ",");
c->host_count++;
}
return 0;
}
void usage(void)
{
printf("\n myscd usage\n\n");
printf(" -c <config file>\n");
printf(" -k [start | stop | restart]\n");
printf("\n");
}
void free_config(struct config *c)
{
dictionary *ini;
char *host;
free(c->pid_file);
free(c->bind_address);
ini = iniparser_load(g_config_file);
host = strtok(c->hosts, ",");
c->host_count = 0;
while (host != NULL)
{
free(c->host_infos[c->host_count]->host_name);
free(c->host_infos[c->host_count]->user_name);
free(c->host_infos[c->host_count]->password);
free(c->host_infos[c->host_count]);
c->host_count++;
host = strtok(NULL, ",");
}
free(c->hosts);
c->host_count = 0;
}
char* itoa(int val, int base)
{
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}