/
server.c
1063 lines (878 loc) · 28.3 KB
/
server.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =====================================================================================
*
* Filename: server.c
*
* Description:
*
* Version: 1.0
* Created: 24/10/2012 11:23:26
* Revision: none
* Compiler: gcc
*
* Author: Connor Moreside (conman720), cmoresid@ualberta.ca
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "server.h"
#include "common.h"
#include "mempool.h"
// Do we want to daemonize?
//#define DAEMONIZE
/* Ensures mutual exclusion for clients linked list */
pthread_mutex_t clients_lock = PTHREAD_MUTEX_INITIALIZER;
/* Shared mask for all threads */
sigset_t mask;
/* The name/path of the directory, as passed in the commandline argument */
char init_dir[PATH_MAX];
/* The full path to the directory */
char full_path[PATH_MAX];
/* Represents all the currently connected clients */
struct clientlist* clients;
/* The previous contents/attributes of directory being monitored */
struct direntrylist* prevdir;
/* The current contents/attributes of directory being monitored */
struct direntrylist* curdir;
/* The period to monitor the directory */
int gperiod;
/* The update buffer */
byte update_buff[MAX_FILENAME];
/* Pipe used to send sockets to remove from the master list in the main thread */
int remove_client_pipes[2];
/* Memory pool for directory entry nodes */
struct mempool* direntry_pool;
/* Absolute path for file names */
char abspath[PATH_MAX];
/* Used to ensure that a socket is removed from the master fd list before proceeding */
pthread_cond_t sready = PTHREAD_COND_INITIALIZER;
/* Mutex that protects the sready condition */
pthread_mutex_t slock = PTHREAD_MUTEX_INITIALIZER;
/* Used in tandum with slock and sready */
int done;
struct direntrylist* init_direntrylist()
{
struct direntrylist* list; /* New directory list */
// Allocate memory for a new directory list
list = (struct direntrylist*)malloc(sizeof(struct direntrylist));
if (list == NULL)
return NULL;
// Initialize to default values
list->count = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
void reuse_direntrylist(struct direntrylist* list)
{
struct direntry* p; /* Used to traverse the linked list */
p = list->head;
while (p != NULL) {
list->head = list->head->next;
p->next = NULL;
// Return memory back to pool
mempool_free(direntry_pool, p);
p = list->head;
list->count--;
}
// NULL for both head and tail
list->tail = list->head;
}
int add_direntry(struct direntrylist* list, struct direntry* entry)
{
// Make sure list is not empty
if (list == NULL)
return -1;
if (list->head == NULL) { // List is emptry
list->head = entry;
list->tail = list->head;
} else if (list->head->next == NULL) { // 1 item in list
list->head->next = entry;
list->tail = list->head->next;
} else { // More than 1 item
list->tail->next = entry;
list->tail = entry;
}
list->count++;
return 1;
}
struct direntry* find_direntry(struct direntrylist* list, struct direntry* entry)
{
struct direntry* p;
// Make sure list is not empty
if (list == NULL)
return NULL;
p = list->head;
while (p != NULL) {
// Check for equality based on the inode, which should
// be unique under the assumption that all the files
// in the monitored directory are on the same filesystem
if (p->attrs.st_ino == entry->attrs.st_ino) {
return p;
}
p = p->next;
}
return NULL;
}
int exploredir(struct direntrylist* list, const char* path)
{
struct stat fattr; /* Used to store attributes of a file entry */
struct direntry* list_entry; /* Used to capture information about file entry */
struct dirent** entries; /* Stores each file entry's name and stuff */
int n; /* How many file entries are in the directory */
int i; /* Used to traverse file entries */
// Alphabetize the entries in the directory since Linux is stupid
// and doesn't do this by default, which Mac OS X does...
if ((n = scandir(path, &entries, 0, alphasort)) < 0) {
// Send error message to all clients and then exit
kill_clients(remove_client_pipes[1], "Cannot open directory! ; Exiting now!");
syslog(LOG_ERR, "Cannot open directory: %s", path);
exit(1);
}
// Delete reference to current directory (.)
free(entries[0]);
// Delete reference to parent directory (..)
free(entries[1]);
// Start after . and ..
i = 2;
while (i < n) {
list_entry = (struct direntry*)mempool_alloc(direntry_pool, sizeof(struct direntry));
list_entry->next = NULL;
memset(list_entry, 0, sizeof(list_entry));
if (list_entry == NULL) {
// Mempool has no free nodes and malloc failed
kill_clients(remove_client_pipes[1], "Unrecoverable server error! ; Exiting now!");
syslog(LOG_ERR, "Cannot malloc direntry");
exit(1);
}
// Make sure absolute path is not too long
if ((strlen(path) + strlen(entries[i]->d_name) + 1) >= PATH_MAX) {
kill_clients(remove_client_pipes[1], "Unrecoverable server error! ; Exiting now!");
syslog(LOG_ERR, "Path is too long.");
exit(1);
} else {
strcpy(abspath, path);
strcat(abspath, "/");
strcat(abspath, entries[i]->d_name);
}
// Get the attributes of the file entry
if (stat(abspath, &fattr) < 0) {
kill_clients(remove_client_pipes[1], "Unrecoverable server error! ; Exiting now!");
syslog(LOG_ERR, "Cannot get stats on file: %s", entries[i]->d_name);
exit(1);
}
// Make sure just the file name is not too long
if (strlen(entries[i]->d_name) > MAX_FILENAME) {
syslog(LOG_ERR, "Filename is too long to be saved.");
exit(1);
}
// Copy the file entry name into direntry representation
strcpy(list_entry->filename, entries[i]->d_name);
list_entry->attrs = fattr;
// Add the list entry now
add_direntry(list, list_entry);
free(entries[i]);
i++;
}
free(entries);
return 0;
}
void append_diff(byte* buff, const char* mode, const char* filename, const char* desc)
{
size_t len; /* Used to find length of filename */
// Zero out buffer
memset(buff, 0, sizeof(buff));
// (! OR - OR +) filename human_description
len = strlen(filename);
strcat(buff, mode);
strcat(buff + 1, " ");
strcat(buff + 2, filename);
strcat(buff + 2 + len, desc);
}
int difference_direntrylist()
{
int ndiffs; /* Number of differences found */
struct direntry* entry_prev; /* Pointer to iterate through previous direntrylist */
struct direntry* entry_cur; /* Pointer to iterate through current direntry list */
ndiffs = 0;
// Populate the curdir list with entries in directory right now
exploredir(curdir, (const char*)full_path); /* Global variable: full_path */
// No differences if there is no entries in the directory
if (curdir->count == 0 && prevdir->count == 0) {
return 0;
}
entry_prev = prevdir->head;
while (entry_prev != NULL) {
if ((entry_cur = find_direntry(curdir, entry_prev)) != NULL
&& !IS_CHECKED(entry_prev->mask)) {
// Permissions
if (entry_prev->attrs.st_mode != entry_cur->attrs.st_mode) {
SET_MODIFIED(entry_prev->mask);
SET_PERM(entry_prev->mask);
ndiffs++;
}
// UID
if (entry_prev->attrs.st_uid != entry_cur->attrs.st_uid) {
SET_MODIFIED(entry_prev->mask);
SET_UID(entry_prev->mask);
ndiffs++;
}
// GID
if (entry_prev->attrs.st_gid != entry_cur->attrs.st_gid) {
SET_MODIFIED(entry_prev->mask);
SET_GID(entry_prev->mask);
ndiffs++;
}
// Size
if (entry_prev->attrs.st_size != entry_cur->attrs.st_size) {
SET_MODIFIED(entry_prev->mask);
SET_SIZE(entry_prev->mask);
ndiffs++;
}
// Access time
if (entry_prev->attrs.st_atime != entry_cur->attrs.st_atime) {
SET_MODIFIED(entry_prev->mask);
SET_LAT(entry_prev->mask);
ndiffs++;
}
// Modified time
if (entry_prev->attrs.st_mtime != entry_cur->attrs.st_mtime) {
SET_MODIFIED(entry_prev->mask);
SET_LMT(entry_prev->mask);
ndiffs++;
}
// File status time
if (entry_prev->attrs.st_ctime != entry_cur->attrs.st_ctime) {
SET_MODIFIED(entry_prev->mask);
SET_LFST(entry_prev->mask);
ndiffs++;
}
// Show that the entries have been checked for differences
// and not to check them again
SET_CHECKED(entry_prev->mask);
SET_CHECKED(entry_cur->mask);
} else {
// If a previous entry cannot be found in the current directory,
// it was removed
SET_REMOVED(entry_prev->mask);
ndiffs++;
}
entry_prev = entry_prev->next;
}
// Now check for any entries that have been added to the monitored
// directory
entry_cur = curdir->head;
while (entry_cur != NULL) {
if (!IS_CHECKED(entry_cur->mask)) {
SET_ADDED(entry_cur->mask);
ndiffs++;
}
entry_cur = entry_cur->next;
}
return ndiffs;
}
void create_daemon(const char* name)
{
pid_t pid;
umask(0);
if ((pid = fork()) < 0) {
err_quit("Can't fork.");
} else if (pid > 0) {
exit(0);
}
setsid();
if (chdir("/") < 0)
err_quit("Can't switch to root directory.");
pid = getpid();
printf("Process ID: %d\n", pid);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
openlog(name, LOG_CONS, LOG_DAEMON);
}
static void* signal_thread(void* arg)
{
pthread_attr_t tattr; /* Used to set thread to detached mode */
int err; /* Indicates an error from sigwait */
int signo; /* The signal number that has been caught */
pthread_t tid; /* ID of the thread that has been spawnned */
// Make sure any threads that are spawned are detached, so the OS
// can reclaim the resources in a timely fashion
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
for (;; ) {
// Check directory for updates
alarm(gperiod);
// Block until signal has been caught
err = sigwait(&mask, &signo);
if (err != 0) {
syslog(LOG_ERR, "sigwait failed");
exit(1);
}
switch (signo) {
case SIGHUP:
// Finish transfers, remove all clients
syslog(LOG_INFO, "Received SIGHUP");
kill_clients(remove_client_pipes[1], "Server received SIGHUP; Disconnect all clients.");
break;
case SIGALRM:
// See if directory has updated
syslog(LOG_INFO, "Received SIGALRM");
pthread_create(&tid, &tattr, send_updates, NULL);
break;
case SIGINT:
// Mainly used when not running in daemon mode
syslog(LOG_INFO, "Received SIGINT");
kill_clients(remove_client_pipes[1], "Server received SIGINT; Disconnect all clients.");
exit(0);
case SIGTERM:
syslog(LOG_INFO, "Received SIGTERM");
kill_clients(remove_client_pipes[1], "Server received SIGTERM; Disconnect all clients.");
exit(0);
default:
syslog(LOG_ERR, "Unexpected signal: %d", signo);
break;
}
}
}
void* send_updates(void* arg)
{
struct client* p; /* Pointer to traverse through client list */
struct direntry* entry; /* Pointer to traverse through a direntry list */
struct direntrylist* tmp; /* Used as tmp storage to swap prevdir and curdir */
int sent; /* How many updates have been sent so far */
int diffs; /* The number of differences in monitored directory */
int more_diffs; /* If diffs > 254, save how many more need to be sent */
// Get number of differences found in monitored directory
diffs = difference_direntrylist();
more_diffs = 0;
// If there are more than 254 changed entries, send the first 254 updates
// then send in separate update the rest of the updates
if (diffs > 254) {
more_diffs = diffs - 254;
diffs = 254;
}
// LOCK : Make sure clients is not altered while sending updates
pthread_mutex_lock(&clients_lock);
p = clients->head;
while (p != NULL) {
// LOCK : Make sure client is not removed while update is being
// sent out
pthread_mutex_lock(p->c_lock);
// Reset
sent = 0;
// Send number of entries that have changed
if (send_byte(p->socket, ((byte)diffs)) <= 0) {
syslog(LOG_ERR, "Could not send number of changed entries");
}
// Now examine bitmask of each entry in prevdir and see if attributes
// have changed or if entry has been removed
entry = prevdir->head;
while (entry != NULL) {
if (IS_MODIFIED(entry->mask)) {
if (IS_PERM(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> permissions");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_UID(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> UID");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_GID(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> GID");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_SIZE(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> size");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_LAT(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> last access time");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_LMT(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> last modfied time");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
if (IS_LFST(entry->mask)) {
append_diff(update_buff, "!", entry->filename, " -> last file status time");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
} else if (IS_REMOVED(entry->mask)) {
append_diff(update_buff, "-", entry->filename, " ");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
entry = entry->next;
}
// Now examine the current state of the monitored directory and see
// if any entries have been added
entry = curdir->head;
while (entry != NULL) {
if (IS_ADDED(entry->mask)) {
append_diff(update_buff, "+", entry->filename, " ");
if (send_string(p->socket, update_buff) <= 0)
syslog(LOG_ERR, "Could not send string");
sent++;
if (sent == diffs && more_diffs > 0) send_byte(p->socket, (byte)more_diffs);
}
entry = entry->next;
}
// UNLOCK
pthread_mutex_unlock(p->c_lock);
p = p->next;
}
// UNLOCK
pthread_mutex_unlock(&clients_lock);
// Now reverse the roles of prevdir and curdir
// i.e. the curdir becomes the old dir
reuse_direntrylist(prevdir);
tmp = prevdir;
prevdir = curdir;
curdir = tmp;
// Clear bitmasks
entry = prevdir->head;
while (entry != NULL) {
entry->mask = 0;
entry = entry->next;
}
return((void*)0);
}
int send_error(int socket, const char* err_msg)
{
struct client* p; /* Used to hold client reference */
// Try to find client in clients linked list
if ((p = find_client_ref(socket)) == NULL) {
syslog(LOG_ERR, "Could not find client to disconnect from.");
return -1;
} else {
// LOCK : Make all updates have been sent to client first
pthread_mutex_lock(p->c_lock);
if (send_byte(p->socket, END_COM) <= 0) {
syslog(LOG_ERR, "Could not send error byte");
return -1;
}
if (send_string(p->socket, err_msg) <= 0) {
syslog(LOG_ERR, "Could not send error string");
return -1;
}
// UNLOCK
pthread_mutex_unlock(p->c_lock);
}
return 0;
}
int send_error2(int socket, const char* err_msg)
{
if (send_byte(socket, END_COM) <= 0) {
syslog(LOG_ERR, "Could not send error byte");
return -1;
}
if (send_string(socket, err_msg) <= 0) {
syslog(LOG_ERR, "Could not send error string");
return -1;
}
return 0;
}
void* init_client(void* arg)
{
int socketfd; /* Socket fd of new client */
size_t len; /* String length */
int socket_buff[1]; /* Buffer used to send socket back to main thread */
socketfd = (int)arg;
len = strlen(init_dir);
// No more clients are being accepted, remove socket from master list,
// and wait until it has been removed properly
if (clients->count == MAX_CLIENTS) {
syslog(LOG_INFO, "No more clients can be accepted.");
// Tell main thread to remove socket from master list
socket_buff[0] = socketfd;
write(remove_client_pipes[1], socket_buff, (int)1);
// Make sure that the socket is removed from
// the master list before preceding any farther
// (Removes a race condition which would cause
// select() to break)
pthread_mutex_lock(&slock);
while (done == 0)
pthread_cond_wait(&sready, &slock);
done = 0;
pthread_mutex_unlock(&slock);
// Send the error now
send_error2(socketfd, "No more clients can be accepted.");
close(socketfd);
pthread_exit((void*)1);
}
// Add client to clients linked list
pthread_mutex_lock(&clients_lock);
add_client_ref(socketfd);
pthread_mutex_unlock(&clients_lock);
// Send 0xFE
if (send_byte(socketfd, INIT_CLIENT1) != 1) {
syslog(LOG_WARNING, "Cannot send init client 1");
exit(1);
}
// Send 0xED
if (send_byte(socketfd, INIT_CLIENT2) != 1) {
syslog(LOG_WARNING, "Cannot send init client 2");
exit(1);
}
// Send monitored directory name/path
if (send_string(socketfd, init_dir) != len) {
syslog(LOG_WARNING, "Cannot send path directory");
exit(1);
}
// Send the refresh period
if (send_byte(socketfd, gperiod) != 1) {
syslog(LOG_WARNING, "Cannot send period");
exit(1);
}
return((void*)0);
}
void* remove_client(void* arg)
{
struct thread_arg* targ; /* Thread arguments */
// Get thread args
targ = (struct thread_arg*)arg;
// LOCK : Make sure clients is not altered
// while trying to remove client ref
pthread_mutex_lock(&clients_lock);
remove_client_ref(targ->socket);
pthread_mutex_unlock(&clients_lock);
// Now disconnect from client nicely
disconnect_from_client(targ->socket, 0);
// Free thread arg
free(targ);
return((void*)0);
}
void kill_clients(int pipe, const char* msg)
{
struct client* p; /* Used to traverse clients linked list */
int socket_buff[1]; /* Send socket fd to main thread to remove from
master fd list */
// LOCK : Make sure clients is not altered while removing all client connections
pthread_mutex_lock(&clients_lock);
if (clients->count > 0) {
p = clients->head;
while (p != NULL) {
// Send the socket fd to the main thread to remove
// it from the master fd list
socket_buff[0] = p->socket;
write(pipe, socket_buff, 1);
// Make sure that the socket is removed from
// the master list before preceding any farther
// (Removes a race condition which would cause
// select() to break)
pthread_mutex_lock(&slock);
while (done == 0)
pthread_cond_wait(&sready, &slock);
done = 0;
pthread_mutex_unlock(&slock);
send_error(p->socket, msg);
close(p->socket);
remove_client_ref(p->socket);
p = clients->head;
}
}
// UNLOCK
pthread_mutex_unlock(&clients_lock);
}
int disconnect_from_client(int socketfd, int pipe)
{
byte b;
if ((b = read_byte(socketfd)) != REQ_REMOVE1) {
syslog(LOG_ERR, "Anticipated 0xDE: Received: 0x%x", b);
close(socketfd);
return -1;
}
if ((b = read_byte(socketfd)) != REQ_REMOVE2) {
syslog(LOG_ERR, "Anticipated 0xAD: Received: 0x%x", b);
close(socketfd);
return -1;
}
if (send_byte(socketfd, END_COM) != 1) {
syslog(LOG_ERR, "Could not send byte!");
close(socketfd);
return -1;
}
if (send_string(socketfd, GOOD_BYE) != 7) {
syslog(LOG_ERR, "Could not send string");
close(socketfd);
return -1;
}
// Close socket now
close(socketfd);
return 0;
}
void add_client_ref(int socketfd)
{
struct client *ct; /* New client reference */
if (clients == NULL) {
clients = (struct clientlist*)malloc(sizeof(struct clientlist));
clients->head = NULL;
clients->tail = NULL;
clients->count = 0;
}
// Try to allocate space for a new client
ct = (struct client*)malloc(sizeof(struct client));
if (ct == NULL) {
syslog(LOG_ERR, "Cannot malloc new client thread");
return;
}
// Initialize client ref
ct->c_lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(ct->c_lock, NULL);
ct->socket = socketfd;
ct->next = NULL;
ct->prev = NULL;
if (clients->head == NULL) {
clients->head = ct;
clients->tail = clients->head;
} else if (clients->head == clients->tail) {
clients->head->next = ct;
ct->prev = clients->head;
clients->tail = clients->head->next;
} else {
clients->tail->next = ct;
ct->prev = clients->tail;
clients->tail = ct;
}
clients->count++;
}
void remove_client_ref(int socketfd)
{
struct client* ct; /* Client ref to remove */
struct client* tmp; /* Tmp storage */
if ((ct = find_client_ref(socketfd)) == NULL) {
syslog(LOG_ERR, "Could not find client.");
exit(1);
}
// If lock is currently locked, maybe server is trying to send
// out data. Wait until lock is aquired.
pthread_mutex_lock(ct->c_lock);
if (clients->count == 1) {
clients->head = clients->head->next;
clients->tail = clients->head;
} else if (ct == clients->head) {
clients->head = clients->head->next;
clients->head->prev = NULL;
} else if (ct == clients->tail) {
clients->tail = clients->tail->prev;
clients->tail->next = NULL;
} else {
tmp = ct->next;
ct->prev->next = tmp;
tmp->prev = ct->prev;
}
// UNLOCK
pthread_mutex_unlock(ct->c_lock);
// Deallocate client now
ct->prev = NULL;
ct->next = NULL;
pthread_mutex_destroy(ct->c_lock);
free(ct->c_lock);
free(ct);
clients->count--;
}
struct client* find_client_ref(int socketfd)
{
struct client* p; /* Client ref with socketfd as its socket */
p = clients->head;
while (p != NULL) {
if (p->socket == socketfd) {
return p;
}
p = p->next;
}
return NULL;
}
int start_server(int port_number, const char* dir_name, int period)
{
pthread_t tid; /* Passed to pthread_create */
struct thread_arg* targ; /* Used to pass arguments to threads */
pthread_attr_t tattr; /* Specifies that thread should be detached */
fd_set master; /* Keep track of all connections / pipes to multiplex */
fd_set read_fds; /* Copy of master for select to populate */
int fdmax; /* Highest numbered file descriptor */
int i; /* Used to index the master fd list */
int listener; /* Listening socket of the server */
int newfd; /* New connection socket fd */
struct sockaddr_in local_addr; /* Local connection info */
struct sockaddr_in remote_addr; /* Remote connection info */
socklen_t addr_len; /* Address length */
int pipe_buff[1]; /* Get sockets into here */
pipe_buff[0] = 0;
// Init signal mask
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = SIG_IGN;
// Start each thread in detached mode, since we don't care about
// their return values
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
// Initialize the memory pool to store the direntries
direntry_pool = init_mempool(sizeof(struct direntry), 512);
// Set done to 0
done = 0;
// Initialize the clients linked list
clients = (struct clientlist*)malloc(sizeof(struct clientlist));
clients->head = NULL;
clients->tail = NULL;
clients->count = 0;
// Copy into global init_dir
strcpy(init_dir, dir_name);
gperiod = period;
// Initialize pipe
if (pipe(remove_client_pipes) < 0) {
syslog(LOG_ERR, "Cannot create IPC in server.");
exit(1);
}
// Get full path of the directory
if (realpath(dir_name, full_path) == NULL) {
syslog(LOG_ERR, "Cannot resolve full path.");
exit(1);
}
#ifdef DAEMONIZE
create_daemon("dirapp");
#endif
#ifndef DAEMONIZE
openlog("dirapp", LOG_CONS, LOG_DAEMON);
#endif
// Make sure SIGPIPE is blocked
if (sigaction(SIGPIPE, &sa, NULL) < 0) {
syslog(LOG_WARNING, "SIGPIPE error");
}
// Signals for the signal thread to handle
sigemptyset(&mask);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGALRM);
// Set the mask
if (pthread_sigmask(SIG_BLOCK, &mask, NULL) != 0)
syslog(LOG_WARNING, "pthread_sigmask failed");
// Initialize file descriptor lists
FD_ZERO(&master);
FD_ZERO(&read_fds);
// Setup local connection info
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
local_addr.sin_port = htons(port_number);
// Create listener socket
listener = socket(AF_INET, SOCK_STREAM, 0);
// Try to bind
if (bind(listener, (struct sockaddr*)&local_addr, sizeof(local_addr))) {
syslog(LOG_ERR, "Cannot bind socket to address");
exit(1);
}
// Now listen!
if (listen(listener, MAX_CLIENTS) < 0) {
syslog(LOG_ERR, "Cannot listen on socket");
exit(1);
}
syslog(LOG_INFO, "Starting server!");
// Have select check for incoming data
FD_SET(remove_client_pipes[0], &master);
FD_SET(listener, &master);
fdmax = listener;
// Initialize the direntry lists
prevdir = init_direntrylist();
curdir = init_direntrylist();
// Initially populate list of file entries in monitored directory
exploredir(prevdir, (const char*)full_path);
// Start signal thread
pthread_create(&tid, NULL, signal_thread, NULL);
int errno = 0;