-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
206 lines (182 loc) · 4.64 KB
/
main.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
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
#include "include/main.h"
/*
* @brief
* backup_loop
* Need for "recursive backups"
*/
void * backup_loop(void * argument)
{
fprintf(stderr, "<%s> thread id %lu start running on backup loop successfully...\n",
__func__, pthread_self());
backup_fs_iteration_main((thread_argument_t *)argument);
pthread_exit(NULL);
}
void * main_loop(void * argument)
{
fprintf(stderr, "<%s>\n", __func__);
return pthread_on_dir_run(argument);
}
/*
* @brief
* main driver
* needed to distribute the work with POSIX threads
*/
int main(int argc, char * argv[])
{
const int sleep_now = 1;
void * (* func[THREAD_COUNT])(void * path_to_dir) = { backup_loop, main_loop };
hash_table_t * HT = hash_table_create(HASH_TABLE_MAX);
thread_argument_t * argument = (thread_argument_t *)calloc(1, sizeof(thread_argument_t));
argument->hash_table = HT;
char path_to_dir_buffer[HELPERS_BUF_SIZE] = {0};
int free_and_exit(int rc)
{
hash_table_free(HT);
free(argument);
return rc;
}
if (NULL == freopen("/dev/null", "r", stdin)
#ifndef ENABLE_DEBUG_INFO
|| NULL == freopen("/dev/null", "w", stderr)
|| NULL == freopen("/dev/null", "w", stdout)
#endif
)
{
printf("Failed to redirect I/Os to /dev/null, exiting...\n");
return free_and_exit(INVALID_EXIT);
}
/* TODO:
* 1) make a func for this purpose (in helpers side)
* 2) change type of parsing conf file (from .yaml, for example)
*/
/* ==================== */
/* Start of Serializing */
bool flag = true;
int before = 0, after = 0, i = 0, j = 0, counter;
size_t size_of_line;
char * tmp;
char * dir;
char before_value[HELPERS_BUF_SIZE] = {0};
char * buffer = parser_read_conf();
if (NULL == buffer)
{
fprintf(stderr, "Error: invalid buffer value");
return free_and_exit(INVALID_EXIT);
}
while (flag)
{
for (i = before; buffer[i] != '\n'; i++)
{
if (buffer[i] == '\0')
{
flag = false;
}
}
after = i;
size_of_line = sizeof(char) * after;
char tmp[after];
counter = 0;
for (j = before; j < after; j++)
{
tmp[counter++] = buffer[j];
}
tmp[counter] = '\0';
if (strlen(tmp) > HELPERS_FILE_STROKE_MAX_LEN)
{
free(buffer);
return INVALID_EXIT;
}
// skipping \n for normal work
before = after + 1;
int index = parser_get_index_by_param(tmp, PARSER_DELIMETER);
if (0 > index)
{
continue;
}
int k;
// in result before_value is parameter without value
for (k = 0; k != index; k++)
{
before_value[k] = tmp[k];
}
before_value[k] = '\0';
// parse value (segment of data with some path)
int local_counter = 0;
char slice_after_delimeter[size_of_line];
for (int i = index + 2; tmp[i] != '\0'; i++)
{
slice_after_delimeter[local_counter++] = tmp[i];
}
slice_after_delimeter[local_counter] = '\0';
if (0 == strcmp(before_value, PATH_TO_DIR_DEFINE))
{
struct stat path_stat;
stat(slice_after_delimeter, &path_stat);
if (!S_ISDIR(path_stat.st_mode))
{
fprintf(stderr, "Warning: this path is not a path to directory !\n");
free(buffer);
return free_and_exit(INVALID_EXIT);
}
strncpy(path_to_dir_buffer, slice_after_delimeter, strlen(slice_after_delimeter));
}
if (0 == strcmp(before_value, PATH_TO_BACKUP_DEFINE))
{
backup_set_path_to_backup(slice_after_delimeter);
}
if (0 == strcmp(before_value, PATH_TO_LOGGER_DEFINE))
{
logger_set_path_to_log(slice_after_delimeter);
}
}
free(buffer);
argument->path_to_dir = path_to_dir_buffer;
/* End of Serializing */
if (signal(SIGINT, signal_handler) == SIG_IGN)
{
/* Ignore the signal SIGINT (Ctrl+C) */
signal(SIGINT, SIG_IGN);
}
pthread_t threads[THREAD_COUNT];
pthread_mutex_t mutex;
if (0 != pthread_mutex_init(&mutex, NULL))
{
printf("Error: pthread_mutex_init() failed !\n");
return free_and_exit(INVALID_EXIT);
}
argument->mutex = &mutex;
int rc = 0;
for (i = 0; i < THREAD_COUNT; i++)
{
rc = pthread_create(&threads[i], NULL, func[i], argument);
if (NORMAL_EXIT != rc)
{
printf("<%s> Error for create thread id #%lu; return code: %d", __func__, threads[i], rc);
pthread_mutex_destroy(&mutex);
return free_and_exit(INVALID_EXIT);
}
}
for (i = 0; i < THREAD_COUNT; i++)
{
while (helpers_get_keep_running()) { sleep(sleep_now); continue; }
int total_sleep = 0;
while (0 != pthread_tryjoin_np(threads[i], NULL))
{
total_sleep += sleep_now;
sleep(sleep_now);
if (!(total_sleep % MAX_SEC_WAIT))
{
printf("\n!!! timeout for waiting %lu thread, exiting !!!\n", threads[i]);
rc = INVALID_EXIT;
break;
}
continue;
}
if (rc != 0)
{
printf("Error for join thread id #%lu; return code: %d\n", threads[i], rc);
}
}
pthread_mutex_destroy(&mutex);
return free_and_exit(rc);
}