... | ... |
@@ -30,10 +30,10 @@ |
30 | 30 |
|
31 | 31 |
|
32 | 32 |
【ファイル構成】 |
33 |
- 以下のファイルがインストールされます。 |
|
33 |
+ make installで以下のファイルがインストールされます。 |
|
34 | 34 |
|
35 |
- /usr/local/sbin/makuosan # 「まくお」本体、全サーバで動かすデーモン |
|
36 |
- /usr/local/bin/msync # ファイルを転送に使うコマンドラインツール |
|
35 |
+ /usr/local/sbin/makuosan # 「まくお」の本体(daemon) |
|
36 |
+ /usr/local/bin/msync # コマンドラインプログラム |
|
37 | 37 |
|
38 | 38 |
|
39 | 39 |
【起動と停止】 |
... | ... |
@@ -48,7 +48,7 @@ |
48 | 48 |
|
49 | 49 |
|
50 | 50 |
【使用方法】 |
51 |
- ファイルを転送するには「makuoコマンド」を使用します。 |
|
51 |
+ ファイルを転送するには「msyncコマンド」を使用します。 |
|
52 | 52 |
|
53 | 53 |
例1)ベースディレクトリ直下のindex.htmlを転送する(/var/www/index.html) |
54 | 54 |
$ msync send index.html |
... | ... |
@@ -60,17 +60,19 @@ |
60 | 60 |
$ msync send -r -n |
61 | 61 |
|
62 | 62 |
|
63 |
-【まくおを利用するメリット】 |
|
64 |
- 「まくお」では、ファイル転送の際に転送先サーバを指定する必要がありません。 |
|
65 |
-rsyncやscpのような、1対1の転送ツール利用する場合は、必ず転送先を指定する必要 |
|
66 |
-があります。そのため、複数のサーバに対してファイルを転送したい場合は、サーバ |
|
67 |
-の数だけrsyncやscpを実行しなければいけません。例えば以下のような感じです。 |
|
63 |
+【利用するメリット】 |
|
64 |
+ 「まくお」は、ファイル転送の際にコマンドラインから転送先サーバを指定する |
|
65 |
+必要がありません。rsyncやscpのような、1対1の転送ツールは、必ず転送先を指定 |
|
66 |
+する必要があります。 |
|
67 |
+ |
|
68 |
+そのため、複数のサーバに対してファイルを転送したい場合、以下のようにサーバ |
|
69 |
+の数だけrsyncやscpを実行しなければいけません。 |
|
68 | 70 |
|
69 | 71 |
$ for h in host2 host3 host4 host5 host6;do rsync -aRv hogehoge $h:/;done |
70 | 72 |
|
71 | 73 |
「まくお」では、makuosanが起動しているサーバ同士でファイルの転送をします。 |
72 |
-したがって、転送したいサーバ全部でmakuosanを起動しておく必要があります。 |
|
73 |
-そのかわり、ファイル転送は、以下の例のように転送したいファイルやディレクトリ |
|
74 |
+したがって、転送したいサーバ全部でmakuosanを起動しておく必要はありますが、 |
|
75 |
+そのかわり、ファイル転送は以下の例のように転送したいファイルやディレクトリ |
|
74 | 76 |
を伝えるだけで済みます。 |
75 | 77 |
|
76 | 78 |
$ msync send -r hogehoge/ |
... | ... |
@@ -79,11 +81,15 @@ rsyncやscpのような、1対1の転送ツール利用する場合は、必ず |
79 | 79 |
ることができます。rsyncやscpを利用する場合と比べて、遥かにシンプルで簡単に転 |
80 | 80 |
送スクリプトを記述できると思います。 |
81 | 81 |
|
82 |
-また「指定したサーバにだけ転送したい」というニーズもあるので、 |
|
82 |
+また「指定したサーバにだけ転送したい」というニーズのために、特定のサーバにだ |
|
83 |
+け転送するオプション(-t)も用意しています。 |
|
83 | 84 |
|
84 | 85 |
$ msync send -t host3 -r hogehoge/ |
85 | 86 |
|
86 |
-というように、特定のサーバにだけ転送するオプションも用意しています。 |
|
87 |
+ただし、この場合でも、host3では makuosanが動いている必要があります。尚、転送 |
|
88 |
+可能なサーバの一覧は、以下のコマンドで確認することができます。 |
|
89 |
+ |
|
90 |
+ $ msync members |
|
87 | 91 |
|
88 | 92 |
|
89 | 93 |
【簡単な使用例】 |
... | ... |
@@ -1,6 +1,6 @@ |
1 |
-/* |
|
2 |
- * [MAKUOSAN] |
|
3 |
- * multicast file synchronization system |
|
1 |
+/* |
|
2 |
+ * makuosan.h |
|
3 |
+ * Copyright (C) 2008 KLab Inc. All rights reserved. |
|
4 | 4 |
*/ |
5 | 5 |
#define MAKUOSAN_VERSION "0.9.1" |
6 | 6 |
#define PROTOCOL_VERSION 3 |
... | ... |
@@ -26,7 +26,6 @@ |
26 | 26 |
#include <stdarg.h> |
27 | 27 |
#include <syslog.h> |
28 | 28 |
#include <pthread.h> |
29 |
-#include <netdb.h> |
|
30 | 29 |
#include <sys/stat.h> |
31 | 30 |
#include <sys/types.h> |
32 | 31 |
#include <sys/socket.h> |
... | ... |
@@ -35,6 +34,7 @@ |
35 | 35 |
#include <sys/timeb.h> |
36 | 36 |
#include <arpa/inet.h> |
37 | 37 |
#include <netinet/in.h> |
38 |
+#include <netdb.h> |
|
38 | 39 |
#include <openssl/md5.h> |
39 | 40 |
#include <openssl/blowfish.h> |
40 | 41 |
|
... | ... |
@@ -1,3 +1,8 @@ |
1 |
+/* |
|
2 |
+ * mexec.c |
|
3 |
+ * Copyright (C) 2008 KLab Inc. All rights reserved. |
|
4 |
+ */ |
|
5 |
+ |
|
1 | 6 |
#include "makuosan.h" |
2 | 7 |
|
3 | 8 |
char *command_list[]={"quit", /* */ |
... | ... |
@@ -357,10 +362,10 @@ int mexec_send(mcomm *c, int n) |
357 | 357 |
|
358 | 358 |
int mexec_members(mcomm *c, int n) |
359 | 359 |
{ |
360 |
- int count = 0; |
|
360 |
+ int count=0; |
|
361 | 361 |
mhost *h; |
362 | 362 |
for(h=members;h;h=h->next){ |
363 |
- cprintf(0, c, "Ver%s: %s (%s)\n", h->version, inet_ntoa(h->ad), h->hostname); |
|
363 |
+ cprintf(0, c, "%s: %s %s\n", h->version, h->hostname, inet_ntoa(h->ad)); |
|
364 | 364 |
count++; |
365 | 365 |
} |
366 | 366 |
cprintf(0, c, "Total: %d members\n", count); |
... | ... |
@@ -482,9 +487,9 @@ int mexec_status(mcomm *c, int n) |
482 | 482 |
|
483 | 483 |
cprintf(0,c,"version : %s\n", MAKUOSAN_VERSION); |
484 | 484 |
if(moption.chroot){ |
485 |
- cprintf(0, c, "chroot : %s\n", moption.real_dir); |
|
485 |
+ cprintf(0, c, "chroot : %s/\n", moption.real_dir); |
|
486 | 486 |
}else{ |
487 |
- cprintf(0, c, "basedir : %s\n", moption.base_dir); |
|
487 |
+ cprintf(0, c, "basedir : %s/\n", moption.base_dir); |
|
488 | 488 |
} |
489 | 489 |
count = 0; |
490 | 490 |
for(m=mftop[0];m;m=m->next) |
... | ... |
@@ -1,104 +1,111 @@ |
1 |
-#include "makuosan.h" |
|
1 |
+/* |
|
2 |
+ * msync.c |
|
3 |
+ * Copyright (C) 2008 KLab Inc. All rights reserved. |
|
4 |
+ */ |
|
5 |
+#define _GNU_SOURCE |
|
6 |
+#define _FILE_OFFSET_BITS 64 |
|
7 |
+#include <stdio.h> |
|
8 |
+#include <unistd.h> |
|
9 |
+#include <stdlib.h> |
|
10 |
+#include <string.h> |
|
11 |
+#include <getopt.h> |
|
12 |
+#include <netdb.h> |
|
13 |
+#include <fcntl.h> |
|
14 |
+#include <sys/stat.h> |
|
15 |
+#include <sys/types.h> |
|
16 |
+#include <sys/socket.h> |
|
17 |
+#include <sys/un.h> |
|
2 | 18 |
|
3 | 19 |
void usage() |
4 | 20 |
{ |
5 |
- printf("usage: makuo [-v] [-c TARGET] [-K PASSWORDFILE] COMMAND [OPT] \n"); |
|
6 |
- printf(" makuo [-v] [-c TARGET] [-K PASSWORDFILE] -f SCRIPT_FILE\n"); |
|
21 |
+ printf("usage: msync [-l loglevel] [-c TARGET] [-K PASSWORDFILE] COMMAND [OPT] \n"); |
|
22 |
+ printf(" msync [-l loglevel] [-c TARGET] [-K PASSWORDFILE] -f SCRIPT_FILE\n"); |
|
7 | 23 |
printf("\n"); |
8 | 24 |
printf(" TARGET\n"); |
9 | 25 |
printf(" tcp:HOST:PORT ex) tcp:127.0.0.1:5000\n"); |
10 |
- printf(" unix:SOCKET ex) tcp:/tmp/makuo.sock\n"); |
|
26 |
+ printf(" unix:SOCKET ex) unix:makuosan.sock\n"); |
|
11 | 27 |
printf("\n"); |
12 | 28 |
printf(" COMMAND\n"); |
13 | 29 |
printf(" send [-n] [-r] [-t HOST] [FILENAME]\n"); |
14 | 30 |
printf(" md5 [-r] [FILENAME]\n"); |
15 | 31 |
printf(" status\n"); |
16 | 32 |
printf(" members\n"); |
17 |
- exit(0); |
|
18 | 33 |
} |
19 | 34 |
|
20 |
-int connect_socket_tcp() |
|
35 |
+int connect_socket_tcp(char *host, char *port) |
|
21 | 36 |
{ |
22 |
- char *p; |
|
23 |
- char host[256]; |
|
24 |
- char port[128]; |
|
25 |
- struct sockaddr_in sa; |
|
26 |
- struct hostent *hn; |
|
27 |
- |
|
28 |
- int s = socket(AF_INET, SOCK_STREAM, 0); |
|
29 |
- if(s == -1){ |
|
37 |
+ int s; |
|
38 |
+ struct addrinfo hint; |
|
39 |
+ struct addrinfo *res; |
|
40 |
+ if(!host || !port){ |
|
30 | 41 |
return(-1); |
31 | 42 |
} |
32 |
- p = strtok(NULL,":"); |
|
33 |
- if(!p){ |
|
34 |
- close(s); |
|
43 |
+ memset(&hint, 0, sizeof(struct addrinfo)); |
|
44 |
+ hint.ai_family = AF_INET; |
|
45 |
+ if(getaddrinfo(host, port, &hint, &res)){ |
|
35 | 46 |
return(-1); |
36 | 47 |
} |
37 |
- strcpy(host, p); |
|
38 |
- p = strtok(NULL,":"); |
|
39 |
- if(!p){ |
|
40 |
- close(s); |
|
48 |
+ if(!res){ |
|
41 | 49 |
return(-1); |
42 | 50 |
} |
43 |
- strcpy(port, p); |
|
44 |
- sa.sin_family = AF_INET; |
|
45 |
- sa.sin_port = htons(atoi(port)); |
|
46 |
- if(!inet_aton(host, &(sa.sin_addr))){ |
|
47 |
- if(hn = gethostbyname(host)){ |
|
48 |
- memcpy(&(sa.sin_addr), hn->h_addr_list[0], hn->h_length); |
|
49 |
- }else{ |
|
50 |
- close(s); |
|
51 |
- fprintf(stderr,"not found %s\n", host); |
|
52 |
- return(-1); |
|
53 |
- } |
|
51 |
+ s = socket(AF_INET, SOCK_STREAM, 0); |
|
52 |
+ if(s == -1){ |
|
53 |
+ freeaddrinfo(res); |
|
54 |
+ return(-1); |
|
54 | 55 |
} |
55 |
- if(connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1){ |
|
56 |
+ if(connect(s, res->ai_addr, res->ai_addrlen) == -1){ |
|
57 |
+ freeaddrinfo(res); |
|
56 | 58 |
close(s); |
57 |
- fprintf(stderr,"connect error tcp:%s:%s\n", host, port); |
|
58 | 59 |
return(-1); |
59 | 60 |
} |
61 |
+ freeaddrinfo(res); |
|
60 | 62 |
return(s); |
61 | 63 |
} |
62 | 64 |
|
63 |
-int connect_socket_unix() |
|
65 |
+int connect_socket_unix(char *path) |
|
64 | 66 |
{ |
65 |
- char *p; |
|
67 |
+ int s; |
|
66 | 68 |
struct sockaddr_un sa; |
67 |
- int s = socket(AF_UNIX, SOCK_STREAM, 0); |
|
68 |
- if(s != -1){ |
|
69 |
- p = strtok(NULL,":"); |
|
70 |
- if(!p){ |
|
71 |
- close(s); |
|
72 |
- return(-1); |
|
73 |
- } |
|
74 |
- sa.sun_family = AF_UNIX; |
|
75 |
- strcpy(sa.sun_path, p); |
|
76 |
- if(connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1){ |
|
77 |
- fprintf(stderr, "can't connect %s\n", sa.sun_path); |
|
78 |
- close(s); |
|
79 |
- return(-1); |
|
80 |
- } |
|
69 |
+ if(!path){ |
|
70 |
+ return(-1); |
|
71 |
+ } |
|
72 |
+ if(strlen(path) >= sizeof(sa.sun_path)){ |
|
73 |
+ return(-1); |
|
74 |
+ } |
|
75 |
+ s = socket(AF_UNIX, SOCK_STREAM, 0); |
|
76 |
+ if(s == -1){ |
|
77 |
+ return(-1); |
|
78 |
+ } |
|
79 |
+ sa.sun_family = AF_UNIX; |
|
80 |
+ strcpy(sa.sun_path,path); |
|
81 |
+ if(connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1){ |
|
82 |
+ close(s); |
|
83 |
+ return(-1); |
|
81 | 84 |
} |
82 | 85 |
return(s); |
83 | 86 |
} |
84 | 87 |
|
85 | 88 |
int connect_socket(char *target) |
86 | 89 |
{ |
90 |
+ char *h; |
|
91 |
+ char *p; |
|
87 | 92 |
char buff[256]; |
88 |
- char *p = buff; |
|
89 | 93 |
|
90 | 94 |
strcpy(buff, target); |
91 | 95 |
p = strtok(buff,":"); |
92 | 96 |
if(!p){ |
93 | 97 |
usage(); |
98 |
+ exit(1); |
|
94 | 99 |
} |
95 | 100 |
if(!strcmp(p, "tcp")){ |
96 |
- return(connect_socket_tcp()); |
|
101 |
+ h = strtok(NULL,":"); |
|
102 |
+ p = strtok(NULL,":"); |
|
103 |
+ return(connect_socket_tcp(h,p)); |
|
97 | 104 |
} |
98 | 105 |
if(!strcmp(p, "unix")){ |
99 |
- return(connect_socket_unix()); |
|
106 |
+ p = strtok(NULL,":"); |
|
107 |
+ return(connect_socket_unix(p)); |
|
100 | 108 |
} |
101 |
- fprintf(stderr,"can't connect %s\n", target); |
|
102 | 109 |
return(-1); |
103 | 110 |
} |
104 | 111 |
|
... | ... |
@@ -120,54 +127,53 @@ int writeline(int s, char *buff) |
120 | 120 |
return(0); |
121 | 121 |
} |
122 | 122 |
|
123 |
+int check_prompt(int s, char *buff, char *passwd) |
|
124 |
+{ |
|
125 |
+ if(!strcmp(buff, "> ")){ |
|
126 |
+ return(1); |
|
127 |
+ } |
|
128 |
+ if(!strcmp(buff, "password: \x1b]E") && passwd){ |
|
129 |
+ writeline(s, passwd); |
|
130 |
+ writeline(s, "\r\n"); |
|
131 |
+ return(2); |
|
132 |
+ } |
|
133 |
+ return(0); |
|
134 |
+} |
|
135 |
+ |
|
123 | 136 |
int readline(int s, char *buff, int size, int prompt, char *passwd) |
124 | 137 |
{ |
125 |
- int c = 0; |
|
126 |
- int r = 0; |
|
127 | 138 |
char d = 0; |
128 | 139 |
char *p = buff; |
129 |
- fd_set fds; |
|
130 |
- struct timeval tv; |
|
131 |
- |
|
132 |
- while(c < size){ |
|
133 |
- FD_ZERO(&fds); |
|
134 |
- FD_SET(s,&fds); |
|
135 |
- tv.tv_sec = 1; |
|
136 |
- tv.tv_usec = 0; |
|
137 |
- if(select(1024, &fds, NULL, NULL, &tv) < 0){ |
|
138 |
- continue; |
|
139 |
- } |
|
140 |
- r = read(s, &d, 1); |
|
141 |
- if(r == -1){ |
|
142 |
- return(-1); |
|
143 |
- } |
|
144 |
- if(r == 0){ |
|
145 |
- *p = 0; |
|
146 |
- return(0); |
|
147 |
- } |
|
148 |
- if(d == '\r'){ |
|
149 |
- continue; |
|
150 |
- } |
|
151 |
- if(d == '\n'){ |
|
152 |
- *p = 0; |
|
153 |
- return(c); |
|
154 |
- } |
|
155 |
- *(p++) = d; |
|
156 |
- c++; |
|
157 |
- if(c < size){ |
|
158 |
- *p = 0; |
|
159 |
- if(prompt && !strcmp(buff, "> ")){ |
|
160 |
- return(-2); |
|
161 |
- } |
|
162 |
- if(prompt && !strcmp(buff, "password: \x1b]E") && passwd){ |
|
163 |
- writeline(s, passwd); |
|
164 |
- writeline(s, "\r\n"); |
|
165 |
- c = 0; |
|
166 |
- p = buff; |
|
140 |
+ |
|
141 |
+ while(p < buff + size){ |
|
142 |
+ *p = 0; |
|
143 |
+ if(prompt){ |
|
144 |
+ switch(check_prompt(s, buff, passwd)){ |
|
145 |
+ case 1: |
|
146 |
+ return(-2); |
|
147 |
+ case 2: |
|
148 |
+ p = buff; |
|
149 |
+ continue; |
|
167 | 150 |
} |
168 | 151 |
} |
152 |
+ switch(read(s, &d, 1)){ |
|
153 |
+ case 0: |
|
154 |
+ return(p - buff); |
|
155 |
+ case -1: |
|
156 |
+ return(-1); |
|
157 |
+ break; |
|
158 |
+ default: |
|
159 |
+ if(d == '\r'){ |
|
160 |
+ break; |
|
161 |
+ } |
|
162 |
+ if((d == '\n') && (p != buff)){ |
|
163 |
+ return(p - buff); |
|
164 |
+ } |
|
165 |
+ *(p++) = d; |
|
166 |
+ break; |
|
167 |
+ } |
|
169 | 168 |
} |
170 |
- return(-1); |
|
169 |
+ return(-1); /* over flow */ |
|
171 | 170 |
} |
172 | 171 |
|
173 | 172 |
int wait_prompt(int s, char *passwd){ |
... | ... |
@@ -190,16 +196,54 @@ int wait_prompt(int s, char *passwd){ |
190 | 190 |
|
191 | 191 |
int makuo(int s, char *c) |
192 | 192 |
{ |
193 |
+ int r; |
|
193 | 194 |
char buff[256]; |
195 |
+ if(sizeof(buff) < strlen(c) + 2){ |
|
196 |
+ fprintf(stderr, "error: command too long\n"); |
|
197 |
+ return(-1); |
|
198 |
+ } |
|
194 | 199 |
sprintf(buff, "%s\r\n", c); |
195 | 200 |
if(writeline(s, buff) == -1){ |
196 |
- fprintf(stderr, "write error\n"); |
|
201 |
+ fprintf(stderr, "error: can't write socket\n"); |
|
202 |
+ return(-1); |
|
203 |
+ } |
|
204 |
+ r = wait_prompt(s, NULL); |
|
205 |
+ if(r == -1){ |
|
206 |
+ fprintf(stderr, "error: can't read socket\n"); |
|
197 | 207 |
return(-1); |
198 | 208 |
} |
199 |
- wait_prompt(s, NULL); |
|
209 |
+ return(r); |
|
210 |
+} |
|
211 |
+ |
|
212 |
+int makuolog(int s, int l) |
|
213 |
+{ |
|
214 |
+ int r; |
|
215 |
+ char cmd[256]; |
|
216 |
+ sprintf(cmd, "loglevel %d", l); |
|
217 |
+ r = makuo(s, cmd); |
|
218 |
+ if(r == 0){ |
|
219 |
+ fprintf(stderr, "error: remote close\n"); |
|
220 |
+ return(1); |
|
221 |
+ } |
|
222 |
+ if(r == -1){ |
|
223 |
+ return(1); |
|
224 |
+ } |
|
200 | 225 |
return(0); |
201 | 226 |
} |
202 | 227 |
|
228 |
+int makuoquit(int s) |
|
229 |
+{ |
|
230 |
+ int r = makuo(s, "quit"); |
|
231 |
+ if(r == 0){ |
|
232 |
+ return(0); /* success */ |
|
233 |
+ } |
|
234 |
+ if(r == -1){ |
|
235 |
+ return(1); |
|
236 |
+ } |
|
237 |
+ fprintf(stderr, "quit error?!\n"); |
|
238 |
+ return(1); |
|
239 |
+} |
|
240 |
+ |
|
203 | 241 |
int fromfile(int s, char *filename) |
204 | 242 |
{ |
205 | 243 |
int f; |
... | ... |
@@ -208,28 +252,68 @@ int fromfile(int s, char *filename) |
208 | 208 |
|
209 | 209 |
if(!strcmp(filename, "-")){ |
210 | 210 |
/* f = stdin */ |
211 |
- f = 0; |
|
211 |
+ f = dup(0); |
|
212 | 212 |
}else{ |
213 | 213 |
f = open(filename, O_RDONLY); |
214 |
- if(f == -1){ |
|
215 |
- fprintf(stderr,"can't open: %s\n", filename); |
|
216 |
- return(-1); |
|
217 |
- } |
|
214 |
+ } |
|
215 |
+ if(f == -1){ |
|
216 |
+ fprintf(stderr,"can't open: %s\n", filename); |
|
217 |
+ return(1); |
|
218 | 218 |
} |
219 | 219 |
|
220 |
+ /* command read loop */ |
|
220 | 221 |
while(r = readline(f, line, sizeof(line), 0, NULL)){ |
221 | 222 |
if(r == -1){ |
222 | 223 |
fprintf(stderr, "file read error: %s\n", filename); |
223 |
- break; |
|
224 |
+ close(f); |
|
225 |
+ return(1); |
|
224 | 226 |
} |
225 | 227 |
r = makuo(s, line); |
228 |
+ if(r == 0){ |
|
229 |
+ fprintf(stderr, "error: makuosan remote close\n"); |
|
230 |
+ close(f); |
|
231 |
+ return(1); |
|
232 |
+ } |
|
226 | 233 |
if(r == -1){ |
227 |
- fprintf(stderr, "makuo error\n"); |
|
228 |
- break; |
|
234 |
+ close(f); |
|
235 |
+ return(1); /* socket error */ |
|
229 | 236 |
} |
230 | 237 |
} |
231 |
- close(f); |
|
232 |
- return(r); |
|
238 |
+ |
|
239 |
+ /* quit */ |
|
240 |
+ return(makuoquit(s)); |
|
241 |
+} |
|
242 |
+ |
|
243 |
+int fromargv(int s, int argc, char *argv[], int start) |
|
244 |
+{ |
|
245 |
+ int i; |
|
246 |
+ int r; |
|
247 |
+ char cmd[256]; |
|
248 |
+ |
|
249 |
+ cmd[0] = 0; |
|
250 |
+ for(i=start;i<argc;i++){ |
|
251 |
+ if(strlen(cmd) + strlen(argv[i]) + 2 > sizeof(cmd)){ |
|
252 |
+ fprintf(stderr, "error: command too long\n"); |
|
253 |
+ return(1); |
|
254 |
+ } |
|
255 |
+ if(cmd[0]){ |
|
256 |
+ strcat(cmd, " "); |
|
257 |
+ } |
|
258 |
+ strcat(cmd, argv[i]); |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ /* command execute */ |
|
262 |
+ r = makuo(s, cmd); |
|
263 |
+ if(r == 0){ |
|
264 |
+ fprintf(stderr, "error: makuosan remote close\n"); |
|
265 |
+ return(1); |
|
266 |
+ } |
|
267 |
+ if(r == -1){ |
|
268 |
+ return(1); /* socket error */ |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ /* quit */ |
|
272 |
+ return(makuoquit(s)); |
|
233 | 273 |
} |
234 | 274 |
|
235 | 275 |
int loadpass(char *filename, char *passwd, int size) |
... | ... |
@@ -250,37 +334,19 @@ int loadpass(char *filename, char *passwd, int size) |
250 | 250 |
return(0); |
251 | 251 |
} |
252 | 252 |
|
253 |
-void defaulttarget(char *target) |
|
253 |
+void defaulttarget(char *target, int size) |
|
254 | 254 |
{ |
255 |
- char *p; |
|
256 |
- if(p = getenv("MAKUO_TARGET")){ |
|
255 |
+ char *p = getenv("MSYNC_TARGET"); |
|
256 |
+ strcpy(target, "tcp:127.0.0.1:5000"); |
|
257 |
+ if(p && (strlen(p) < size)){ |
|
257 | 258 |
strcpy(target, p); |
258 |
- }else{ |
|
259 |
- strcpy(target, "tcp:127.0.0.1:5000"); |
|
260 | 259 |
} |
261 | 260 |
} |
262 | 261 |
|
263 |
-int fromargv(int s, int argc, char *argv[], int start) |
|
264 |
-{ |
|
265 |
- int i; |
|
266 |
- char cmd[256]; |
|
267 |
- |
|
268 |
- cmd[0] = 0; |
|
269 |
- for(i=start;i<argc;i++){ |
|
270 |
- if(cmd[0]){ |
|
271 |
- strcat(cmd, " "); |
|
272 |
- } |
|
273 |
- strcat(cmd, argv[i]); |
|
274 |
- } |
|
275 |
- return(makuo(s, cmd)); |
|
276 |
-} |
|
277 |
- |
|
278 |
- |
|
279 | 262 |
int main(int argc, char *argv[]) |
280 | 263 |
{ |
281 | 264 |
int r; |
282 | 265 |
int s; |
283 |
- char cmd[256]; |
|
284 | 266 |
|
285 | 267 |
/* option */ |
286 | 268 |
int loglevel = 0; |
... | ... |
@@ -291,21 +357,36 @@ int main(int argc, char *argv[]) |
291 | 291 |
/* default */ |
292 | 292 |
scfile[0] = 0; |
293 | 293 |
passwd[0] = 0; |
294 |
- defaulttarget(target); |
|
294 |
+ defaulttarget(target, sizeof(target)); |
|
295 | 295 |
|
296 |
- while((r = getopt(argc, argv, "+c:f:K:hv")) != -1){ |
|
296 |
+ while((r = getopt(argc, argv, "+c:f:K:l:h")) != -1){ |
|
297 | 297 |
switch(r){ |
298 | 298 |
case 'h': |
299 | 299 |
usage(); |
300 |
- case 'v': |
|
301 |
- loglevel++; |
|
300 |
+ return(0); |
|
301 |
+ |
|
302 |
+ case 'l': |
|
303 |
+ loglevel = atoi(optarg); |
|
302 | 304 |
break; |
305 |
+ |
|
303 | 306 |
case 'f': |
304 |
- strcpy(scfile, optarg); |
|
307 |
+ if(strlen(optarg) < sizeof(scfile)){ |
|
308 |
+ strcpy(scfile, optarg); |
|
309 |
+ }else{ |
|
310 |
+ fprintf(stderr, "filename too long\n"); |
|
311 |
+ return(1); |
|
312 |
+ } |
|
305 | 313 |
break; |
314 |
+ |
|
306 | 315 |
case 'c': |
307 |
- strcpy(target, optarg); |
|
316 |
+ if(strlen(optarg) < sizeof(target)){ |
|
317 |
+ strcpy(target, optarg); |
|
318 |
+ }else{ |
|
319 |
+ fprintf(stderr, "target too long\n"); |
|
320 |
+ return(1); |
|
321 |
+ } |
|
308 | 322 |
break; |
323 |
+ |
|
309 | 324 |
case 'K': |
310 | 325 |
if(loadpass(optarg, passwd, sizeof(passwd)) == -1){ |
311 | 326 |
return(1); |
... | ... |
@@ -316,23 +397,36 @@ int main(int argc, char *argv[]) |
316 | 316 |
|
317 | 317 |
if(!scfile[0] && optind == argc){ |
318 | 318 |
usage(); |
319 |
+ return(1); |
|
319 | 320 |
} |
320 | 321 |
|
321 | 322 |
s = connect_socket(target); |
322 | 323 |
if(s == -1){ |
324 |
+ fprintf(stderr, "can't connect %s\n", target); |
|
323 | 325 |
return(1); |
324 | 326 |
} |
327 |
+ |
|
325 | 328 |
r = wait_prompt(s, passwd); |
329 |
+ if(r == 0){ |
|
330 |
+ fprintf(stderr, "remote socket close\n"); |
|
331 |
+ close(s); |
|
332 |
+ return(1); |
|
333 |
+ } |
|
334 |
+ if(r == -1){ |
|
335 |
+ fprintf(stderr, "socket read error\n"); |
|
336 |
+ close(s); |
|
337 |
+ return(1); |
|
338 |
+ } |
|
326 | 339 |
|
327 |
- if(r == 1){ |
|
328 |
- sprintf(cmd, "loglevel %d", loglevel); |
|
329 |
- makuo(s, cmd); |
|
330 |
- if(scfile[0]){ |
|
331 |
- r = fromfile(s, scfile); |
|
332 |
- }else{ |
|
333 |
- r = fromargv(s, argc, argv, optind); |
|
334 |
- } |
|
335 |
- makuo(s, "quit"); |
|
340 |
+ if(makuolog(s, loglevel)){ |
|
341 |
+ close(s); |
|
342 |
+ return(1); |
|
343 |
+ } |
|
344 |
+ |
|
345 |
+ if(scfile[0]){ |
|
346 |
+ r = fromfile(s, scfile); |
|
347 |
+ }else{ |
|
348 |
+ r = fromargv(s, argc, argv, optind); |
|
336 | 349 |
} |
337 | 350 |
close(s); |
338 | 351 |
return(r); |