Masanobu Yasui authored on 2009/06/01 15:25:31
Showing 8 changed files
... ...
@@ -108,7 +108,6 @@ int workend(mcomm *c)
108 108
         cprintf(0, c, "password: \x1b]E");
109 109
       }else{
110 110
         cprintf(0,c,"> ");
111
-        lprintf(1,"mexec: ======= separator =======\n");
112 111
       }
113 112
     }
114 113
   }
... ...
@@ -564,8 +563,7 @@ int ack_check(mfile *m, int state)
564 564
     if(!m->sendto){
565 565
       s = get_hoststate(t,m);
566 566
       if(!s){
567
-        lprintf(0,"[error] %s: can't get state area host=%s fn=%s\n", 
568
-          __func__, t->hostname, m->fn);
567
+        lprintf(0,"[error] %s: can't get state area host=%s fn=%s\n", __func__, t->hostname, m->fn);
569 568
       }else{
570 569
         if(*s == state){
571 570
           return(1);
... ...
@@ -575,8 +573,7 @@ int ack_check(mfile *m, int state)
575 575
       if(!memcmp(&(m->addr.sin_addr), &(t->ad), sizeof(m->addr.sin_addr))){
576 576
         s = get_hoststate(t,m);
577 577
         if(!s){
578
-          lprintf(0,"[error] %s: can't get state area host=%s fn=%s\n", 
579
-            __func__, t->hostname, m->fn);
578
+          lprintf(0,"[error] %s: can't get state area host=%s fn=%s\n", __func__, t->hostname, m->fn);
580 579
         }else{
581 580
           if(*s == state){
582 581
             return(1);
... ...
@@ -660,17 +657,12 @@ int is_reg(char *path)
660 660
   return(0);
661 661
 }
662 662
 
663
-int set_guid(uid_t uid, gid_t gid, gid_t *gids)
663
+int set_guid(uid_t uid, gid_t gid, size_t gidn, gid_t *gids)
664 664
 {
665
-  size_t num;
666
-
667 665
   /*----- setgids -----*/
668
-  if(gids){
669
-    for(num=0;gids[num];num++);
670
-    if(num){
671
-      if(setgroups(num, gids) == -1){
672
-        return(-1);
673
-      }
666
+  if(gidn && gids){
667
+    if(setgroups(gidn, gids) == -1){
668
+      return(-1);
674 669
     }
675 670
   }else{
676 671
     if(gid != getegid()){
... ...
@@ -699,10 +691,25 @@ int set_guid(uid_t uid, gid_t gid, gid_t *gids)
699 699
 int set_gids(char *groups)
700 700
 {
701 701
   char *p;
702
+  gid_t gid;
702 703
   size_t num;
703 704
   struct group *g;
704 705
   char buff[1024];
705 706
 
707
+  if(moption.gids){
708
+    free(moption.gids);
709
+  }
710
+  moption.gids = NULL;
711
+  moption.gidn = 0;
712
+
713
+  if(!groups){
714
+    return(0);
715
+  }
716
+
717
+  if(strlen(groups) >= sizeof(buff)){
718
+    return(-1);
719
+  }
720
+
706 721
   num = 0;
707 722
   strcpy(buff, groups);
708 723
   p = strtok(buff,",");
... ...
@@ -710,30 +717,35 @@ int set_gids(char *groups)
710 710
     p = strtok(NULL,",");
711 711
     num++;
712 712
   }
713
-  if(moption.gids){
714
-    free(moption.gids);
713
+  if(!num){
714
+    return(0);
715 715
   }
716
-  moption.gids = malloc(sizeof(gid_t) * (num + 1));
716
+  moption.gidn = num;
717
+  moption.gids = malloc(sizeof(gid_t) * num);
717 718
  
718 719
   num = 0; 
719 720
   strcpy(buff, groups);
720 721
   p = strtok(buff,",");
721 722
   while(p){
722 723
     if(*p >= '0' && *p <= '9'){
723
-      moption.gids[num] = atoi(p);
724
-      if(g = getgrgid(moption.gids[num])){
724
+      gid = atoi(p);
725
+      if(g = getgrgid(gid)){
726
+        moption.gids[num] = gid;
725 727
         strcpy(moption.grnames[num], g->gr_name);
728
+      }else{
729
+        return(-1);
726 730
       }
727 731
     }else{
728 732
       if(g = getgrnam(p)){
729 733
         moption.gids[num] = g->gr_gid;
730 734
         strcpy(moption.grnames[num], p);
735
+      }else{
736
+        return(-1);
731 737
       }
732 738
     }
733 739
     p = strtok(NULL,",");
734 740
     num++;
735 741
   }
736
-  moption.gids[num] = 0;
737 742
   return(0);
738 743
 }
739 744
 
... ...
@@ -241,14 +241,22 @@ void do_exechk(mcomm *c){
241 241
   int    i;
242 242
   mfile *m;
243 243
   for(i=0;i<MAX_COMM;i++){
244
-    if(c[i].working && !c[i].cpid && (c[i].fd[1] == -1)){
245
-      for(m=mftop[MFSEND];m;m=m->next){
246
-        if(m->comm == &c[i]){
247
-          break; /* working */
244
+    if(c[i].working){
245
+      if(c[i].isalive){
246
+        if(mtimeout(&(c[i].tv), (uint32_t)1000)){
247
+          cprintf(0, &(c[i]), "alive\n");
248
+          mtimeget(&(c[i].tv));
248 249
         }
249 250
       }
250
-      if(!m){
251
-        workend(&c[i]);
251
+      if((c[i].cpid == 0) && (c[i].fd[1] == -1)){
252
+        for(m=mftop[MFSEND];m;m=m->next){
253
+          if(m->comm == &c[i]){
254
+            break; /* working */
255
+          }
256
+        }
257
+        if(!m){
258
+          workend(&c[i]);
259
+        }
252 260
       }
253 261
     }
254 262
   }
... ...
@@ -281,13 +289,13 @@ int do_accept(mcomm *c, fd_set *fds)
281 281
   c[i].fd[0] = accept(s, (struct sockaddr *)&ss, &sslen);
282 282
   switch(ss.ss_family){
283 283
     case AF_UNIX:
284
-      lprintf(1, "%s: socket[%d]\n", __func__, i);
284
+      lprintf(5, "%s: socket=%d\n", __func__, i);
285 285
       break;
286 286
     case AF_INET:
287 287
     case AF_INET6:
288 288
       buff[0]=0;
289
-      getnameinfo((const struct sockaddr*)&ss, sizeof(ss), buff, sizeof(buff), NULL, NULL, NI_NUMERICHOST);
290
-      lprintf(1, "%s: socket[%d] from %s\n", __func__, i, buff);
289
+      getnameinfo((const struct sockaddr *)&ss, sizeof(ss), buff, sizeof(buff), NULL, 0, NI_NUMERICHOST);
290
+      lprintf(5, "%s: socket=%d from %s\n", __func__, i, buff);
291 291
       break;
292 292
   }
293 293
   c[i].working = 1;
... ...
@@ -175,6 +175,7 @@ typedef struct excludeitem
175 175
 
176 176
 typedef struct
177 177
 {
178
+  int no;
178 179
   int cpid;
179 180
   int fd[2];
180 181
   int size[2];
... ...
@@ -183,10 +184,13 @@ typedef struct
183 183
   int loglevel;
184 184
   int working;
185 185
   int authchk;
186
+  int isalive;
187
+  int logflag;
186 188
   char cmdline[2][MAKUO_BUFFER_SIZE];
187 189
   char parse[2][8][MAKUO_BUFFER_SIZE];
188 190
   char readbuff[2][MAKUO_BUFFER_SIZE];
189 191
   excludeitem *exclude;
192
+  struct timeval tv;
190 193
 } mcomm;
191 194
 
192 195
 typedef struct mmark
... ...
@@ -268,6 +272,7 @@ typedef struct
268 268
   uid_t uid;
269 269
   gid_t gid;
270 270
   gid_t *gids;
271
+  size_t gidn;
271 272
   char group_name[64];
272 273
   char user_name[64];
273 274
   char grnames[32][64];
... ...
@@ -335,7 +340,7 @@ int atomic_read(int fd, void *buff, int size, int nb);
335 335
 void set_filestat(char *path, uid_t uid, gid_t gid, mode_t mode);
336 336
 
337 337
 /*----- uid/gid -----*/
338
-int set_guid(uid_t uid, gid_t gid, gid_t *gids);
338
+int set_guid(uid_t uid, gid_t gid, size_t gidn, gid_t *gids);
339 339
 int set_gids(char *groups);
340 340
 
341 341
 /*----- member operation -----*/
... ...
@@ -18,6 +18,7 @@ char *command_list[]={"quit",
18 18
                       "echo",
19 19
                       "exclude",
20 20
                       "loglevel",
21
+                      "alive",
21 22
                       "help",
22 23
                       NULL};
23 24
 
... ...
@@ -241,6 +242,7 @@ int mexec_scan(mcomm *c, char *fn, mhost *h, int mode, gid_t gid)
241 241
     close(p[1]);
242 242
     c->cpid  = pid;
243 243
     c->fd[1] = p[0];
244
+    lprintf(9, "  %d: open\n", c->no);
244 245
     return(0);
245 246
   }else{
246 247
     /* child */
... ...
@@ -252,29 +254,48 @@ int mexec_scan(mcomm *c, char *fn, mhost *h, int mode, gid_t gid)
252 252
   return(0);
253 253
 }
254 254
 
255
+int mexec_open(int l, mcomm *c, int n)
256
+{
257
+  if(n){
258
+    lprintf(9, "  %d> %s\n", c->no, c->cmdline[n]);
259
+    return(0);
260
+  }
261
+  if(c->logflag == 0){
262
+    if(l <= moption.loglevel){
263
+      lprintf(1, "======= separator =======\n");
264
+      lprintf(1, "%d: open\n", c->no);
265
+      c->logflag = 1;
266
+    }
267
+  }
268
+  if(c->logflag){
269
+    lprintf(1, "-------------------------\n");
270
+    lprintf(1, "%d> %s\n", c->no, c->cmdline[n]);
271
+  }
272
+  return(0);
273
+}
274
+
255 275
 int mexec_close(mcomm *c, int n)
256 276
 {
257
-  int i;
258 277
   mfile *m;
259 278
 
260 279
   if(c->fd[n] != -1){
261 280
     close(c->fd[n]);
262
-    for(i=0;i<MAX_COMM;i++){
263
-      if(c == &(moption.comm[i])){
264
-        break;
265
-      }
266
-    }
267 281
     if(n){
268
-      lprintf(8, "%s: socket[%d] (backend)\n", __func__, i);
282
+      lprintf(9, "  %d: close\n", c->no);
269 283
     }else{
270
-      lprintf(1, "%s: socket[%d]\n", __func__, i);
284
+      if(c->logflag){
285
+        lprintf(1, "-------------------------\n");
286
+        lprintf(1, "%d: close\n", c->no);
287
+      }
271 288
     }
272 289
   }
273 290
   c->fd[n]  = -1;
274 291
   c->size[n] = 0;
275 292
   if(!n){
276 293
     c->authchk  = 0;
294
+    c->logflag  = 0;
277 295
     c->loglevel = 0;
296
+    c->isalive  = 0;
278 297
     if(c->cpid){
279 298
       kill(c->cpid, SIGTERM);
280 299
       mexec_close(c, 1);
... ...
@@ -310,6 +331,7 @@ int mexec_help(mcomm *c, int n)
310 310
   cprintf(0, c, "  sync  [-r] [-t host] [-n] [path]\n");
311 311
   cprintf(0, c, "  dsync [-r] [-t host] [-n] [path]\n");
312 312
   cprintf(0, c, "  check [-r] [-t host] [path]\n");
313
+  cprintf(0, c, "  alive on|off\n");
313 314
   cprintf(0, c, "  loglevel num (0-9)\n");
314 315
   cprintf(0, c, "  members\n");
315 316
   cprintf(0, c, "  help\n");
... ...
@@ -358,14 +380,16 @@ int mexec_send(mcomm *c, int n, int sync)
358 358
         if(*optarg >= '0' && *optarg <='9'){
359 359
           gid = atoi(optarg);
360 360
         }else{
361
-          if(moption.gids){
362
-            for(j=0;moption.gids[j];j++){
363
-              if(!strcmp(optarg, moption.grnames[j])){
364
-                gid = moption.gids[j];
365
-                break;
366
-              }
361
+          for(j=0;j<moption.gidn;j++){
362
+            if(!strcmp(optarg, moption.grnames[j])){
363
+              gid = moption.gids[j];
364
+              break;
367 365
             }
368 366
           }
367
+          if(gid == -1){
368
+            cprintf(0, c, "nou found %s group\n", optarg);
369
+            return(0);
370
+          }
369 371
         }
370 372
         break;
371 373
       case 't':
... ...
@@ -501,7 +525,8 @@ int mexec_send(mcomm *c, int n, int sync)
501 501
   if((m->dryrun == 0) && (gid != -1)){
502 502
     if(m->fs.st_gid != gid){
503 503
       if(lchown(m->fn, -1, gid) == -1){
504
-        lprintf(0, "%s: chgrp error (%s) [%d->%d] %s\n", __func__, strerror(errno),  m->fs.st_gid, gid, m->fn);
504
+        cprintf(0, c,   "error: can't chgrp (%s) [%d->%d] %s\n", strerror(errno),  m->fs.st_gid, gid, m->fn);
505
+        lprintf(0, "[error] %s: can't chgrp (%s) [%d->%d] %s\n", __func__, strerror(errno),  m->fs.st_gid, gid, m->fn);
505 506
       }else{
506 507
         m->fs.st_gid = gid;
507 508
       }
... ...
@@ -926,6 +951,22 @@ int mexec_status(mcomm *c, int n)
926 926
   return(0);
927 927
 }
928 928
 
929
+int mexec_alive(mcomm *c, int n)
930
+{
931
+  if(c->argc[n] != 2){
932
+    return(mexec_help(c, n));
933
+  }
934
+  if(!strcmp("on", c->parse[n][1])){
935
+    c->isalive = 1;
936
+    return(0);
937
+  }
938
+  if(!strcmp("off", c->parse[n][1])){
939
+    c->isalive = 0;
940
+    return(0);
941
+  }
942
+  return(mexec_help(c, n));
943
+}
944
+
929 945
 int mexec_password(char *password)
930 946
 {
931 947
   unsigned char digest[16];
... ...
@@ -1013,17 +1054,20 @@ int mexec(mcomm *c, int n)
1013 1013
   mfile *m   = NULL;
1014 1014
   int count  = 0;
1015 1015
 
1016
-  if(n == 0 && c->working){
1017
-    c->size[n] = 0;
1018
-    r = read(c->fd[n], buff, size);
1019
-    if(r>0){
1020
-    }else{
1021
-      if(r == -1){
1022
-        lprintf(0, "[error] %s: read error n=%d\n", __func__, n);
1016
+  if(n == 0){
1017
+    mtimeget(&(c->tv));
1018
+    if(c->working){
1019
+      c->size[n] = 0;
1020
+      r = read(c->fd[n], buff, size);
1021
+      if(r>0){
1022
+      }else{
1023
+        if(r == -1){
1024
+          lprintf(0, "[error] %s: read error n=%d fd=%d\n", __func__, n, c->fd[n]);
1025
+        }
1026
+        mexec_close(c, n);
1023 1027
       }
1024
-      mexec_close(c, n);
1028
+      return(-1);
1025 1029
     }
1026
-    return(-1);
1027 1030
   }
1028 1031
 
1029 1032
   if(n == 1){
... ...
@@ -1049,7 +1093,7 @@ int mexec(mcomm *c, int n)
1049 1049
       c->size[n] += r;
1050 1050
     }else{
1051 1051
       if(r < 0){
1052
-        lprintf(0, "%s: read error n=%d\n", __func__, n);
1052
+        lprintf(0, "[error] %s: read error(%s) n=%d fd=%d\n", __func__, strerror(errno), n, c->fd[n]);
1053 1053
       }
1054 1054
       mexec_close(c, n);
1055 1055
       return(-1);
... ...
@@ -1069,51 +1113,67 @@ int mexec(mcomm *c, int n)
1069 1069
       cprintf(0, c, "> ");
1070 1070
     }
1071 1071
   }else{
1072
-    lprintf(1 + n * 7, "%s: %s\n", __func__, c->cmdline[n]);
1073 1072
     c->working = 1;
1074
-
1075
-    if(!strcmp("help",command_list[r]))
1076
-      return(mexec_help(c,n));
1077
-
1078
-    if(!strcmp("quit",command_list[r]))
1079
-      return(mexec_quit(c,n));
1080
-
1081
-    if(!strcmp("exit",command_list[r]))
1082
-      return(mexec_quit(c,n));
1083
-
1084
-    if(!strcmp("bye",command_list[r]))
1085
-      return(mexec_quit(c,n));
1086
-
1087
-    if(!strcmp("send",command_list[r]))
1088
-      return(mexec_send(c,n,0));
1089
-
1090
-    if(!strcmp("sync",command_list[r]))
1091
-      return(mexec_send(c,n,1));
1092
-
1093
-    if(!strcmp("md5",command_list[r]))
1094
-      return(mexec_check(c,n));
1095
-
1096
-    if(!strcmp("check",command_list[r]))
1097
-      return(mexec_check(c,n));
1098
-
1099
-    if(!strcmp("dsync",command_list[r]))
1100
-      return(mexec_dsync(c,n));
1101
-
1102
-    if(!strcmp("members",command_list[r]))
1103
-      return(mexec_members(c,n));
1104
-
1105
-    if(!strcmp("echo",command_list[r]))
1106
-      return(mexec_echo(c,n));
1107
-
1108
-    if(!strcmp("loglevel",command_list[r]))
1109
-      return(mexec_loglevel(c,n));
1110
-
1111
-    if(!strcmp("exclude",command_list[r]))
1112
-      return(mexec_exclude(c,n));
1113
-
1114
-    if(!strcmp("status",command_list[r]))
1115
-      return(mexec_status(c,n));
1116
-
1073
+    if(!strcmp("help", command_list[r])){
1074
+      mexec_open(1, c, n);
1075
+      return(mexec_help(c, n));
1076
+    }
1077
+    if(!strcmp("quit", command_list[r])){
1078
+      mexec_open(8, c, n);
1079
+      return(mexec_quit(c, n));
1080
+    }
1081
+    if(!strcmp("exit", command_list[r])){
1082
+      mexec_open(8, c, n);
1083
+      return(mexec_quit(c, n));
1084
+    }
1085
+    if(!strcmp("bye", command_list[r])){
1086
+      mexec_open(8, c, n);
1087
+      return(mexec_quit(c, n));
1088
+    }
1089
+    if(!strcmp("send", command_list[r])){
1090
+      mexec_open(1, c, n);
1091
+      return(mexec_send(c, n, 0));
1092
+    }
1093
+    if(!strcmp("sync", command_list[r])){
1094
+      mexec_open(1, c, n);
1095
+      return(mexec_send(c, n, 1));
1096
+    }
1097
+    if(!strcmp("md5", command_list[r])){
1098
+      mexec_open(1, c, n);
1099
+      return(mexec_check(c, n));
1100
+    }
1101
+    if(!strcmp("check", command_list[r])){
1102
+      mexec_open(1, c, n);
1103
+      return(mexec_check(c, n));
1104
+    }
1105
+    if(!strcmp("dsync", command_list[r])){
1106
+      mexec_open(1, c, n);
1107
+      return(mexec_dsync(c, n));
1108
+    }
1109
+    if(!strcmp("members", command_list[r])){
1110
+      mexec_open(5, c, n);
1111
+      return(mexec_members(c, n));
1112
+    }
1113
+    if(!strcmp("echo", command_list[r])){
1114
+      mexec_open(5, c, n);
1115
+      return(mexec_echo(c, n));
1116
+    }
1117
+    if(!strcmp("loglevel", command_list[r])){
1118
+      mexec_open(5, c, n);
1119
+      return(mexec_loglevel(c, n));
1120
+    }
1121
+    if(!strcmp("exclude", command_list[r])){
1122
+      mexec_open(1, c, n);
1123
+      return(mexec_exclude(c, n));
1124
+    }
1125
+    if(!strcmp("status", command_list[r])){
1126
+      mexec_open(5, c, n);
1127
+      return(mexec_status(c, n));
1128
+    }
1129
+    if(!strcmp("alive", command_list[r])){
1130
+      mexec_open(8, c, n);
1131
+      return(mexec_alive(c, n));
1132
+    }
1117 1133
     c->working = 0;
1118 1134
   }
1119 1135
   return(r);
... ...
@@ -82,6 +82,7 @@ static void minit_option_setdefault()
82 82
   moption.gids                  = NULL;
83 83
   getcwd(moption.base_dir, PATH_MAX);
84 84
   for(i=0;i<MAX_COMM;i++){
85
+    moption.comm[i].no = i;
85 86
     moption.comm[i].fd[0] = -1;
86 87
     moption.comm[i].fd[1] = -1;
87 88
   }
... ...
@@ -93,6 +94,9 @@ static void minit_option_getenv()
93 93
   struct passwd *pw;
94 94
   struct group  *gr;
95 95
 
96
+  if(env=getenv("MAKUOSAN_BASE")){
97
+    realpath(env, moption.base_dir);
98
+  }
96 99
   if(env=getenv("MAKUOSAN_PORT")){
97 100
     moption.maddr.sin_port = htons(atoi(env));
98 101
     moption.laddr.sin_port = htons(atoi(env));
... ...
@@ -247,6 +251,9 @@ static void minit_getopt(int argc, char *argv[])
247 247
           if(pw = getpwnam(optarg)){
248 248
             moption.uid = pw->pw_uid;
249 249
             moption.gid = pw->pw_gid;
250
+          }else{
251
+            lprintf(0, "%s: not found user %s\n", __func__, optarg);
252
+            exit(1);
250 253
           }
251 254
         }
252 255
         break;
... ...
@@ -257,13 +264,16 @@ static void minit_getopt(int argc, char *argv[])
257 257
         }else{
258 258
          if(gr = getgrnam(optarg)){
259 259
             moption.gid = gr->gr_gid;
260
+          }else{
261
+            lprintf(0, "%s: not found group %s\n", __func__, optarg);
262
+            exit(1);
260 263
           }
261 264
         }
262 265
         break;
263 266
 
264 267
       case 'G':
265 268
         if(set_gids(optarg) == -1){
266
-          lprintf(0, "%s: set gids error\n", __func__);
269
+          lprintf(0, "%s: can't set gids %s\n", __func__, optarg);
267 270
           exit(1);
268 271
         }
269 272
         break;
... ...
@@ -448,10 +458,10 @@ static void minit_chroot()
448 448
 static void minit_setguid()
449 449
 {
450 450
   size_t num;
451
-  if(set_guid(moption.uid, moption.gid, moption.gids) == -1){
451
+  if(set_guid(moption.uid, moption.gid, moption.gidn, moption.gids) == -1){
452 452
     fprintf(stderr, "%s: can't setguid %d:%d", __func__, moption.uid, moption.gid);
453
-    if(moption.gids){
454
-      for(num=0;moption.gids[num];num++){
453
+    if(moption.gidn){
454
+      for(num=0;num<moption.gidn;num++){
455 455
         fprintf(stderr, ",%d", moption.gids[num]);
456 456
       }
457 457
     }
... ...
@@ -1242,11 +1242,11 @@ static void mrecv_req_del_data_report(mfile *m, mcomm *c, uint32_t err, char *hn
1242 1242
     dryrun = "(dryrun) ";
1243 1243
   }
1244 1244
   if(err){
1245
-    cprintf(0, c,  "(%s) delete error %s:%s\n", strerror(err), hn, path);
1246
-    lprintf(1, "%s: (%s) delete error %s:%s\n", __func__, strerror(err), hn, path);
1245
+    cprintf(0, c,  "delete error (%s) %s:%s\n", strerror(err), hn, path);
1246
+    lprintf(1, "delete error (%s) %s:%s\n", strerror(err), hn, path);
1247 1247
   }else{
1248 1248
     cprintf(0, c,  "%sdelete %s:%s\n", dryrun, hn, path);
1249
-    lprintf(1, "%s: %sdelete %s:%s\n", __func__, dryrun, hn, path);
1249
+    lprintf(1, "%sdelete %s:%s\n", dryrun, hn, path);
1250 1250
   }
1251 1251
 }
1252 1252
 
... ...
@@ -554,7 +554,7 @@ static void msend_req_send_filedata(int s, mfile *m)
554 554
       cprintf(0, m->comm, "error: can't read (%s) seqno=%d %s\n", strerror(errno), m->mdata.head.seqno, m->fn);
555 555
     }else{
556 556
       /* eof */
557
-      lprintf(4, "%s: block send count=%d %s\n", __func__, m->mdata.head.seqno, m->fn);
557
+      lprintf(9, "%s: block send count=%d %s\n", __func__, m->mdata.head.seqno, m->fn);
558 558
       m->mdata.head.seqno  = 0;
559 559
       m->mdata.head.nstate = MAKUO_SENDSTATE_MARK;
560 560
       m->initstate = 1;
... ...
@@ -638,7 +638,7 @@ static void msend_req_send_close(int s, mfile *m)
638 638
   if(m->mdata.head.ostate == MAKUO_SENDSTATE_MARK || 
639 639
      m->mdata.head.ostate == MAKUO_SENDSTATE_DATA ||
640 640
      m->mdata.head.ostate == MAKUO_SENDSTATE_OPEN){
641
-    lprintf(6,"%s: update complate %s \n", __func__, m->fn);
641
+    lprintf(4, "update complate %s\n", m->fn);
642 642
   }
643 643
   m->initstate = 1;
644 644
   m->mdata.head.nstate = MAKUO_SENDSTATE_LAST;
... ...
@@ -20,7 +20,23 @@
20 20
 #include <sys/un.h>
21 21
 #include "makuosan.h"
22 22
 
23
-excludeitem *exclude = NULL;
23
+typedef struct msyncdata
24
+{
25
+  int s;                /* */
26
+  int loopflag;         /* */
27
+  int loglevel;         /* */
28
+  int sendflag;         /* */
29
+  int delflag;          /* */
30
+  int grpflag;          /* */
31
+  char scfile[256];     /* */
32
+  char passwd[256];     /* */
33
+  char target[256];     /* */
34
+  char mcmd[256];       /* */
35
+  char mopt[256];       /* */
36
+  char sopt[256];       /* */
37
+  char path[PATH_MAX];  /* */
38
+  excludeitem *exclude; /* */
39
+} msyncdata;
24 40
 
25 41
 void usage()
26 42
 {
... ...
@@ -53,18 +69,18 @@ void usage()
53 53
   printf("\n");
54 54
 }
55 55
 
56
-excludeitem *add_exclude(char *pattern)
56
+excludeitem *add_exclude(msyncdata *md, char *pattern)
57 57
 {
58 58
   excludeitem *item = malloc(sizeof(excludeitem));
59 59
   item->pattern = malloc(strlen(pattern) + 1);
60 60
   strcpy(item->pattern, pattern);
61 61
   item->prev = NULL;
62 62
   item->next = NULL;
63
-  if(exclude){
64
-    exclude->prev = item;
65
-    item->next = exclude;
63
+  if(md->exclude){
64
+    md->exclude->prev = item;
65
+    item->next = md->exclude;
66 66
   }
67
-  exclude = item;
67
+  md->exclude = item;
68 68
   return(item);
69 69
 }
70 70
 
... ...
@@ -214,31 +230,41 @@ int readline(int s, char *buff, int size, int prompt, char *passwd)
214 214
   return(-1); /* over flow */
215 215
 }
216 216
 
217
-int wait_prompt(int s, char *passwd){
217
+int wait_prompt(int s, char *passwd, int view, int *line){
218 218
   int  r;
219 219
   char buff[8192];
220
-
221 220
   while(r = readline(s, buff, sizeof(buff), 1, passwd)){
222 221
     if(r == -1){
223 222
       /* read error */
224
-      return(-1);
223
+      r = -1;
224
+      break;
225 225
     }
226 226
     if(r == -2){
227 227
       /* return prompt */
228
-      return(1);
228
+      r = 1;
229
+      break;
229 230
     }
230
-    if(!memcmp(buff, "error:", 6)){
231
-      fprintf(stderr, "%s\n", buff);
232
-    }else{
233
-      fprintf(stdout, "%s\n", buff);
231
+    if(!memcmp(buff, "alive", 5)){
232
+      continue;
233
+    }
234
+    if(line){
235
+      (*line)++;
236
+    }
237
+    if(view){
238
+      if(!memcmp(buff, "error:", 6)){
239
+        fprintf(stderr, "%s\n", buff);
240
+      }else{
241
+        fprintf(stdout, "%s\n", buff);
242
+      }
234 243
     }
235 244
   }
236
-  return(0);
245
+  return(r);
237 246
 } 
238 247
 
239
-int makuo(int s, char *c)
248
+int makuo(int s, char *c, int view)
240 249
 {
241 250
   int  r;
251
+  int  line = 1;
242 252
   char buff[256];
243 253
   if(sizeof(buff) < strlen(c) + 2){
244 254
     fprintf(stderr, "error: command too long\n");
... ...
@@ -249,54 +275,77 @@ int makuo(int s, char *c)
249 249
     fprintf(stderr, "error: can't write socket\n");
250 250
     return(-1);
251 251
   }
252
-  r = wait_prompt(s, NULL);
252
+  r = wait_prompt(s, NULL, view, &line);
253 253
   if(r == -1){
254 254
     fprintf(stderr, "error: can't read socket\n");
255 255
     return(-1);
256 256
   }
257
-  return(r);
257
+  if(r == 0){
258
+    return(0);
259
+  }
260
+  return(line);
261
+}
262
+
263
+void makuo_aliveon(msyncdata *md)
264
+{
265
+  int r;
266
+  char cmd[256];
267
+  struct timeval tv;
268
+  sprintf(cmd, "alive on");
269
+  r = makuo(md->s, cmd, 0);
270
+  if(r == 0){
271
+    exit(1);
272
+  }
273
+  if(r == -1){
274
+    exit(1);
275
+  }
276
+  if(r == 1){
277
+    tv.tv_sec  = 30;
278
+    tv.tv_usec = 0;
279
+  }else{
280
+    tv.tv_sec  = 0;
281
+    tv.tv_usec = 0;
282
+  }
283
+  setsockopt(md->s, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
258 284
 }
259 285
 
260
-int makuo_log(int s, int l)
286
+void makuo_log(msyncdata *md)
261 287
 {
262 288
   int r;
263 289
   char cmd[256];
264
-  sprintf(cmd, "loglevel %d", l);
265
-  r = makuo(s, cmd);
290
+  sprintf(cmd, "loglevel %d", md->loglevel);
291
+  r = makuo(md->s, cmd, 0);
266 292
   if(r == 0){
267 293
     fprintf(stderr, "error: remote close\n");
268
-    return(1);
294
+    exit(1);
269 295
   }
270 296
   if(r == -1){
271
-    return(1);
297
+    exit(1);
272 298
   }
273
-  return(0);
274 299
 }
275 300
 
276
-int makuo_exclude(int s)
301
+void makuo_exclude(msyncdata *md)
277 302
 {
278 303
   int r;
279 304
   char cmd[1024];
280 305
   excludeitem *item;
281
-  for(item=exclude;item;item=item->next){
306
+  for(item=md->exclude;item;item=item->next){
282 307
     sprintf(cmd, "exclude add %s", item->pattern);
283
-    r = makuo(s, cmd);
308
+    r = makuo(md->s, cmd, 0);
284 309
     if(r == 0){
285 310
       fprintf(stderr, "error: makuosan remote close. (%s)\n", cmd);
286
-      return(1);
311
+      exit(1);
287 312
     }
288 313
     if(r == -1){
289 314
       fprintf(stderr, "error: makuosan socket error. (%s)\n", cmd);
290
-      return(1);
315
+      exit(1);
291 316
     }
292 317
   }
293
-  return(0);
294 318
 }
295 319
 
296
-
297 320
 int makuo_exec(int s, char *cmd)
298 321
 { 
299
-  int r = makuo(s, cmd);
322
+  int r = makuo(s, cmd, 1);
300 323
   if(r == 0){
301 324
     fprintf(stderr, "error: makuosan remote close. (%s)\n", cmd);
302 325
     return(1);
... ...
@@ -308,10 +357,31 @@ int makuo_exec(int s, char *cmd)
308 308
   return(0);
309 309
 }
310 310
 
311
-int makuo_quit(int s)
311
+void makuo_send(msyncdata *md)
312
+{
313
+  char cmd[1024];
314
+  if(md->delflag){
315
+    sprintf(cmd, "dsync%s %s", md->mopt, md->path);
316
+    if(makuo_exec(md->s, cmd)){
317
+      close(md->s);
318
+      exit(1);
319
+    }
320
+  }
321
+  if(md->sendflag){
322
+    sprintf(cmd, "%s%s%s %s", md->mcmd, md->mopt, md->sopt, md->path);
323
+  }else{
324
+    sprintf(cmd, "%s%s %s", md->mcmd, md->mopt, md->path);
325
+  }
326
+  if(makuo_exec(md->s, cmd)){
327
+    close(md->s);
328
+    exit(1);
329
+  }
330
+}
331
+
332
+int makuo_quit(msyncdata *md)
312 333
 { 
313
-  int r = makuo(s, "quit");
314
-  close(s);
334
+  int r = makuo(md->s, "quit", 0);
335
+  close(md->s);
315 336
   if(r == 0){
316 337
     return(0); /* success */
317 338
   }
... ...
@@ -322,7 +392,7 @@ int makuo_quit(int s)
322 322
   return(1);
323 323
 }
324 324
 
325
-int exclude_from(char *filename)
325
+int exclude_from(msyncdata *md, char *filename)
326 326
 {
327 327
   int  f;
328 328
   int  r;
... ...
@@ -344,19 +414,24 @@ int exclude_from(char *filename)
344 344
       return(1);
345 345
     }
346 346
     if((*line != '\r') && (*line != '\n') && (*line !=0)){
347
-      add_exclude(line);
347
+      add_exclude(md, line);
348 348
     }
349 349
   }
350 350
   close(f);
351 351
   return(0);
352 352
 }
353 353
 
354
-int fromfile(int s, char *filename)
354
+int makuo_file(msyncdata *md)
355 355
 {
356 356
   int  f;
357 357
   int  r;
358 358
   char line[256];
359
+  char *filename;
359 360
 
361
+  filename = md->scfile;
362
+  if(!strlen(filename)){
363
+    return(0);
364
+  }
360 365
   if(!strcmp(filename, "-")){
361 366
     /* f = stdin */
362 367
     f = dup(0);
... ...
@@ -372,16 +447,15 @@ int fromfile(int s, char *filename)
372 372
   while(r = readline(f, line, sizeof(line), 0, NULL)){
373 373
     if(r == -1){
374 374
       fprintf(stderr, "file read error: %s\n", filename);
375
-      close(f);
376
-      return(1);
375
+      break;
377 376
     }
378
-    if(makuo_exec(s, line)){
377
+    if(makuo_exec(md->s, line)){
379 378
       close(f);
380
-      return(1);
379
+      exit(1);
381 380
     }
382 381
   }
383 382
   close(f);
384
-  return(makuo_quit(s));
383
+  return(1);
385 384
 }
386 385
 
387 386
 int loadpass(char *filename, char *passwd, int size)
... ...
@@ -402,48 +476,18 @@ int loadpass(char *filename, char *passwd, int size)
402 402
   return(0);
403 403
 }
404 404
 
405
-void defaulttarget(char *target, int size)
405
+void defaulttarget(msyncdata *md)
406 406
 {
407 407
   char *p = getenv("MSYNC_TARGET");
408
-  strcpy(target, "tcp:127.0.0.1:5000");
409
-  if(p && (strlen(p) < size)){
410
-    strcpy(target, p);
408
+  strcpy(md->target, "tcp:127.0.0.1:5000");
409
+  if(p && (strlen(p) < sizeof(md->target))){
410
+    strcpy(md->target, p);
411 411
   }
412 412
 }
413 413
 
414
-int main(int argc, char *argv[])
414
+struct option *optinit()
415 415
 {
416
-  int i;
417
-  int r;
418
-  int s;
419
-
420
-  if(argc == 1){
421
-    usage();
422
-    return(1);
423
-  }
424
-
425
-  /* makuo command */
426
-  char cmd[1024];
427
-  char mcmd[256];
428
-  char mopt[256];
429
-  char sopt[256];
430
-  strcpy(mcmd,"send");
431
-  strcpy(mopt,"");
432
-  strcpy(sopt,"");
433
-
434
-  /* option */
435
-  int loopflag = 1;
436
-  int loglevel = 0;
437
-  int sendflag = 1;
438
-  int delflag  = 0;
439
-  int grpflag  = 0;
440
-  char scfile[256];
441
-  char passwd[256];
442
-  char target[256];
443
-
444
-  /* long option */
445
-  struct option longopt[9];
446
-  memset(longopt, 0, sizeof(longopt));
416
+  static struct option longopt[9];
447 417
   longopt[0].name    = "help";
448 418
   longopt[0].has_arg = 0;
449 419
   longopt[0].flag    = NULL;
... ...
@@ -476,191 +520,197 @@ int main(int argc, char *argv[])
476 476
   longopt[7].has_arg = 0;
477 477
   longopt[7].flag    = NULL;
478 478
   longopt[7].val     = 'd';
479
+  longopt[8].name    = NULL;
480
+  longopt[8].has_arg = 0;
481
+  longopt[8].flag    = NULL;
482
+  longopt[8].val     = 0;
483
+  return(longopt);
484
+}
479 485
 
480
-  /* default */
481
-  scfile[0] = 0;
482
-  passwd[0] = 0;
483
-  defaulttarget(target, sizeof(target));
484
-
485
-  while((r = getopt_long(argc, argv, "g:c:f:t:K:l:hvrn", longopt, NULL)) != -1){
486
+void parse_opt(int argc, char *argv[], struct option *opt, msyncdata *md)
487
+{
488
+  int r;
489
+  while((r = getopt_long(argc, argv, "g:c:f:t:K:l:hvrn", opt, NULL)) != -1){
486 490
     switch(r){
487 491
       case 'h':
488 492
         usage();
489
-        return(0);
493
+        exit(0);
490 494
 
491 495
       case 'D':
492
-        delflag = 1;
496
+        md->delflag = 1;
493 497
         break;
494 498
 
495 499
       case 'd':
496
-        strcpy(mcmd, "sync");
500
+        strcpy(md->mcmd, "sync");
497 501
         break;
498 502
 
499 503
       case 'S':
500
-        strcpy(mcmd, "status");
501
-        loopflag = 0;
502
-        sendflag = 0;
504
+        strcpy(md->mcmd, "status");
505
+        md->loopflag = 0;
506
+        md->sendflag = 0;
503 507
         break;
504 508
 
505 509
       case 'M':
506
-        strcpy(mcmd, "members");
507
-        loopflag = 0;
508
-        sendflag = 0;
510
+        strcpy(md->mcmd, "members");
511
+        md->loopflag = 0;
512
+        md->sendflag = 0;
509 513
         break;
510 514
 
511 515
       case 'C':
512
-        strcpy(mcmd, "check");
513
-        sendflag = 0;
516
+        strcpy(md->mcmd, "check");
517
+        md->sendflag = 0;
514 518
         break;
515 519
 
516 520
       case 'E':
517
-        add_exclude(optarg);
521
+        add_exclude(md, optarg);
518 522
         break;
519 523
 
520 524
       case 'F':
521
-        if(exclude_from(optarg)){
522
-          return(1);
525
+        if(exclude_from(md, optarg)){
526
+          exit(1);
523 527
         }
524 528
         break;
525 529
 
526 530
       case 'r':
527
-        strcat(mopt," -r");
531
+        strcat(md->mopt," -r");
528 532
         break;
529 533
 
530 534
       case 'n':
531
-        strcat(mopt," -n");
535
+        strcat(md->mopt," -n");
532 536
         break;
533 537
 
534 538
       case 't':
535
-        strcat(mopt," -t ");
536
-        strcat(mopt,optarg);
539
+        strcat(md->mopt," -t ");
540
+        strcat(md->mopt,optarg);
537 541
         break;
538 542
 
539 543
       case 'g':
540
-        grpflag = 1;
541
-        strcat(sopt," -g ");
542
-        strcat(sopt,optarg);
544
+        md->grpflag = 1;
545
+        strcat(md->sopt," -g ");
546
+        strcat(md->sopt,optarg);
543 547
         break;
544 548
 
545 549
       case 'v':
546
-        loglevel++;
550
+        md->loglevel++;
547 551
         break;
548 552
 
549 553
       case 'l':
550
-        loglevel = atoi(optarg);
554
+        md->loglevel = atoi(optarg);
551 555
         break;
552 556
 
553 557
       case 'f':
554
-        if(strlen(optarg) < sizeof(scfile)){
555
-          strcpy(scfile, optarg);
558
+        if(strlen(optarg) < sizeof(md->scfile)){
559
+          strcpy(md->scfile, optarg);
556 560
         }else{
557 561
           fprintf(stderr, "filename too long\n");
558
-          return(1);
562
+          exit(1);
559 563
         }
560 564
         break;
561 565
 
562 566
       case 'c':
563
-        if(strlen(optarg) < sizeof(target)){
564
-          strcpy(target, optarg);
567
+        if(strlen(optarg) < sizeof(md->target)){
568
+          strcpy(md->target, optarg);
565 569
         }else{
566 570
           fprintf(stderr, "target too long\n");
567
-          return(1);
571
+          exit(1);
568 572
         }
569 573
         break;
570 574
 
571 575
       case 'K':
572
-        if(loadpass(optarg, passwd, sizeof(passwd)) == -1){
573
-          return(1);
576
+        if(loadpass(optarg, md->passwd, sizeof(md->passwd)) == -1){
577
+          exit(1);
574 578
         }
575 579
         break;
576 580
 
577 581
       case '?':
578 582
         usage();
579
-        return(1);
583
+        exit(1);
580 584
         break;
581 585
     }
582 586
   }
583
-
584
-  if(delflag && !sendflag){
587
+  if(md->delflag && !md->sendflag){
585 588
     usage();
586
-    return(1);
589
+    exit(1);
587 590
   }
588
-
589
-  if(grpflag && !sendflag){
591
+  if(md->grpflag && !md->sendflag){
590 592
     usage();
591
-    return(1);
593
+    exit(1);
592 594
   }
593
-
594
-  s = connect_socket(target);
595
-  if(s == -1){
596
-    fprintf(stderr, "can't connect %s\n", target);
597
-    return(1);
595
+  if(argc == optind){
596
+    md->loopflag = 0;
598 597
   }
598
+}
599 599
 
600
-  r = wait_prompt(s, passwd);
600
+int connect_wait(msyncdata *md)
601
+{
602
+  int r;
603
+  r = wait_prompt(md->s, md->passwd, 0, NULL);
601 604
   if(r == 0){
602 605
     fprintf(stderr, "remote socket close\n");
603
-    close(s);
604 606
     return(1);
605 607
   }
606
-
607 608
   if(r == -1){
608 609
     fprintf(stderr, "socket read error\n");
609
-    close(s);
610 610
     return(1);
611 611
   }
612
+  return(0);
613
+}
612 614
 
613
-  if(makuo_log(s, loglevel)){
614
-    close(s);
615
-    return(1);
615
+void connect_target(msyncdata *md)
616
+{
617
+  struct timeval tv;
618
+  md->s = connect_socket(md->target);
619
+  if(md->s == -1){
620
+    fprintf(stderr, "can't connect %s\n", md->target);
621
+    exit(1);
616 622
   }
623
+  tv.tv_sec  = 5;
624
+  tv.tv_usec = 0;
625
+  setsockopt(md->s, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
626
+  if(connect_wait(md)){
627
+    close(md->s);
628
+    exit(1);
629
+  } 
630
+}
617 631
 
618
-  if(makuo_exclude(s)){
619
-    close(s);
620
-    return(1);
621
-  }
632
+void msync_init(msyncdata *md)
633
+{
634
+  memset(md, 0, sizeof(msyncdata));
635
+  md->loopflag = 1;
636
+  md->sendflag = 1;
637
+  strcpy(md->mcmd, "send");
638
+  defaulttarget(md);
639
+}
622 640
 
623
-  if(scfile[0]){
624
-    return(fromfile(s, scfile));
641
+int main(int argc, char *argv[])
642
+{
643
+  int i;
644
+  msyncdata md;
645
+
646
+  if(argc == 1){
647
+    usage();
648
+    exit(1);
625 649
   }
626 650
 
627
-  if(loopflag && (optind < argc)){
628
-    for(i=optind;i<argc;i++){
629
-      if(delflag){
630
-        sprintf(cmd, "dsync%s %s", mopt, argv[i]);
631
-        if(makuo_exec(s, cmd)){
632
-          close(s);
633
-          return(1);
634
-        }
635
-      }
636
-      sprintf(cmd, "%s%s%s %s", mcmd, mopt, sopt, argv[i]);
637
-      if(makuo_exec(s, cmd)){
638
-        close(s);
639
-        return(1);
640
-      }
641
-    }
642
-  }else{
643
-    if(delflag){
644
-      sprintf(cmd, "dsync%s", mopt);
651
+  msync_init(&md);
652
+  parse_opt(argc, argv, optinit(), &md);
653
+  connect_target(&md);
654
+
655
+  makuo_aliveon(&md);
656
+  makuo_log(&md);
657
+  makuo_exclude(&md);
658
+
659
+  if(!makuo_file(&md)){
660
+    if(!md.loopflag){
661
+      md.path[0] = 0;
662
+      makuo_send(&md);
663
+    }else{
645 664
       for(i=optind;i<argc;i++){
646
-        strcat(cmd, " ");
647
-        strcat(cmd, argv[i]);
648
-      }
649
-      if(makuo_exec(s, cmd)){
650
-        close(s);
651
-        return(1);
665
+        strcpy(md.path, argv[i]);
666
+        makuo_send(&md);
652 667
       }
653 668
     }
654
-    sprintf(cmd, "%s%s%s", mcmd, mopt, sopt);
655
-    for(i=optind;i<argc;i++){
656
-      strcat(cmd, " ");
657
-      strcat(cmd, argv[i]);
658
-    }
659
-    if(makuo_exec(s, cmd)){
660
-      close(s);
661
-      return(1);
662
-    }
663 669
   }
664
-  return(makuo_quit(s));
670
+  return(makuo_quit(&md));
665 671
 }
666 672