1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 #include "common.h"
4 
5 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
6 #include <event2/event.h>
7 #include <event2/event_compat.h>
8 #include <event2/event_struct.h>
9 #else
10 #include <event.h>
11 #endif
12 
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <signal.h>
19 
20 #include <glib.h>
21 #include <glib-object.h>
22 
23 #include <searpc-server.h>
24 #include <searpc-client.h>
25 
26 #include "seafile-session.h"
27 #include "seafile-rpc.h"
28 #include "log.h"
29 #include "utils.h"
30 
31 #include "cdc/cdc.h"
32 
33 SeafileSession *seaf;
34 
35 char *pidfile = NULL;
36 
37 static const char *short_options = "hvc:d:l:fP:D:F:p:t";
38 static struct option long_options[] = {
39     { "help", no_argument, NULL, 'h', },
40     { "version", no_argument, NULL, 'v', },
41     { "config-file", required_argument, NULL, 'c' },
42     { "central-config-dir", required_argument, NULL, 'F' },
43     { "seafdir", required_argument, NULL, 'd' },
44     { "log", required_argument, NULL, 'l' },
45     { "debug", required_argument, NULL, 'D' },
46     { "foreground", no_argument, NULL, 'f' },
47     { "pidfile", required_argument, NULL, 'P' },
48     { "rpc-pipe-path", required_argument, NULL, 'p' },
49     { "test-config", no_argument, NULL, 't' },
50     { NULL, 0, NULL, 0, },
51 };
52 
usage()53 static void usage ()
54 {
55     fprintf (stderr, "usage: seaf-server [-c config_dir] [-d seafile_dir]\n");
56 }
57 
58 #include <searpc.h>
59 #include "searpc-signature.h"
60 #include "searpc-marshal.h"
61 #include <searpc-named-pipe-transport.h>
62 
63 #define SEAFILE_RPC_PIPE_NAME "seafile.sock"
64 
65 #define NAMED_PIPE_SERVER_THREAD_POOL_SIZE 50
66 
start_rpc_service(const char * seafile_dir,const char * rpc_pipe_path)67 static void start_rpc_service (const char *seafile_dir,
68                                const char *rpc_pipe_path)
69 {
70     SearpcNamedPipeServer *rpc_server = NULL;
71     char *pipe_path = NULL;
72 
73     searpc_server_init (register_marshals);
74 
75     searpc_create_service ("seafserv-threaded-rpcserver");
76 
77     /* threaded services */
78 
79     /* repo manipulation */
80     searpc_server_register_function ("seafserv-threaded-rpcserver",
81                                      seafile_get_repo,
82                                      "seafile_get_repo",
83                                      searpc_signature_object__string());
84     searpc_server_register_function ("seafserv-threaded-rpcserver",
85                                      seafile_destroy_repo,
86                                      "seafile_destroy_repo",
87                                      searpc_signature_int__string());
88     searpc_server_register_function ("seafserv-threaded-rpcserver",
89                                      seafile_get_repo_list,
90                                      "seafile_get_repo_list",
91                                      searpc_signature_objlist__int_int_string());
92     searpc_server_register_function ("seafserv-threaded-rpcserver",
93                                      seafile_count_repos,
94                                      "seafile_count_repos",
95                                      searpc_signature_int64__void());
96     searpc_server_register_function ("seafserv-threaded-rpcserver",
97                                      seafile_set_repo_owner,
98                                      "seafile_set_repo_owner",
99                                      searpc_signature_int__string_string());
100     searpc_server_register_function ("seafserv-threaded-rpcserver",
101                                      seafile_get_repo_owner,
102                                      "seafile_get_repo_owner",
103                                      searpc_signature_string__string());
104     searpc_server_register_function ("seafserv-threaded-rpcserver",
105                                      seafile_get_orphan_repo_list,
106                                      "seafile_get_orphan_repo_list",
107                                      searpc_signature_objlist__void());
108 
109     searpc_server_register_function ("seafserv-threaded-rpcserver",
110                                      seafile_edit_repo,
111                                      "seafile_edit_repo",
112                                      searpc_signature_int__string_string_string_string());
113 
114     searpc_server_register_function ("seafserv-threaded-rpcserver",
115                                      seafile_change_repo_passwd,
116                                      "seafile_change_repo_passwd",
117                                      searpc_signature_int__string_string_string_string());
118 
119     searpc_server_register_function ("seafserv-threaded-rpcserver",
120                                      seafile_is_repo_owner,
121                                      "seafile_is_repo_owner",
122                                      searpc_signature_int__string_string());
123     searpc_server_register_function ("seafserv-threaded-rpcserver",
124                                      seafile_list_owned_repos,
125                                      "seafile_list_owned_repos",
126                                      searpc_signature_objlist__string_int_int_int());
127     searpc_server_register_function ("seafserv-threaded-rpcserver",
128                                      seafile_search_repos_by_name,
129                                      "seafile_search_repos_by_name",
130                                      searpc_signature_objlist__string());
131 
132     searpc_server_register_function ("seafserv-threaded-rpcserver",
133                                      seafile_server_repo_size,
134                                      "seafile_server_repo_size",
135                                      searpc_signature_int64__string());
136 
137     searpc_server_register_function ("seafserv-threaded-rpcserver",
138                                      seafile_repo_set_access_property,
139                                      "seafile_repo_set_access_property",
140                                      searpc_signature_int__string_string());
141     searpc_server_register_function ("seafserv-threaded-rpcserver",
142                                      seafile_repo_query_access_property,
143                                      "seafile_repo_query_access_property",
144                                      searpc_signature_string__string());
145 
146     searpc_server_register_function ("seafserv-threaded-rpcserver",
147                                      seafile_revert_on_server,
148                                      "seafile_revert_on_server",
149                                      searpc_signature_int__string_string_string());
150 
151     searpc_server_register_function ("seafserv-threaded-rpcserver",
152                                      seafile_diff,
153                                      "seafile_diff",
154                                      searpc_signature_objlist__string_string_string_int());
155 
156     searpc_server_register_function ("seafserv-threaded-rpcserver",
157                                      seafile_post_file,
158                                      "seafile_post_file",
159                     searpc_signature_int__string_string_string_string_string());
160 
161     /* searpc_server_register_function ("seafserv-threaded-rpcserver", */
162     /*                                  seafile_post_file_blocks, */
163     /*                                  "seafile_post_file_blocks", */
164     /*                 searpc_signature_string__string_string_string_string_string_string_int64_int()); */
165     searpc_server_register_function ("seafserv-threaded-rpcserver",
166                                      seafile_post_multi_files,
167                                      "seafile_post_multi_files",
168                     searpc_signature_string__string_string_string_string_string_int());
169 
170     searpc_server_register_function ("seafserv-threaded-rpcserver",
171                                      seafile_put_file,
172                                      "seafile_put_file",
173                     searpc_signature_string__string_string_string_string_string_string());
174     /* searpc_server_register_function ("seafserv-threaded-rpcserver", */
175     /*                                  seafile_put_file_blocks, */
176     /*                                  "seafile_put_file_blocks", */
177     /*                 searpc_signature_string__string_string_string_string_string_string_string_int64()); */
178 
179     searpc_server_register_function ("seafserv-threaded-rpcserver",
180                                      seafile_post_empty_file,
181                                      "seafile_post_empty_file",
182                         searpc_signature_int__string_string_string_string());
183 
184     searpc_server_register_function ("seafserv-threaded-rpcserver",
185                                      seafile_post_dir,
186                                      "seafile_post_dir",
187                         searpc_signature_int__string_string_string_string());
188 
189     searpc_server_register_function ("seafserv-threaded-rpcserver",
190                                      seafile_mkdir_with_parents,
191                                      "seafile_mkdir_with_parents",
192                         searpc_signature_int__string_string_string_string());
193 
194     searpc_server_register_function ("seafserv-threaded-rpcserver",
195                                      seafile_del_file,
196                                      "seafile_del_file",
197                         searpc_signature_int__string_string_string_string());
198 
199     searpc_server_register_function ("seafserv-threaded-rpcserver",
200                                      seafile_copy_file,
201                                      "seafile_copy_file",
202        searpc_signature_object__string_string_string_string_string_string_string_int_int());
203 
204     searpc_server_register_function ("seafserv-threaded-rpcserver",
205                                      seafile_move_file,
206                                      "seafile_move_file",
207        searpc_signature_object__string_string_string_string_string_string_int_string_int_int());
208 
209     searpc_server_register_function ("seafserv-threaded-rpcserver",
210                                      seafile_rename_file,
211                                      "seafile_rename_file",
212                     searpc_signature_int__string_string_string_string_string());
213 
214     searpc_server_register_function ("seafserv-threaded-rpcserver",
215                                      seafile_is_valid_filename,
216                                      "seafile_is_valid_filename",
217                                      searpc_signature_int__string_string());
218 
219     searpc_server_register_function ("seafserv-threaded-rpcserver",
220                                      seafile_create_repo,
221                                      "seafile_create_repo",
222                                      searpc_signature_string__string_string_string_string_int());
223 
224     searpc_server_register_function ("seafserv-threaded-rpcserver",
225                                      seafile_create_enc_repo,
226                                      "seafile_create_enc_repo",
227                                      searpc_signature_string__string_string_string_string_string_string_string_int());
228 
229     searpc_server_register_function ("seafserv-threaded-rpcserver",
230                                      seafile_get_commit,
231                                      "seafile_get_commit",
232                                      searpc_signature_object__string_int_string());
233 
234     searpc_server_register_function ("seafserv-threaded-rpcserver",
235                                      seafile_list_dir,
236                                      "seafile_list_dir",
237                                      searpc_signature_objlist__string_string_int_int());
238 
239     searpc_server_register_function ("seafserv-threaded-rpcserver",
240                                      seafile_list_dir_with_perm,
241                                      "list_dir_with_perm",
242                                      searpc_signature_objlist__string_string_string_string_int_int());
243 
244     searpc_server_register_function ("seafserv-threaded-rpcserver",
245                                      seafile_list_file_blocks,
246                                      "seafile_list_file_blocks",
247                                      searpc_signature_string__string_string_int_int());
248 
249     searpc_server_register_function ("seafserv-threaded-rpcserver",
250                                      seafile_get_file_size,
251                                      "seafile_get_file_size",
252                                      searpc_signature_int64__string_int_string());
253 
254     searpc_server_register_function ("seafserv-threaded-rpcserver",
255                                      seafile_get_dir_size,
256                                      "seafile_get_dir_size",
257                                      searpc_signature_int64__string_int_string());
258 
259     searpc_server_register_function ("seafserv-threaded-rpcserver",
260                                      seafile_list_dir_by_path,
261                                      "seafile_list_dir_by_path",
262                                      searpc_signature_objlist__string_string_string());
263 
264     searpc_server_register_function ("seafserv-threaded-rpcserver",
265                                      seafile_get_dir_id_by_commit_and_path,
266                                      "seafile_get_dir_id_by_commit_and_path",
267                                      searpc_signature_string__string_string_string());
268 
269     searpc_server_register_function ("seafserv-threaded-rpcserver",
270                                      seafile_get_file_id_by_path,
271                                      "seafile_get_file_id_by_path",
272                                      searpc_signature_string__string_string());
273 
274     searpc_server_register_function ("seafserv-threaded-rpcserver",
275                                      seafile_get_dir_id_by_path,
276                                      "seafile_get_dir_id_by_path",
277                                      searpc_signature_string__string_string());
278 
279     searpc_server_register_function ("seafserv-threaded-rpcserver",
280                                      seafile_get_dirent_by_path,
281                                      "seafile_get_dirent_by_path",
282                                      searpc_signature_object__string_string());
283 
284     searpc_server_register_function ("seafserv-threaded-rpcserver",
285                                      seafile_list_file_revisions,
286                                      "seafile_list_file_revisions",
287                                      searpc_signature_objlist__string_string_string_int());
288 
289     searpc_server_register_function ("seafserv-threaded-rpcserver",
290                                      seafile_calc_files_last_modified,
291                                      "seafile_calc_files_last_modified",
292                                      searpc_signature_objlist__string_string_int());
293 
294     searpc_server_register_function ("seafserv-threaded-rpcserver",
295                                      seafile_revert_file,
296                                      "seafile_revert_file",
297                                      searpc_signature_int__string_string_string_string());
298 
299     searpc_server_register_function ("seafserv-threaded-rpcserver",
300                                      seafile_revert_dir,
301                                      "seafile_revert_dir",
302                                      searpc_signature_int__string_string_string_string());
303 
304     searpc_server_register_function ("seafserv-threaded-rpcserver",
305                                      seafile_check_repo_blocks_missing,
306                                      "seafile_check_repo_blocks_missing",
307                                      searpc_signature_string__string_string());
308 
309     searpc_server_register_function ("seafserv-threaded-rpcserver",
310                                      seafile_get_deleted,
311                                      "get_deleted",
312                                      searpc_signature_objlist__string_int_string_string_int());
313 
314     searpc_server_register_function ("seafserv-threaded-rpcserver",
315                                      seafile_get_total_file_number,
316                                      "get_total_file_number",
317                                      searpc_signature_int64__void());
318 
319     searpc_server_register_function ("seafserv-threaded-rpcserver",
320                                      seafile_get_total_storage,
321                                      "get_total_storage",
322                                      searpc_signature_int64__void());
323 
324     searpc_server_register_function ("seafserv-threaded-rpcserver",
325                                      seafile_get_file_count_info_by_path,
326                                      "get_file_count_info_by_path",
327                                      searpc_signature_object__string_string());
328 
329     searpc_server_register_function ("seafserv-threaded-rpcserver",
330                                      seafile_get_trash_repo_owner,
331                                      "get_trash_repo_owner",
332                                      searpc_signature_string__string());
333 
334     searpc_server_register_function ("seafserv-threaded-rpcserver",
335                                      seafile_convert_repo_path,
336                                      "convert_repo_path",
337                                      searpc_signature_string__string_string_string_int());
338 
339     searpc_server_register_function ("seafserv-threaded-rpcserver",
340                                      seafile_search_files,
341                                      "search_files",
342                                      searpc_signature_objlist__string_string());
343 
344     /* share repo to user */
345     searpc_server_register_function ("seafserv-threaded-rpcserver",
346                                      seafile_add_share,
347                                      "seafile_add_share",
348                                      searpc_signature_int__string_string_string_string());
349     searpc_server_register_function ("seafserv-threaded-rpcserver",
350                                      seafile_list_share_repos,
351                                      "seafile_list_share_repos",
352                                      searpc_signature_objlist__string_string_int_int());
353     searpc_server_register_function ("seafserv-threaded-rpcserver",
354                                      seafile_list_repo_shared_to,
355                                      "seafile_list_repo_shared_to",
356                                      searpc_signature_objlist__string_string());
357     searpc_server_register_function ("seafserv-threaded-rpcserver",
358                                      seafile_remove_share,
359                                      "seafile_remove_share",
360                                      searpc_signature_int__string_string_string());
361 
362     searpc_server_register_function ("seafserv-threaded-rpcserver",
363                                      seafile_set_share_permission,
364                                      "set_share_permission",
365                                      searpc_signature_int__string_string_string_string());
366 
367     searpc_server_register_function ("seafserv-threaded-rpcserver",
368                                      seafile_share_subdir_to_user,
369                                      "share_subdir_to_user",
370                                      searpc_signature_string__string_string_string_string_string_string());
371 
372     searpc_server_register_function ("seafserv-threaded-rpcserver",
373                                      seafile_unshare_subdir_for_user,
374                                      "unshare_subdir_for_user",
375                                      searpc_signature_int__string_string_string_string());
376 
377     searpc_server_register_function ("seafserv-threaded-rpcserver",
378                                      seafile_update_share_subdir_perm_for_user,
379                                      "update_share_subdir_perm_for_user",
380                                      searpc_signature_int__string_string_string_string_string());
381 
382     searpc_server_register_function ("seafserv-threaded-rpcserver",
383                                      seafile_get_shared_repo_by_path,
384                                      "get_shared_repo_by_path",
385                                      searpc_signature_object__string_string_string_int());
386     searpc_server_register_function ("seafserv-threaded-rpcserver",
387                                      seafile_get_shared_users_by_repo,
388                                      "get_shared_users_by_repo",
389                                      searpc_signature_objlist__string());
390     searpc_server_register_function ("seafserv-threaded-rpcserver",
391                                      seafile_org_get_shared_users_by_repo,
392                                      "org_get_shared_users_by_repo",
393                                      searpc_signature_objlist__int_string());
394 
395     /* share repo to group */
396     searpc_server_register_function ("seafserv-threaded-rpcserver",
397                                      seafile_group_share_repo,
398                                      "seafile_group_share_repo",
399                                      searpc_signature_int__string_int_string_string());
400     searpc_server_register_function ("seafserv-threaded-rpcserver",
401                                      seafile_group_unshare_repo,
402                                      "seafile_group_unshare_repo",
403                                      searpc_signature_int__string_int_string());
404     searpc_server_register_function ("seafserv-threaded-rpcserver",
405                                      seafile_get_shared_groups_by_repo,
406                                      "seafile_get_shared_groups_by_repo",
407                                      searpc_signature_string__string());
408 
409     searpc_server_register_function ("seafserv-threaded-rpcserver",
410                                      seafile_share_subdir_to_group,
411                                      "share_subdir_to_group",
412                                      searpc_signature_string__string_string_string_int_string_string());
413 
414     searpc_server_register_function ("seafserv-threaded-rpcserver",
415                                      seafile_unshare_subdir_for_group,
416                                      "unshare_subdir_for_group",
417                                      searpc_signature_int__string_string_string_int());
418 
419     searpc_server_register_function ("seafserv-threaded-rpcserver",
420                                      seafile_update_share_subdir_perm_for_group,
421                                      "update_share_subdir_perm_for_group",
422                                      searpc_signature_int__string_string_string_int_string());
423 
424     searpc_server_register_function ("seafserv-threaded-rpcserver",
425                                      seafile_get_group_repoids,
426                                      "seafile_get_group_repoids",
427                                      searpc_signature_string__int());
428     searpc_server_register_function ("seafserv-threaded-rpcserver",
429                                      seafile_list_repo_shared_group,
430                                      "seafile_list_repo_shared_group",
431                                      searpc_signature_objlist__string_string());
432     searpc_server_register_function ("seafserv-threaded-rpcserver",
433                                      seafile_get_group_shared_repo_by_path,
434                                      "get_group_shared_repo_by_path",
435                                      searpc_signature_object__string_string_int_int());
436     searpc_server_register_function ("seafserv-threaded-rpcserver",
437                                      seafile_get_group_repos_by_user,
438                                      "get_group_repos_by_user",
439                                      searpc_signature_objlist__string());
440     searpc_server_register_function ("seafserv-threaded-rpcserver",
441                                      seafile_get_org_group_repos_by_user,
442                                      "get_org_group_repos_by_user",
443                                      searpc_signature_objlist__string_int());
444     searpc_server_register_function ("seafserv-threaded-rpcserver",
445                                      seafile_get_repos_by_group,
446                                      "seafile_get_repos_by_group",
447                                      searpc_signature_objlist__int());
448     searpc_server_register_function ("seafserv-threaded-rpcserver",
449                                      seafile_get_group_repos_by_owner,
450                                      "get_group_repos_by_owner",
451                                      searpc_signature_objlist__string());
452     searpc_server_register_function ("seafserv-threaded-rpcserver",
453                                      seafile_get_group_repo_owner,
454                                      "get_group_repo_owner",
455                                      searpc_signature_string__string());
456     searpc_server_register_function ("seafserv-threaded-rpcserver",
457                                      seafile_remove_repo_group,
458                                      "seafile_remove_repo_group",
459                                      searpc_signature_int__int_string());
460 
461     searpc_server_register_function ("seafserv-threaded-rpcserver",
462                                      seafile_set_group_repo_permission,
463                                      "set_group_repo_permission",
464                                      searpc_signature_int__int_string_string());
465 
466     searpc_server_register_function ("seafserv-threaded-rpcserver",
467                                      seafile_get_shared_users_for_subdir,
468                                      "seafile_get_shared_users_for_subdir",
469                                      searpc_signature_objlist__string_string_string());
470 
471     searpc_server_register_function ("seafserv-threaded-rpcserver",
472                                      seafile_get_shared_groups_for_subdir,
473                                      "seafile_get_shared_groups_for_subdir",
474                                      searpc_signature_objlist__string_string_string());
475 
476     searpc_server_register_function ("seafserv-threaded-rpcserver",
477                                      seafile_repo_has_been_shared,
478                                      "repo_has_been_shared",
479                                      searpc_signature_int__string_int());
480 
481     /* branch and commit */
482     searpc_server_register_function ("seafserv-threaded-rpcserver",
483                                      seafile_branch_gets,
484                                      "seafile_branch_gets",
485                                      searpc_signature_objlist__string());
486     searpc_server_register_function ("seafserv-threaded-rpcserver",
487                                      seafile_get_commit_list,
488                                      "seafile_get_commit_list",
489                                      searpc_signature_objlist__string_int_int());
490 
491     /* token */
492     searpc_server_register_function ("seafserv-threaded-rpcserver",
493                                      seafile_generate_repo_token,
494                                      "seafile_generate_repo_token",
495                                      searpc_signature_string__string_string());
496 
497     searpc_server_register_function ("seafserv-threaded-rpcserver",
498                                      seafile_delete_repo_token,
499                                      "seafile_delete_repo_token",
500                                      searpc_signature_int__string_string_string());
501 
502     searpc_server_register_function ("seafserv-threaded-rpcserver",
503                                      seafile_list_repo_tokens,
504                                      "seafile_list_repo_tokens",
505                                      searpc_signature_objlist__string());
506 
507     searpc_server_register_function ("seafserv-threaded-rpcserver",
508                                      seafile_list_repo_tokens_by_email,
509                                      "seafile_list_repo_tokens_by_email",
510                                      searpc_signature_objlist__string());
511 
512     searpc_server_register_function ("seafserv-threaded-rpcserver",
513                                      seafile_delete_repo_tokens_by_peer_id,
514                                      "seafile_delete_repo_tokens_by_peer_id",
515                                      searpc_signature_int__string_string());
516 
517     searpc_server_register_function ("seafserv-threaded-rpcserver",
518                                      seafile_delete_repo_tokens_by_email,
519                                      "delete_repo_tokens_by_email",
520                                      searpc_signature_int__string());
521 
522     /* quota */
523     searpc_server_register_function ("seafserv-threaded-rpcserver",
524                                      seafile_get_user_quota_usage,
525                                      "seafile_get_user_quota_usage",
526                                      searpc_signature_int64__string());
527     searpc_server_register_function ("seafserv-threaded-rpcserver",
528                                      seafile_get_user_share_usage,
529                                      "seafile_get_user_share_usage",
530                                      searpc_signature_int64__string());
531 
532     /* virtual repo */
533     searpc_server_register_function ("seafserv-threaded-rpcserver",
534                                      seafile_create_virtual_repo,
535                                      "create_virtual_repo",
536                                      searpc_signature_string__string_string_string_string_string_string());
537 
538     searpc_server_register_function ("seafserv-threaded-rpcserver",
539                                      seafile_get_virtual_repos_by_owner,
540                                      "get_virtual_repos_by_owner",
541                                      searpc_signature_objlist__string());
542 
543     searpc_server_register_function ("seafserv-threaded-rpcserver",
544                                      seafile_get_virtual_repo,
545                                      "get_virtual_repo",
546                                      searpc_signature_object__string_string_string());
547 
548     searpc_server_register_function ("seafserv-threaded-rpcserver",
549                                      seafile_get_upload_tmp_file_offset,
550                                      "seafile_get_upload_tmp_file_offset",
551                                      searpc_signature_int64__string_string());
552 
553     /* Clean trash */
554 
555     searpc_server_register_function ("seafserv-threaded-rpcserver",
556                                      seafile_clean_up_repo_history,
557                                      "clean_up_repo_history",
558                                      searpc_signature_int__string_int());
559 
560     /* -------- rpc services -------- */
561     /* token for web access to repo */
562     searpc_server_register_function ("seafserv-threaded-rpcserver",
563                                      seafile_web_get_access_token,
564                                      "seafile_web_get_access_token",
565                                      searpc_signature_string__string_string_string_string_int());
566     searpc_server_register_function ("seafserv-threaded-rpcserver",
567                                      seafile_web_query_access_token,
568                                      "seafile_web_query_access_token",
569                                      searpc_signature_object__string());
570 
571     searpc_server_register_function ("seafserv-threaded-rpcserver",
572                                      seafile_query_zip_progress,
573                                      "seafile_query_zip_progress",
574                                      searpc_signature_string__string());
575     searpc_server_register_function ("seafserv-threaded-rpcserver",
576                                      seafile_cancel_zip_task,
577                                      "cancel_zip_task",
578                                      searpc_signature_int__string());
579 
580     /* Copy task related. */
581 
582     searpc_server_register_function ("seafserv-threaded-rpcserver",
583                                      seafile_get_copy_task,
584                                      "get_copy_task",
585                                      searpc_signature_object__string());
586 
587     searpc_server_register_function ("seafserv-threaded-rpcserver",
588                                      seafile_cancel_copy_task,
589                                      "cancel_copy_task",
590                                      searpc_signature_int__string());
591 
592     /* password management */
593     searpc_server_register_function ("seafserv-threaded-rpcserver",
594                                      seafile_check_passwd,
595                                      "seafile_check_passwd",
596                                      searpc_signature_int__string_string());
597     searpc_server_register_function ("seafserv-threaded-rpcserver",
598                                      seafile_set_passwd,
599                                      "seafile_set_passwd",
600                                      searpc_signature_int__string_string_string());
601     searpc_server_register_function ("seafserv-threaded-rpcserver",
602                                      seafile_unset_passwd,
603                                      "seafile_unset_passwd",
604                                      searpc_signature_int__string_string());
605     searpc_server_register_function ("seafserv-threaded-rpcserver",
606                                      seafile_is_passwd_set,
607                                      "seafile_is_passwd_set",
608                                      searpc_signature_int__string_string());
609     searpc_server_register_function ("seafserv-threaded-rpcserver",
610                                      seafile_get_decrypt_key,
611                                      "seafile_get_decrypt_key",
612                                      searpc_signature_object__string_string());
613 
614     /* quota management */
615     searpc_server_register_function ("seafserv-threaded-rpcserver",
616                                      seafile_set_user_quota,
617                                      "set_user_quota",
618                                      searpc_signature_int__string_int64());
619     searpc_server_register_function ("seafserv-threaded-rpcserver",
620                                      seafile_get_user_quota,
621                                      "get_user_quota",
622                                      searpc_signature_int64__string());
623     searpc_server_register_function ("seafserv-threaded-rpcserver",
624                                      seafile_check_quota,
625                                      "check_quota",
626                                      searpc_signature_int__string_int64());
627     searpc_server_register_function ("seafserv-threaded-rpcserver",
628                                      seafile_list_user_quota_usage,
629                                      "list_user_quota_usage",
630                                      searpc_signature_objlist__void());
631 
632     /* repo permission */
633     searpc_server_register_function ("seafserv-threaded-rpcserver",
634                                      seafile_check_permission,
635                                      "check_permission",
636                                      searpc_signature_string__string_string());
637 
638     searpc_server_register_function ("seafserv-threaded-rpcserver",
639                                      seafile_set_repo_status,
640                                      "set_repo_status",
641                                      searpc_signature_int__string_int());
642 
643     searpc_server_register_function ("seafserv-threaded-rpcserver",
644                                      seafile_get_repo_status,
645                                      "get_repo_status",
646                                      searpc_signature_int__string());
647 
648     /* folder permission */
649     searpc_server_register_function ("seafserv-threaded-rpcserver",
650                                      seafile_check_permission_by_path,
651                                      "check_permission_by_path",
652                                      searpc_signature_string__string_string_string());
653 
654     searpc_server_register_function ("seafserv-threaded-rpcserver",
655                                      seafile_get_file_id_by_commit_and_path,
656                                      "seafile_get_file_id_by_commit_and_path",
657                                      searpc_signature_string__string_string_string());
658 
659     /* event */
660     searpc_server_register_function ("seafserv-threaded-rpcserver",
661                                      seafile_publish_event,
662                                      "publish_event",
663                                      searpc_signature_int__string_string());
664 
665     searpc_server_register_function ("seafserv-threaded-rpcserver",
666                                      seafile_pop_event,
667                                      "pop_event",
668                                      searpc_signature_json__string());
669 
670 
671     searpc_server_register_function ("seafserv-threaded-rpcserver",
672                                      seafile_set_inner_pub_repo,
673                                      "set_inner_pub_repo",
674                                      searpc_signature_int__string_string());
675     searpc_server_register_function ("seafserv-threaded-rpcserver",
676                                      seafile_unset_inner_pub_repo,
677                                      "unset_inner_pub_repo",
678                                      searpc_signature_int__string());
679     searpc_server_register_function ("seafserv-threaded-rpcserver",
680                                      seafile_is_inner_pub_repo,
681                                      "is_inner_pub_repo",
682                                      searpc_signature_int__string());
683     searpc_server_register_function ("seafserv-threaded-rpcserver",
684                                      seafile_list_inner_pub_repos,
685                                      "list_inner_pub_repos",
686                                      searpc_signature_objlist__void());
687     searpc_server_register_function ("seafserv-threaded-rpcserver",
688                                      seafile_count_inner_pub_repos,
689                                      "count_inner_pub_repos",
690                                      searpc_signature_int64__void());
691     searpc_server_register_function ("seafserv-threaded-rpcserver",
692                                      seafile_list_inner_pub_repos_by_owner,
693                                      "list_inner_pub_repos_by_owner",
694                                      searpc_signature_objlist__string());
695 
696     /* History */
697     searpc_server_register_function ("seafserv-threaded-rpcserver",
698                                      seafile_set_repo_history_limit,
699                                      "set_repo_history_limit",
700                                      searpc_signature_int__string_int());
701     searpc_server_register_function ("seafserv-threaded-rpcserver",
702                                      seafile_get_repo_history_limit,
703                                      "get_repo_history_limit",
704                                      searpc_signature_int__string());
705 
706     /* System default library */
707     searpc_server_register_function ("seafserv-threaded-rpcserver",
708                                      seafile_get_system_default_repo_id,
709                                      "get_system_default_repo_id",
710                                      searpc_signature_string__void());
711 
712     /* Trashed repos. */
713     searpc_server_register_function ("seafserv-threaded-rpcserver",
714                                      seafile_get_trash_repo_list,
715                                      "get_trash_repo_list",
716                                      searpc_signature_objlist__int_int());
717     searpc_server_register_function ("seafserv-threaded-rpcserver",
718                                      seafile_del_repo_from_trash,
719                                      "del_repo_from_trash",
720                                      searpc_signature_int__string());
721     searpc_server_register_function ("seafserv-threaded-rpcserver",
722                                      seafile_restore_repo_from_trash,
723                                      "restore_repo_from_trash",
724                                      searpc_signature_int__string());
725     searpc_server_register_function ("seafserv-threaded-rpcserver",
726                                      seafile_get_trash_repos_by_owner,
727                                      "get_trash_repos_by_owner",
728                                      searpc_signature_objlist__string());
729     searpc_server_register_function ("seafserv-threaded-rpcserver",
730                                      seafile_empty_repo_trash,
731                                      "empty_repo_trash",
732                                      searpc_signature_int__void());
733     searpc_server_register_function ("seafserv-threaded-rpcserver",
734                                      seafile_empty_repo_trash_by_owner,
735                                      "empty_repo_trash_by_owner",
736                                      searpc_signature_int__string());
737     searpc_server_register_function ("seafserv-threaded-rpcserver",
738                                      seafile_generate_magic_and_random_key,
739                                      "generate_magic_and_random_key",
740                                      searpc_signature_object__int_string_string());
741 
742     /* Config */
743     searpc_server_register_function ("seafserv-threaded-rpcserver",
744                                      seafile_get_server_config_int,
745                                      "get_server_config_int",
746                                      searpc_signature_int__string_string());
747 
748     searpc_server_register_function ("seafserv-threaded-rpcserver",
749                                      seafile_set_server_config_int,
750                                      "set_server_config_int",
751                                      searpc_signature_int__string_string_int());
752 
753     searpc_server_register_function ("seafserv-threaded-rpcserver",
754                                      seafile_get_server_config_int64,
755                                      "get_server_config_int64",
756                                      searpc_signature_int64__string_string());
757 
758     searpc_server_register_function ("seafserv-threaded-rpcserver",
759                                      seafile_set_server_config_int64,
760                                      "set_server_config_int64",
761                                      searpc_signature_int__string_string_int64());
762 
763     searpc_server_register_function ("seafserv-threaded-rpcserver",
764                                      seafile_get_server_config_string,
765                                      "get_server_config_string",
766                                      searpc_signature_string__string_string());
767 
768     searpc_server_register_function ("seafserv-threaded-rpcserver",
769                                      seafile_set_server_config_string,
770                                      "set_server_config_string",
771                                      searpc_signature_int__string_string_string());
772 
773     searpc_server_register_function ("seafserv-threaded-rpcserver",
774                                      seafile_get_server_config_boolean,
775                                      "get_server_config_boolean",
776                                      searpc_signature_int__string_string());
777 
778     searpc_server_register_function ("seafserv-threaded-rpcserver",
779                                      seafile_set_server_config_boolean,
780                                      "set_server_config_boolean",
781                                      searpc_signature_int__string_string_int());
782 
783     /*user management*/
784    searpc_server_register_function ("seafserv-threaded-rpcserver",
785                                      ccnet_rpc_add_emailuser,
786                                      "add_emailuser",
787                                      searpc_signature_int__string_string_int_int());
788     searpc_server_register_function ("seafserv-threaded-rpcserver",
789                                      ccnet_rpc_remove_emailuser,
790                                      "remove_emailuser",
791                                      searpc_signature_int__string_string());
792     searpc_server_register_function ("seafserv-threaded-rpcserver",
793                                      ccnet_rpc_validate_emailuser,
794                                      "validate_emailuser",
795                                      searpc_signature_int__string_string());
796     searpc_server_register_function ("seafserv-threaded-rpcserver",
797                                      ccnet_rpc_get_emailuser,
798                                      "get_emailuser",
799                                      searpc_signature_object__string());
800     searpc_server_register_function ("seafserv-threaded-rpcserver",
801                                      ccnet_rpc_get_emailuser_with_import,
802                                      "get_emailuser_with_import",
803                                      searpc_signature_object__string());
804     searpc_server_register_function ("seafserv-threaded-rpcserver",
805                                      ccnet_rpc_get_emailuser_by_id,
806                                      "get_emailuser_by_id",
807                                      searpc_signature_object__int());
808     searpc_server_register_function ("seafserv-threaded-rpcserver",
809                                      ccnet_rpc_get_emailusers,
810                                      "get_emailusers",
811                                      searpc_signature_objlist__string_int_int_string());
812     searpc_server_register_function ("seafserv-threaded-rpcserver",
813                                      ccnet_rpc_search_emailusers,
814                                      "search_emailusers",
815                                      searpc_signature_objlist__string_string_int_int());
816     searpc_server_register_function ("seafserv-threaded-rpcserver",
817                                      ccnet_rpc_search_ldapusers,
818                                      "search_ldapusers",
819                                      searpc_signature_objlist__string_int_int());
820     searpc_server_register_function ("seafserv-threaded-rpcserver",
821                                      ccnet_rpc_count_emailusers,
822                                      "count_emailusers",
823                                      searpc_signature_int64__string());
824     searpc_server_register_function ("seafserv-threaded-rpcserver",
825                                      ccnet_rpc_count_inactive_emailusers,
826                                      "count_inactive_emailusers",
827                                      searpc_signature_int64__string());
828     searpc_server_register_function ("seafserv-threaded-rpcserver",
829                                      ccnet_rpc_update_emailuser,
830                                      "update_emailuser",
831                                      searpc_signature_int__string_int_string_int_int());
832     searpc_server_register_function ("seafserv-threaded-rpcserver",
833                                      ccnet_rpc_update_role_emailuser,
834                                      "update_role_emailuser",
835                                      searpc_signature_int__string_string());
836     searpc_server_register_function ("seafserv-threaded-rpcserver",
837                                      ccnet_rpc_get_superusers,
838                                      "get_superusers",
839                                      searpc_signature_objlist__void());
840     searpc_server_register_function ("seafserv-threaded-rpcserver",
841                                      ccnet_rpc_get_emailusers_in_list,
842                                      "get_emailusers_in_list",
843                                      searpc_signature_objlist__string_string());
844 
845     searpc_server_register_function ("seafserv-threaded-rpcserver",
846                                      ccnet_rpc_update_emailuser_id,
847                                      "update_emailuser_id",
848                                      searpc_signature_int__string_string());
849 
850     /*group management*/
851     searpc_server_register_function ("seafserv-threaded-rpcserver",
852                                      ccnet_rpc_create_group,
853                                      "create_group",
854                                      searpc_signature_int__string_string_string_int());
855     searpc_server_register_function ("seafserv-threaded-rpcserver",
856                                      ccnet_rpc_create_org_group,
857                                      "create_org_group",
858                                  searpc_signature_int__int_string_string_int());
859     searpc_server_register_function ("seafserv-threaded-rpcserver",
860                                      ccnet_rpc_remove_group,
861                                      "remove_group",
862                                      searpc_signature_int__int());
863     searpc_server_register_function ("seafserv-threaded-rpcserver",
864                                      ccnet_rpc_group_add_member,
865                                      "group_add_member",
866                                      searpc_signature_int__int_string_string());
867     searpc_server_register_function ("seafserv-threaded-rpcserver",
868                                      ccnet_rpc_group_remove_member,
869                                      "group_remove_member",
870                                      searpc_signature_int__int_string_string());
871     searpc_server_register_function ("seafserv-threaded-rpcserver",
872                                      ccnet_rpc_group_set_admin,
873                                      "group_set_admin",
874                                      searpc_signature_int__int_string());
875     searpc_server_register_function ("seafserv-threaded-rpcserver",
876                                      ccnet_rpc_group_unset_admin,
877                                      "group_unset_admin",
878                                      searpc_signature_int__int_string());
879     searpc_server_register_function ("seafserv-threaded-rpcserver",
880                                      ccnet_rpc_set_group_name,
881                                      "set_group_name",
882                                      searpc_signature_int__int_string());
883     searpc_server_register_function ("seafserv-threaded-rpcserver",
884                                      ccnet_rpc_quit_group,
885                                      "quit_group",
886                                      searpc_signature_int__int_string());
887     searpc_server_register_function ("seafserv-threaded-rpcserver",
888                                      ccnet_rpc_get_groups,
889                                      "get_groups",
890                                      searpc_signature_objlist__string_int());
891     searpc_server_register_function ("seafserv-threaded-rpcserver",
892                                       ccnet_rpc_list_all_departments,
893                                      "list_all_departments",
894                                      searpc_signature_objlist__void());
895     searpc_server_register_function ("seafserv-threaded-rpcserver",
896                                      ccnet_rpc_get_all_groups,
897                                      "get_all_groups",
898                                      searpc_signature_objlist__int_int_string());
899     searpc_server_register_function ("seafserv-threaded-rpcserver",
900                                      ccnet_rpc_get_ancestor_groups,
901                                      "get_ancestor_groups",
902                                      searpc_signature_objlist__int());
903     searpc_server_register_function ("seafserv-threaded-rpcserver",
904                                      ccnet_rpc_get_group,
905                                      "get_group",
906                                      searpc_signature_object__int());
907     searpc_server_register_function ("seafserv-threaded-rpcserver",
908                                      ccnet_rpc_get_group_members,
909                                      "get_group_members",
910                                      searpc_signature_objlist__int_int_int());
911     searpc_server_register_function ("seafserv-threaded-rpcserver",
912                                      ccnet_rpc_get_members_with_prefix,
913                                      "get_members_with_prefix",
914                                      searpc_signature_objlist__int_string());
915     searpc_server_register_function ("seafserv-threaded-rpcserver",
916                                      ccnet_rpc_check_group_staff,
917                                      "check_group_staff",
918                                      searpc_signature_int__int_string_int());
919     searpc_server_register_function ("seafserv-threaded-rpcserver",
920                                      ccnet_rpc_remove_group_user,
921                                      "remove_group_user",
922                                      searpc_signature_int__string());
923     searpc_server_register_function ("seafserv-threaded-rpcserver",
924                                      ccnet_rpc_is_group_user,
925                                      "is_group_user",
926                                      searpc_signature_int__int_string_int());
927     searpc_server_register_function ("seafserv-threaded-rpcserver",
928                                      ccnet_rpc_set_group_creator,
929                                      "set_group_creator",
930                                      searpc_signature_int__int_string());
931     searpc_server_register_function ("seafserv-threaded-rpcserver",
932                                      ccnet_rpc_search_groups,
933                                      "search_groups",
934                                      searpc_signature_objlist__string_int_int());
935     searpc_server_register_function ("seafserv-threaded-rpcserver",
936                                      ccnet_rpc_get_top_groups,
937                                      "get_top_groups",
938                                      searpc_signature_objlist__int());
939     searpc_server_register_function ("seafserv-threaded-rpcserver",
940                                      ccnet_rpc_get_child_groups,
941                                      "get_child_groups",
942                                      searpc_signature_objlist__int());
943     searpc_server_register_function ("seafserv-threaded-rpcserver",
944                                      ccnet_rpc_get_descendants_groups,
945                                      "get_descendants_groups",
946                                      searpc_signature_objlist__int());
947     searpc_server_register_function ("seafserv-threaded-rpcserver",
948                                      ccnet_rpc_get_groups_members,
949                                      "get_groups_members",
950                                      searpc_signature_objlist__string());
951     /*org management*/
952     searpc_server_register_function ("seafserv-threaded-rpcserver",
953                                      ccnet_rpc_create_org,
954                                      "create_org",
955                                      searpc_signature_int__string_string_string());
956     searpc_server_register_function ("seafserv-threaded-rpcserver",
957                                      ccnet_rpc_remove_org,
958                                      "remove_org",
959                                      searpc_signature_int__int());
960     searpc_server_register_function ("seafserv-threaded-rpcserver",
961                                      ccnet_rpc_get_all_orgs,
962                                      "get_all_orgs",
963                                      searpc_signature_objlist__int_int());
964     searpc_server_register_function ("seafserv-threaded-rpcserver",
965                                      ccnet_rpc_count_orgs,
966                                      "count_orgs",
967                                      searpc_signature_int64__void());
968     searpc_server_register_function ("seafserv-threaded-rpcserver",
969                                      ccnet_rpc_get_org_by_url_prefix,
970                                      "get_org_by_url_prefix",
971                                      searpc_signature_object__string());
972     searpc_server_register_function ("seafserv-threaded-rpcserver",
973                                      ccnet_rpc_get_org_by_id,
974                                      "get_org_by_id",
975                                      searpc_signature_object__int());
976     searpc_server_register_function ("seafserv-threaded-rpcserver",
977                                      ccnet_rpc_add_org_user,
978                                      "add_org_user",
979                                      searpc_signature_int__int_string_int());
980     searpc_server_register_function ("seafserv-threaded-rpcserver",
981                                      ccnet_rpc_remove_org_user,
982                                      "remove_org_user",
983                                      searpc_signature_int__int_string());
984     searpc_server_register_function ("seafserv-threaded-rpcserver",
985                                      ccnet_rpc_get_orgs_by_user,
986                                      "get_orgs_by_user",
987                                      searpc_signature_objlist__string());
988     searpc_server_register_function ("seafserv-threaded-rpcserver",
989                                      ccnet_rpc_get_org_emailusers,
990                                      "get_org_emailusers",
991                                      searpc_signature_objlist__string_int_int());
992     searpc_server_register_function ("seafserv-threaded-rpcserver",
993                                      ccnet_rpc_add_org_group,
994                                      "add_org_group",
995                                      searpc_signature_int__int_int());
996     searpc_server_register_function ("seafserv-threaded-rpcserver",
997                                      ccnet_rpc_remove_org_group,
998                                      "remove_org_group",
999                                      searpc_signature_int__int_int());
1000     searpc_server_register_function ("seafserv-threaded-rpcserver",
1001                                      ccnet_rpc_is_org_group,
1002                                      "is_org_group",
1003                                      searpc_signature_int__int());
1004     searpc_server_register_function ("seafserv-threaded-rpcserver",
1005                                      ccnet_rpc_get_org_id_by_group,
1006                                      "get_org_id_by_group",
1007                                      searpc_signature_int__int());
1008     searpc_server_register_function ("seafserv-threaded-rpcserver",
1009                                      ccnet_rpc_get_org_groups,
1010                                      "get_org_groups",
1011                                      searpc_signature_objlist__int_int_int());
1012     searpc_server_register_function ("seafserv-threaded-rpcserver",
1013                                      ccnet_rpc_get_org_groups_by_user,
1014                                      "get_org_groups_by_user",
1015                                      searpc_signature_objlist__string_int());
1016     searpc_server_register_function ("seafserv-threaded-rpcserver",
1017                                      ccnet_rpc_get_org_top_groups,
1018                                      "get_org_top_groups",
1019                                      searpc_signature_objlist__int());
1020     searpc_server_register_function ("seafserv-threaded-rpcserver",
1021                                      ccnet_rpc_org_user_exists,
1022                                      "org_user_exists",
1023                                      searpc_signature_int__int_string());
1024     searpc_server_register_function ("seafserv-threaded-rpcserver",
1025                                      ccnet_rpc_is_org_staff,
1026                                      "is_org_staff",
1027                                      searpc_signature_int__int_string());
1028     searpc_server_register_function ("seafserv-threaded-rpcserver",
1029                                      ccnet_rpc_set_org_staff,
1030                                      "set_org_staff",
1031                                      searpc_signature_int__int_string());
1032     searpc_server_register_function ("seafserv-threaded-rpcserver",
1033                                      ccnet_rpc_unset_org_staff,
1034                                      "unset_org_staff",
1035                                      searpc_signature_int__int_string());
1036     searpc_server_register_function ("seafserv-threaded-rpcserver",
1037                                      ccnet_rpc_set_org_name,
1038                                      "set_org_name",
1039                                      searpc_signature_int__int_string());
1040     searpc_server_register_function ("seafserv-threaded-rpcserver",
1041                                      ccnet_rpc_set_reference_id,
1042                                      "set_reference_id",
1043                                      searpc_signature_int__string_string());
1044     searpc_server_register_function ("seafserv-threaded-rpcserver",
1045                                      ccnet_rpc_get_primary_id,
1046                                      "get_primary_id",
1047                                      searpc_signature_string__string());
1048 
1049     if (rpc_pipe_path) {
1050         pipe_path = g_build_path ("/", rpc_pipe_path, SEAFILE_RPC_PIPE_NAME, NULL);
1051     } else {
1052         pipe_path = g_build_path ("/", seafile_dir, SEAFILE_RPC_PIPE_NAME, NULL);
1053     }
1054     rpc_server = searpc_create_named_pipe_server_with_threadpool (pipe_path, NAMED_PIPE_SERVER_THREAD_POOL_SIZE);
1055 
1056     g_free(pipe_path);
1057     if (!rpc_server) {
1058         seaf_warning ("Failed to create rpc server.\n");
1059         exit (1);
1060     }
1061 
1062     if (searpc_named_pipe_server_start(rpc_server) < 0) {
1063         seaf_warning ("Failed to start rpc server.\n");
1064         exit (1);
1065     }
1066 }
1067 
1068 static struct event sigusr1;
1069 
sigusr1Handler(int fd,short event,void * user_data)1070 static void sigusr1Handler (int fd, short event, void *user_data)
1071 {
1072     seafile_log_reopen ();
1073 }
1074 
1075 static void
set_signal_handlers(SeafileSession * session)1076 set_signal_handlers (SeafileSession *session)
1077 {
1078 #ifndef WIN32
1079     signal (SIGPIPE, SIG_IGN);
1080 
1081     /* design as reopen log */
1082     event_set(&sigusr1, SIGUSR1, EV_SIGNAL | EV_PERSIST, sigusr1Handler, NULL);
1083     event_add(&sigusr1, NULL);
1084 #endif
1085 }
1086 
1087 static void
remove_pidfile(const char * pidfile)1088 remove_pidfile (const char *pidfile)
1089 {
1090     if (pidfile) {
1091         g_unlink (pidfile);
1092     }
1093 }
1094 
1095 static int
write_pidfile(const char * pidfile_path)1096 write_pidfile (const char *pidfile_path)
1097 {
1098     if (!pidfile_path)
1099         return -1;
1100 
1101     pid_t pid = getpid();
1102 
1103     FILE *pidfile = g_fopen(pidfile_path, "w");
1104     if (!pidfile) {
1105         seaf_warning ("Failed to fopen() pidfile %s: %s\n",
1106                       pidfile_path, strerror(errno));
1107         return -1;
1108     }
1109 
1110     char buf[32];
1111     snprintf (buf, sizeof(buf), "%d\n", pid);
1112     if (fputs(buf, pidfile) < 0) {
1113         seaf_warning ("Failed to write pidfile %s: %s\n",
1114                       pidfile_path, strerror(errno));
1115         fclose (pidfile);
1116         return -1;
1117     }
1118 
1119     fflush (pidfile);
1120     fclose (pidfile);
1121     return 0;
1122 }
1123 
1124 static void
on_seaf_server_exit(void)1125 on_seaf_server_exit(void)
1126 {
1127     if (pidfile)
1128         remove_pidfile (pidfile);
1129 }
1130 
1131 #ifdef WIN32
1132 /* Get the commandline arguments in unicode, then convert them to utf8  */
1133 static char **
get_argv_utf8(int * argc)1134 get_argv_utf8 (int *argc)
1135 {
1136     int i = 0;
1137     char **argv = NULL;
1138     const wchar_t *cmdline = NULL;
1139     wchar_t **argv_w = NULL;
1140 
1141     cmdline = GetCommandLineW();
1142     argv_w = CommandLineToArgvW (cmdline, argc);
1143     if (!argv_w) {
1144         printf("failed to CommandLineToArgvW(), GLE=%lu\n", GetLastError());
1145         return NULL;
1146     }
1147 
1148     argv = (char **)malloc (sizeof(char*) * (*argc));
1149     for (i = 0; i < *argc; i++) {
1150         argv[i] = wchar_to_utf8 (argv_w[i]);
1151     }
1152 
1153     return argv;
1154 }
1155 #endif
1156 
1157 int
test_seafile_config(const char * central_config_dir,const char * config_dir,const char * seafile_dir)1158 test_seafile_config(const char *central_config_dir, const char *config_dir, const char *seafile_dir)
1159 {
1160 #if !GLIB_CHECK_VERSION(2, 36, 0)
1161     g_type_init ();
1162 #endif
1163 
1164     config_dir = ccnet_expand_path (config_dir);
1165     if (central_config_dir) {
1166         central_config_dir = ccnet_expand_path (central_config_dir);
1167     }
1168 
1169     if (seafile_log_init ("-", "debug", "debug") < 0) {
1170         fprintf (stderr, "seafile_log_init error: %s\n", strerror(errno));
1171         return -1;
1172     }
1173 
1174     srand (time(NULL));
1175 
1176     event_init ();
1177 
1178     seaf = seafile_session_new (central_config_dir, seafile_dir, config_dir);
1179     if (!seaf) {
1180         fprintf (stderr, "Error: failed to create ccnet session\n");
1181         return -1;
1182     }
1183 
1184     if (seafile_session_init (seaf) < 0)
1185         return -1;
1186 
1187     return 0;
1188 }
1189 
1190 int
main(int argc,char ** argv)1191 main (int argc, char **argv)
1192 {
1193     int c;
1194     char *ccnet_dir = DEFAULT_CONFIG_DIR;
1195     char *seafile_dir = NULL;
1196     char *central_config_dir = NULL;
1197     char *logfile = NULL;
1198     char *rpc_pipe_path = NULL;
1199     const char *debug_str = NULL;
1200     int daemon_mode = 1;
1201     gboolean test_config = FALSE;
1202 
1203 #ifdef WIN32
1204     argv = get_argv_utf8 (&argc);
1205 #endif
1206 
1207     while ((c = getopt_long (argc, argv, short_options,
1208                              long_options, NULL)) != EOF)
1209     {
1210         switch (c) {
1211         case 'h':
1212             exit (1);
1213             break;
1214         case 'v':
1215             exit (1);
1216             break;
1217         case 'c':
1218             ccnet_dir = optarg;
1219             break;
1220         case 'd':
1221             seafile_dir = g_strdup(optarg);
1222             break;
1223         case 'F':
1224             central_config_dir = g_strdup(optarg);
1225             break;
1226         case 'f':
1227             daemon_mode = 0;
1228             break;
1229         case 'l':
1230             logfile = g_strdup(optarg);
1231             break;
1232         case 'D':
1233             debug_str = optarg;
1234             break;
1235         case 'P':
1236             pidfile = optarg;
1237             break;
1238         case 'p':
1239             rpc_pipe_path = g_strdup (optarg);
1240             break;
1241         case 't':
1242             test_config = TRUE;
1243             break;
1244         default:
1245             usage ();
1246             exit (1);
1247         }
1248     }
1249 
1250     argc -= optind;
1251     argv += optind;
1252 
1253     if (test_config) {
1254         return test_seafile_config (central_config_dir, ccnet_dir, seafile_dir);
1255     }
1256 
1257 #ifndef WIN32
1258     if (daemon_mode) {
1259 #ifndef __APPLE__
1260         daemon (1, 0);
1261 #else   /* __APPLE */
1262         /* daemon is deprecated under APPLE
1263          * use fork() instead
1264          * */
1265         switch (fork ()) {
1266           case -1:
1267               seaf_warning ("Failed to daemonize");
1268               exit (-1);
1269               break;
1270           case 0:
1271               /* all good*/
1272               break;
1273           default:
1274               /* kill origin process */
1275               exit (0);
1276         }
1277 #endif  /* __APPLE */
1278     }
1279 #endif /* !WIN32 */
1280 
1281     cdc_init ();
1282 
1283 #if !GLIB_CHECK_VERSION(2, 35, 0)
1284     g_type_init();
1285 #endif
1286 #if !GLIB_CHECK_VERSION(2,32,0)
1287     g_thread_init (NULL);
1288 #endif
1289 
1290     if (!debug_str)
1291         debug_str = g_getenv("SEAFILE_DEBUG");
1292     seafile_debug_set_flags_string (debug_str);
1293 
1294     if (seafile_dir == NULL)
1295         seafile_dir = g_build_filename (ccnet_dir, "seafile", NULL);
1296     if (logfile == NULL)
1297         logfile = g_build_filename (seafile_dir, "seafile.log", NULL);
1298 
1299     if (seafile_log_init (logfile, "info", "debug") < 0) {
1300         seaf_warning ("Failed to init log.\n");
1301         exit (1);
1302     }
1303 
1304     event_init ();
1305 
1306     start_rpc_service (seafile_dir, rpc_pipe_path);
1307 
1308     seaf = seafile_session_new (central_config_dir, seafile_dir, ccnet_dir);
1309     if (!seaf) {
1310         seaf_warning ("Failed to create seafile session.\n");
1311         exit (1);
1312     }
1313 
1314 
1315 #ifndef WIN32
1316     set_syslog_config (seaf->config);
1317 #endif
1318 
1319     g_free (seafile_dir);
1320     g_free (logfile);
1321     g_free (rpc_pipe_path);
1322 
1323     set_signal_handlers (seaf);
1324 
1325     /* Create pid file before connecting to database.
1326      * Connecting to database and creating tables may take long if the db
1327      * is on a remote host. This may make controller think seaf-server fails
1328      * to start and restart it.
1329      */
1330     if (pidfile) {
1331         if (write_pidfile (pidfile) < 0) {
1332             ccnet_message ("Failed to write pidfile\n");
1333             return -1;
1334         }
1335     }
1336 
1337     /* init seaf */
1338     if (seafile_session_init (seaf) < 0)
1339         exit (1);
1340 
1341     if (seafile_session_start (seaf) < 0)
1342         exit (1);
1343 
1344     atexit (on_seaf_server_exit);
1345 
1346     /* Create a system default repo to contain the tutorial file. */
1347     schedule_create_system_default_repo (seaf);
1348 
1349     event_dispatch ();
1350 
1351     return 0;
1352 }
1353