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
|
#define _POSIX_SOURCE 1
#include "server.h"
#include "cache.h"
#include "common.h"
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <grp.h>
#include <pwd.h>
#include <sys/wait.h>
#include <errno.h>
#include <time.h>
extern int errno;
void SIGCHLD_handler(int sig){
signal(SIGCHLD , SIGCHLD_handler);
wait(NULL);
printf("un fils mort !\n");
}
int main(int argc, char ** argv )
{
int server_port;
char vfs_server_port[5];
char vfs_server_root[PATH_MAX_LEN];
struct sockaddr_in sin;
int sock , sockcom; /*socket de cnx , socket de comm */
struct hostent *hp;
int fromlen=sizeof(hp);
sigset_t ens , ens_old;
void SIGINT_handler(int sig){
printf("SIGINT !! \n");
shutdown(sock , 2);
close(sock);
shutdown(sockcom,2);
close(sockcom);
exit(0);
}
sigfillset(&ens);
sigdelset(&ens , SIGINT);
/* sigdelset(&ens , SIGCHLD); */
sigprocmask(SIG_SETMASK , &ens , &ens_old ) ;
setbuf(stdout, NULL);
setbuf(stdin, NULL);
signal(SIGINT, SIGINT_handler);
signal(SIGCHLD , SIGCHLD_handler);
if (getParam(CACHE_CONF_PATH , "vfs_server_port" , vfs_server_port) != E_SUCCESS ){
fprintf(stderr , "parametre vfs_server_port non trouvé\n");
exit(1);
}
server_port = atoi(vfs_server_port);
if (getParam(CACHE_CONF_PATH , "vfs_server_root" , vfs_server_root) != E_SUCCESS ){
fprintf(stderr , "parametre vfs_server_root non trouvé\n");
exit(1);
}
alltrim(vfs_server_root);
/* creation de la socket */
if ( (sock = socket(AF_INET , SOCK_STREAM , 0 )) < 0 ) {
perror("socket");
exit(1);
}
memset(&sin , 0 , sizeof(sin));
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(server_port);
sin.sin_family = AF_INET;
if ( bind(sock, (struct sockaddr *)&sin , sizeof(sin)) < 0 ){
perror("bind");
exit(1);
}
if (listen(sock , 5)==-1){
perror("listen");
exit(1);
}
while(1){
if ( (sockcom = accept( sock , (struct sockaddr*)&hp , &fromlen)) == -1 ){
printf("%d\n" , errno );
printf("[%s]\n" , strerror(errno) );
perror("accept");
exit(1);
}
switch(fork()){
case -1 : {
perror("fork");
exit(1);
}
case 0:{
int size , desc , needDL=0;
char buffer[SYNCH_MSG_LEN];
char filepath[PATH_MAX_LEN];
char date_s[300];
char date_c[300];
char open_log_path[PATH_MAX_LEN];
struct lc_requete_open req;
struct reponse_open rep;
char req_user_name[USER_LEN];
char req_group_name[USER_LEN];
struct group *req_group;
struct pwd *req_user;
struct stat sbuf;
FILE *open_log;
printf("test\n");
if ( (size = read(sockcom , buffer , SYNCH_MSG_LEN)) < 0){
perror("read");
exit(1);
}
/*traitement de la requete */
memcpy(&req , buffer, size);
/* file exists ? */
if ( ! checkFile(req.path , vfs_server_root , &sbuf) ){
rep.status = STATUS_ERR;
rep.error = E_NOT_EXISTS;
printf("returning ERR\n");
if ( write(sockcom , &rep , sizeof(rep) ) == -1 ){
perror("write");
}
exit(1);
}
/* controle des droits en fonctions du mode demandé */
/* controle des dates */
ctime_r(&(req.date) , date_c);
ctime_r( &(sbuf.st_mtime) , date_s);
printf("locale : %s server : %s " ,date_c , date_s);
if (sbuf.st_mtime > req.date)
needDL = 1;
/* envoi STATUS_DL */
if (needDL)
{
rep.status = STATUS_DL ;
if ( write(sockcom , &rep , sizeof(rep)) == -1 ){
perror("write rep");
exit(1);
}
/* envoi du fichier */
strcpy(filepath , vfs_server_root );
strncat(filepath , "/" , 1 );
strncat(filepath , req.path , strlen(req.path) );
if ( (desc=open(filepath , O_RDONLY )) < 0 ){
fprintf(stderr , "[%s]\n" , filepath );
perror("open file");
exit(1);
}
while ( (size=read(desc,buffer,SYNCH_MSG_LEN)) > 0 ){
if (write (sockcom , buffer , size) == -1){
perror("write sur sock pendant dl");
exit(1);
}
}
if ( size == -1 ){
perror("read dans le fichier pendant dl");
exit(1);
}
/* envoi date dernière modif */
if ( write(sockcom , &(sbuf.st_mtime) , sizeof(sbuf.st_mtime)) == -1){
perror("write mt_time");
exit(1);
}
close(desc);
}
else{
/* envoi OK au cache */
rep.status = STATUS_OK ;
if ( write(sockcom , &rep , sizeof(rep)) == -1 ){
perror("write rep");
exit(1);
}
}
/* logger l'ouverture de fichier dans le fichier vfs_server_root/.open.log */
shutdown(sockcom,2);
close(sockcom);
exit(0);
} /* fin fils */
default : close(sockcom) ; continue ;
} /* fin switch */
} /* while */
printf("sortie du while\n");
return 0;
} |
Partager