-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.cpp
More file actions
289 lines (227 loc) · 6.51 KB
/
daemon.cpp
File metadata and controls
289 lines (227 loc) · 6.51 KB
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
#define MAX_BUF 1024
struct Address
{
char* site1;
char* site2;
int site1Type; /*0 for file, 1 for folder*/
int site2Type; /*0 for file, 1 for folder*/
};
struct TimeAddress
{
Address addr;
int hour; /*0-24*/
int minute;/*0-59*/
int wasUsed;/*0-1*/
};
struct DriveAddress
{
Address addr;
int accessable;
};
struct UpdateList
{
TimeAddress tAddr[];
int tSize;
DriveAddress dAddr[];
int dSize;
Address rAddr[];
int rSize;
};
struct Settings
{
int updateTime; /*Should I make this into a float?*/
int scheduledUpdate;
int realtimeUpdate;
int driveUpdate;
};
int pollLocs();
int pollDrives();
int pollTime(UpdateList*, FILE**);
int updateLocs();
int checkFifo(char *, char[MAX_BUF], int, FILE*);
int testList(UpdateList*, FILE*);
void initSettings(Settings *, int, int, int, int);
//void cleanup(void);
int main(void)
{
pid_t pid, sid;
int fd;
char * myfifo = "/tmp/synkFifo";
char buffer[MAX_BUF];
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
printf("process_id of child process %d \n", pid);
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/*Open log files here*/
FILE *f = fopen("/home/joseph/Documents/C_C++/Synk/daemonlog.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(EXIT_FAILURE);
}
FILE **fPtr = &f;
fprintf(f, "File start;\n");
/*Sets sid*/
sid = setsid();
if (sid < 0)
{
fprintf(f, "Setup failure: setsid() failed\n");
exit(EXIT_FAILURE);
}
/*Sets working dir*/
if ((chdir("/")) < 0)
{
fprintf(f, "Setup failure: chdir() failed\n");
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
//close(STDOUT_FILENO);
close(STDERR_FILENO);
struct Settings *settings = (Settings *)malloc(sizeof *settings);
initSettings(settings, 1, 0, 0, 30);
struct UpdateList *masterList = (UpdateList *)malloc(sizeof *masterList);
if (testList(masterList, f) < 0)
{
fprintf(f, "Setup failure: testList() failed\n");
exit(EXIT_FAILURE);
}
//atexit(cleanup);
fprintf(f, "masterList.tAddr[0].wasUsed: %d (In 'main()')\n", masterList->tAddr[0].wasUsed);
while (1)
{
int shouldUpdate = 0;
//settings.scheduledUpdate = 1;
if (settings->realtimeUpdate)
{
shouldUpdate = pollLocs();
}
fprintf(f, "settings.scheduledUpdate: %d\n", settings->scheduledUpdate);
/*if (settings->scheduledUpdate)
{
/*shouldUpdate = pollTime(masterList, fPtr); PollTime is giving some trouble so it is excluded from current execution
}*/
if (settings->driveUpdate)
{
shouldUpdate = pollDrives();
}
if (shouldUpdate)
{
updateLocs();
}
checkFifo(myfifo, buffer, fd, f);
break; //testing only
sleep(settings->updateTime);
}
/*Cleanup on exit*/
fprintf(f, "Successfull exit\n*********\n");
fclose(f);
close(fd);
exit(EXIT_SUCCESS);
}
int pollLocs()
{
return 0;
}
int pollDrives()
{
/*if a path on the list that previously didn't exist does now return its path to update*/
return 0;
}
int pollTime(UpdateList* list, FILE** fLog)
{
int returnVal = 0;
time_t rawTime;
struct tm *cTime;
fputs("BEFORE ERROR STATEMENT\n", *fLog);
time(&rawTime);
fputs("after first fprintf", *fLog);
cTime = localtime(&rawTime); //This is the line causing all the problems
fprintf(*fLog, "list.tAddr[0].wasUsed: %d (Inside of pollTime)\n", list->tAddr[0].wasUsed);
fputs("after sencond fprintf", *fLog);
TimeAddress *timesList = new TimeAddress[list->tSize];
int arraySize = list->tSize;
memcpy(timesList, list->tAddr, sizeof(*timesList)); //Copies from masterList to times list
fprintf(*fLog, "timesList[0].wasUsed: %d\n", timesList[0].wasUsed);
for (int i = 0; i < 1/*arraySize*/; ++i)/*Remember to set this to loop through all of the list in the future*/
{
if (timesList[i].hour >= cTime->tm_hour && timesList[i].minute >= cTime->tm_min && timesList[i].wasUsed == 0)
{
timesList[i].wasUsed = 1;
fprintf(*fLog, "Updating Locations: its %d:%d\n", timesList[i].hour, timesList[i].minute);
returnVal = 1;
}
else
{
fprintf(*fLog, "Time comparison failed.\ntimesList[%d].hour:\t%d\ncTime->tm_hour:\t\t%d\ntimesList[%d].minute:\t%d\ncTime->tm_min:\t\t%d\ntimesList[%d].wasUsed:\t%d\n", i, timesList[i].hour, cTime->tm_hour, i, timesList[i].minute, cTime->tm_min, i, timesList[i].wasUsed);
}
}
return returnVal;
}
int updateLocs(){ return 0; }
int checkFifo(char *fifoPath, char buf[MAX_BUF], int fd, FILE* fLog)
{
/* open, read, and display the message from the FIFO */
fd = open(fifoPath, O_RDWR);
if (fd < 0)
{
fprintf(fLog, "checkFifo(): open() failed\n");
return 0;
}
if (read(fd, buf, MAX_BUF) < 0)
{
fprintf(fLog, "checkFifo(): read() failed\n");
return 0;
}
fprintf(fLog, "Received: %s\n", buf);
return 1;
}
int testList(UpdateList* list, FILE* eff)
{
time_t curTime = time(NULL);
struct tm *localTime;
localTime = localtime(&curTime);
Address testAddr;
TimeAddress testtAddr;
testtAddr.addr = testAddr;
testtAddr.hour = localTime->tm_hour;
testtAddr.minute = localTime->tm_min;
testtAddr.wasUsed = 0;
testtAddr.minute = testtAddr.minute + 1; /*This is used for testing pollTime()*/
// fprintf(eff, "testtAddr.wasUsed: %d\n", testtAddr.wasUsed);
list->tAddr[0] = testtAddr;
// fprintf(eff, "list.tAddr[0].wasUsed: %d\n", list.tAddr[0].wasUsed);
return 1;
}
void initSettings(Settings *sett, int sUp, int dUp, int rUp, int upRate)
{
sett -> scheduledUpdate = sUp;
sett -> driveUpdate = dUp;
sett -> realtimeUpdate = rUp;
sett -> updateTime = upRate;
}
/*void cleanup(void)
{
fprintf(f, "Successfull exit\n*********\n");
fclose(f);
close(fd);
}*/