Guest User

Untitled

a guest
Oct 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.02 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <signal.h>
  11. #include "mmboxtypes.h"
  12. #include "mmboxfunction.h"
  13. #include "mmboxd.h"
  14. #include "load.h"
  15.  
  16. int main(int argc, char *argv[]);
  17.  
  18. void sighandler(int signal);
  19.  
  20.  
  21. int readInt();
  22.  
  23. pid_t readPid();
  24.  
  25. size_t readSize();
  26.  
  27. char* readString();
  28.  
  29. void writeInt(int num);
  30.  
  31. void writePid(pid_t num);
  32.  
  33. void writeChar(char c);
  34.  
  35. void writeString(int num,char *str);
  36.  
  37. void writeSize(size_t num);
  38.  
  39.  
  40. int to,from;
  41.  
  42. struct flock *f;
  43.  
  44. /*contiene la lista degli utenti*/
  45. user_t *userlist;
  46.  
  47. /*contiene n, viene settato a y se la lista degli utenti è modificata*/
  48. char mod='n';
  49.  
  50. int main(int argc, char *argv[]){
  51. /* installazione del segnale SIGTERM*/
  52. signal(SIGTERM, sighandler);
  53.  
  54. /* creazione delle fifo*/
  55. umask(S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  56. if ((mkfifo(FIFOTOMMBOX, FILE_MODE) < 0) && (errno != EEXIST)) {
  57. fprintf(stderr, "Errore creazione fifo.\n");
  58. exit(EXIT_FAILURE);
  59. }
  60. umask(S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  61. if ((mkfifo(FIFOFROMMMBOX, FILE_MODE) < 0) && (errno != EEXIST)) {
  62. fprintf(stderr, "Errore creazione fifo.\n");
  63. exit(EXIT_FAILURE);
  64. }
  65.  
  66. /* inizializzo la lista utenti*/
  67. userlist=openUsers();
  68. userlist->id=getppid();//assegno ad admin il pid di mmboxMan
  69. userlist->logged=1;
  70. userlist=openMsgs(userlist);
  71. /* vedere cosa devo farci*/
  72. f = malloc(sizeof(struct flock));
  73. if (f == NULL) {
  74. fprintf(stderr, "Errore allocazione di memoria\n");
  75. exit(EXIT_FAILURE);
  76. }
  77. memset(f, 0, sizeof(struct flock));
  78.  
  79. /* variabili usate nel while*/
  80. user_t *list;//lista di appoggio
  81. int cmd; //comando
  82. pid_t id;//identificatore
  83. char *username;
  84. char *password;
  85. char *obj;
  86. void *buf;
  87. pid_t res; //risultato del comando, è il primo valore che viene spedito
  88. int number;
  89. mail_t *msgs;
  90. user_t *user;
  91. int m=0;//usato in rcv
  92. /* ciclo infinito fino al segnale sigterm*/
  93. while(1){
  94.  
  95. list=userlist;
  96. /*apertura delle fifo*/
  97. to=open(FIFOTOMMBOX,O_RDWR);
  98. from=open(FIFOFROMMMBOX,O_WRONLY);
  99. if(to<0 || from<0) {
  100. unlink(FIFOFROMMMBOX);
  101. unlink(FIFOTOMMBOX);
  102. exit(EXIT_FAILURE);
  103. }
  104.  
  105. cmd=readInt();
  106. id=readPid();
  107. switch(cmd){
  108.  
  109. case CONNECT:
  110. /*sulla fifo devono essere spedit:
  111. -comando
  112. -pid
  113. -dim username
  114. -username
  115. */
  116. username=readString();
  117. res=connect(list,username,id);
  118. /*scrivo il risultato sulla fifo, il risultato è 0 se non è stato possibile connettersi, altrimenti è un identivicativo*/
  119. writePid(res);
  120. if(username!=NULL)free(username);
  121. break;
  122.  
  123. case LOGIN:
  124. /*sulla fifo devono essere spediti:d
  125. -comando
  126. -pid
  127. -dim password
  128. -password*/
  129. password=readString();
  130. list=login(list,password,id);
  131. if(list==NULL) writePid(0);//non è stato possibile effettuare la login
  132. else writePid(list->id);//scrive l'id se è stato possibile effettuare il login
  133. if(password!=NULL)free(password);
  134. break;
  135.  
  136. case QUIT:
  137. /*sulla fifo devono essere spediti:
  138. -comando
  139. -pid*/
  140. list=searchId(list,id);
  141. list=closedMsgs(list);
  142. list->logged--;//assumo che solo chi ha fatto login può chiamare quit
  143. if(list->logged==0) list->id=0;
  144. break;
  145. //non scrivo niente sulla fifo
  146.  
  147. case STAT_CLIENT:
  148. /*sulla fifo devono essere spediti:
  149. -comando
  150. -pid*/
  151. list=searchId(list,id);
  152. if(list==NULL){
  153. /*questa configuarazione indica che non è stato trovato il pid come loggato,
  154. non dovrebbe mai avvenire per controlli di mmbox*/
  155. writeInt(0); //free_space
  156. writeInt(0); //used_space
  157. writeInt(0); //message number
  158. writeInt(0); //dim username
  159. }
  160. else{
  161. writeInt((list->quota)-(list->used_space));//free_space
  162. writeInt(list->used_space);//used_space
  163. writeInt(list->msg_n); //number of messages
  164. writeString(strlen(list->username),list->username);//dimensione e username
  165. }
  166. break;
  167.  
  168. case LIST:
  169. /*ritorna tutti i messaggi della mailbox srive uso lo 0 come valore sentinella*/
  170. /*leggo dalla fifo:
  171. -comando
  172. -identificativo
  173. */
  174. msgs=searchMailsetI(list,id);
  175. list=userlist;
  176. if(msgs==NULL) {
  177. list=openMsgs(list);
  178. msgs=list->mailset;
  179. }
  180. /*scrivo sulla fifo le seguenti cose:*/
  181. while(msgs!=NULL){
  182. writeString(strlen(msgs->sender),msgs->sender);//sender
  183. //recipient è sempre lo stesso
  184. writeString(strlen(msgs->obj),msgs->obj);//object
  185. writeString(strlen(msgs->date),msgs->date);//date
  186. writeChar(msgs->r); //flag letto
  187. writeChar(msgs->m); //flag marcato
  188. writeSize(msgs->dim);//size
  189. msgs=msgs->next;
  190. }
  191. writeInt(0); //valore sentinella, indica che sono terminati i messaggi
  192. break;
  193.  
  194. case SEND:
  195. /*sulla fifo devono essere spediti:
  196. -comando
  197. -pid
  198. -dest(dim)
  199. -obj(dim)
  200. -dim msg
  201. -msg*/
  202. username=readString(); //dest
  203. obj=readString(); //object
  204. size_t size;
  205. size=readSize();
  206. int r,again;
  207. buf=NULL;/////
  208. buf=malloc(size);////////////
  209. if(buf==NULL) exit(EXIT_FAILURE);
  210. do {
  211. r = read(to,buf,size);
  212. again = (r < 0 && errno == EINTR);
  213. } while (again); //msg
  214. msgs=send(list,username,obj,buf,size,id);
  215. /*viene scritto 0 in caso di successo 1 altrimenti*/
  216. if(msgs==NULL) writeInt(1);
  217. else{
  218. /*in questo caso la send è andata a buon fine*/
  219. writeInt(0);
  220. list=userlist;
  221. list=searchUsername(list,username);
  222. list->mailset=msgs;
  223. list->mailset_changed='y';//TODO ridondante (lo stesso c'è anche nella send di mmboxfunction)
  224. }
  225. if(obj!=NULL) free(obj);
  226. break;
  227.  
  228. case RCV:
  229. /*sulla fifo devono essere spediti:
  230. -pid
  231. -comando
  232. -id msg
  233. -size*/
  234. number=readInt();
  235. size_t s;
  236. s=readSize();//dal manager mando 0
  237. if(s==0)m=1;
  238. list=searchId(list,id);
  239. list->mailset_changed='y';
  240. if(list->logged==0 && list->mailset==NULL)list=openMsgs(list);
  241. buf=receive(list,id,number,buf,s);
  242. /*sulla fifo viene spedito 0 + buf se l'operazione è andata a buon fine altrimenti viene spedito 1*/
  243. if(buf!=NULL){
  244. writeInt(0);
  245. int w,again=0;
  246. if(m==1) {
  247. msgs=searchMsgId(list->mailset,number);
  248. writeSize(msgs->dim);
  249. s=msgs->dim;
  250. }
  251. do {
  252. w = write(from,buf,s);
  253. again = (w < 0 && errno == EINTR);
  254. } while (again);
  255. break;
  256. }
  257. else{
  258. writeInt(1);
  259. }
  260. break;
  261.  
  262. case DELETE:
  263. /*sulla fifo devono essere spediti:
  264. -pid
  265. -comando
  266. -id msg*/
  267. number=readInt();
  268. number=delete(list,id,number);
  269. writeInt(number);
  270. if (number==0) {
  271. list=searchId(list,id);
  272. list->mailset_changed='y';
  273. }
  274.  
  275. break;
  276.  
  277. case RESUME:
  278. /*sulla fifo devono essere spediti:
  279. -pid
  280. -comando
  281. -id msg*/
  282. number=readInt();
  283. number=resume(list,id,number);
  284. if(number>0)writeInt(0);
  285. else writeInt(1);
  286. break;
  287.  
  288. case STAT_MANAGER:
  289. /*se è specificato l'username lo leggo*/
  290. /*sulla fifo devono essere spediti
  291. -pid
  292. -comando
  293. -username(*)*/
  294. username=readString();
  295. while(list!=NULL){
  296. /*sulla fifo scrivo utente e relativa quota se l'username è uguale a quello in considerazione spedisco anche used space*/
  297. writeString(strlen(list->username),list->username);
  298. writeInt(list->quota);
  299. if(username!=NULL && strcmp(username,list->username)==0){
  300. if(list->mailset==NULL) {
  301. user=openMsgs(list);
  302. list=user;
  303. }
  304. writeInt(list->used_space);
  305. writeInt(list->quota-list->used_space);
  306. }
  307. list=list->next;
  308. }
  309. writeInt(0);
  310. if(username!=NULL)free(username);
  311. break;
  312.  
  313. case ADDUSER:
  314. /*sulla fifo devono essere spediti
  315. -pid
  316. -comando
  317. -username*/
  318. username=readString();
  319. list=addUser(list,username);
  320. /*vuol dire che l'add è andata a buon fine*/
  321. if(list!=NULL) {
  322. userlist=list;
  323. writeInt(0); //scriviamo che l'operazione è andata a buon fine
  324. }
  325. else{
  326. writeInt(1); //scriviamo che l'operazione non è andata a buon fine
  327. }
  328. if(username!=NULL)free(username);
  329. mod='y';
  330. break;
  331.  
  332. case PASSWD:
  333. /*sulla fifo devono essere spediti
  334. -pid
  335. -comando
  336. -username
  337. -password*/
  338. username=readString();//username
  339. buf=readString();//password
  340. list=addPassword(list,username,buf);
  341. if(list!=NULL) {
  342. userlist=list;
  343. writeInt(0); //scriviamo che l'operazione è andata a buon fine
  344. }
  345. else{
  346. writeInt(1); //scriviamo che l'operazione non è andata a buon fine
  347. }
  348. if(username != NULL) free(username);
  349. mod='y';
  350. break;
  351.  
  352. case DELUSER:
  353. /*sulla fifo devono essere spediti
  354. -pid
  355. -comando
  356. -username*/
  357. username=readString();
  358. list=deleteUser(list,username);
  359. if(list!=NULL) {
  360. userlist=list;
  361. writeInt(0); //scriviamo che l'operazione è andata a buon fine
  362. }
  363. else{
  364. writeInt(1); //scriviamo che l'operazione non è andata a buon fine
  365. }
  366. if(username != NULL) free(username);
  367. mod='y';
  368. break;
  369.  
  370. case QMOD:
  371. /*sulla fifo devono essere spediti
  372. -pid
  373. -comando
  374. -username
  375. -newsize*/
  376. username=readString();
  377. number=readInt();//newsize
  378. writeInt(qmod(list,username,number));
  379. mod='y';
  380. break;
  381.  
  382. }//fine switch
  383. /* chiusura delle fifo*/
  384. if(close(to)<0 || close(from)){
  385. unlink(FIFOFROMMMBOX);
  386. unlink(FIFOTOMMBOX);
  387. exit(EXIT_FAILURE);
  388. }
  389.  
  390. } //FINE CICLO WHILE
  391. } //END
  392.  
  393.  
  394. /*legge un intero dalla fifo*/
  395. int readInt(){
  396. int num,again=0,r;
  397. do {
  398. r = read(to, &num, sizeof(int));
  399. again = (r < 0 && errno == EINTR);
  400. } while (again);
  401. if (r < 0) {
  402. close(to);
  403. close(from);
  404. unlink(FIFOTOMMBOX);
  405. unlink(FIFOFROMMMBOX);
  406. exit(EXIT_FAILURE);
  407. }
  408. return num;
  409. }
  410.  
  411. /*legge un numero di tipo pid dalla fifo*/
  412. pid_t readPid(){
  413. pid_t pid;
  414. int again=0,r;
  415. do {
  416. r = read(to, &pid, sizeof(pid_t));
  417. again = (r < 0 && errno == EINTR);
  418. }
  419. while (again);
  420. if (r < 0) {
  421. close(to);
  422. close(from);
  423. unlink(FIFOTOMMBOX);
  424. unlink(FIFOFROMMMBOX);
  425. exit(EXIT_FAILURE);
  426. }
  427. return pid;
  428. }
  429.  
  430. /*legge un intero di tipo size_t*/
  431. size_t readSize(){
  432. size_t size;
  433. int again=0,r;
  434. do {
  435. r = read(to, &size, sizeof(size_t));
  436. again = (r < 0 && errno == EINTR);
  437. } while (again);
  438. if (r < 0) {
  439. close(to);
  440. close(from);
  441. unlink(FIFOTOMMBOX);
  442. unlink(FIFOFROMMMBOX);
  443. exit(EXIT_FAILURE);
  444. }
  445. return size;
  446. }
  447.  
  448. /*legge una stringa dalla fifo (prima legge la dimensione poi la stringa)*/
  449. char* readString(){
  450. int again=0,r,size;
  451. size=readInt();
  452. if(size==0) return NULL;
  453. char *str;
  454. str=(char*)malloc(size*sizeof(char)+1);
  455. if(str==NULL) {
  456. exit(EXIT_FAILURE);
  457. }
  458. do {
  459. r = read(to, str, size*sizeof(char));
  460. again = (r < 0 && errno == EINTR);
  461. } while (again);
  462. if (r < 0) {
  463. close(to);
  464. close(from);
  465. unlink(FIFOTOMMBOX);
  466. unlink(FIFOFROMMMBOX);
  467. exit(EXIT_FAILURE);
  468. }
  469. str[size]='\0';
  470. return str;
  471. }
  472.  
  473. /*scrive un intero sulla fifo*/
  474. void writeInt(int num){
  475. int again=0,w;
  476. do {
  477. w = write(from,&num, sizeof(int));
  478. again = (w < 0 && errno == EINTR);
  479. } while (again);
  480. if (w < 0) {
  481. close(to);
  482. close(from);
  483. unlink(FIFOTOMMBOX);
  484. unlink(FIFOFROMMMBOX);
  485. exit(EXIT_FAILURE);
  486. }
  487. }
  488.  
  489. /*scrive un pid sulla fifo*/
  490. void writePid(pid_t num){
  491. int again=0,w;
  492. do {
  493. w = write(from,&num, sizeof(pid_t));
  494. again = (w < 0 && errno == EINTR);
  495. } while (again);
  496. if (w < 0) {
  497. close(to);
  498. close(from);
  499. unlink(FIFOTOMMBOX);
  500. unlink(FIFOFROMMMBOX);
  501. exit(EXIT_FAILURE);
  502. }
  503. }
  504.  
  505. /*scrive un elemento di tipo size_t*/
  506. void writeSize(size_t num){
  507. int again=0,w;
  508. do {
  509. w = write(from,&num, sizeof(size_t));
  510. again = (w < 0 && errno == EINTR);
  511. } while (again);
  512. if (w < 0) {
  513. close(to);
  514. close(from);
  515. unlink(FIFOTOMMBOX);
  516. unlink(FIFOFROMMMBOX);
  517. exit(EXIT_FAILURE);
  518. }
  519. }
  520.  
  521. /*scrive un char*/
  522. void writeChar(char c){
  523. int again=0,w;
  524. do {
  525. w = write(from,&c, sizeof(char));
  526. again = (w < 0 && errno == EINTR);
  527. } while (again);
  528. if (w < 0) {
  529. close(to);
  530. close(from);
  531. unlink(FIFOTOMMBOX);
  532. unlink(FIFOFROMMMBOX);
  533. exit(EXIT_FAILURE);
  534. }
  535. }
  536.  
  537. /* scrive una stringa*/
  538. void writeString(int num,char *str){
  539. int again=0,w;
  540. writeInt(num);
  541. do {
  542. w = write(from,str, num*sizeof(char));
  543. again = (w < 0 && errno == EINTR);
  544. } while (again);
  545. if (w < 0) {
  546. close(to);
  547. close(from);
  548. unlink(FIFOTOMMBOX);
  549. unlink(FIFOFROMMMBOX);
  550. exit(EXIT_FAILURE);
  551. }
  552. }
  553.  
  554. void sighandler(int signal) {
  555. if (signal == SIGTERM){
  556. /* if (f != NULL) {*/
  557. /* free(f);*/
  558. /* f = NULL;*/
  559. /* }*/
  560. user_t *user;
  561. user=userlist;
  562. while(user!=NULL){
  563. user=closedMsgs(user);
  564. user=user->next;
  565. }
  566. if(mod=='y')
  567. closedUsers(userlist);
  568. close(to);
  569. close(from);
  570. unlink(FIFOTOMMBOX);
  571. unlink(FIFOFROMMMBOX);
  572. exit(EXIT_SUCCESS);
  573. }
  574. }
Add Comment
Please, Sign In to add comment