--- imap/sync_client.c.orig 2007-11-26 23:35:59.000000000 +0300 +++ imap/sync_client.c 2008-01-14 22:38:34.000000000 +0300 @@ -2934,6 +2934,11 @@ input = prot_new(fd, 0); + if (verbose_logging) { + syslog(LOG_INFO, "About to start collecting events from %s", + filename); + } + while (1) { if ((c = getword(input, &type)) == EOF) break; @@ -2993,6 +2998,9 @@ else syslog(LOG_ERR, "Unknown action type: %s", type.s); } + if (verbose_logging) { + syslog(LOG_INFO, "Done event collecting. About to start optimizing"); + } /* Optimise out redundant clauses */ @@ -3034,9 +3042,17 @@ remove_folder(action->name, annot_list, 0); } + if (verbose_logging) { + syslog(LOG_INFO, "Done optimizing. Sending lock"); + } + /* Create a lock for our transaction */ if ((r = send_lock())) goto cleanup; + if (verbose_logging) { + syslog(LOG_INFO, "Lock sent. Running..."); + } + /* And then run tasks. */ for (action = append_list->head ; action ; action = action->next) { if (!action->active) @@ -3299,7 +3315,12 @@ /* Check for shutdown file */ if (sync_shutdown_file && !stat(sync_shutdown_file, &sbuf)) { - unlink(sync_shutdown_file); + syslog(LOG_NOTICE, + "%s detected, shutting down.", sync_shutdown_file); + if (unlink(sync_shutdown_file)) { + syslog(LOG_ERR, + "Failed to delete %s: %s", sync_shutdown_file, strerror(errno)); + } break; } @@ -3338,12 +3359,16 @@ } /* Process the work log */ + syslog(LOG_NOTICE, + "About to run sync on log file %s", work_file_name); if ((r=do_sync(work_file_name))) { syslog(LOG_ERR, "Processing sync log file %s failed: %s", work_file_name, error_message(r)); break; } + syslog(LOG_NOTICE, + "Done running sync on log file %s", work_file_name); /* Remove the work log */ if (unlink(work_file_name) < 0) { @@ -3423,7 +3448,7 @@ void do_daemon(const char *sync_log_file, const char *sync_shutdown_file, unsigned long timeout, unsigned long min_delta, - struct backend *be, sasl_callback_t *cb) + struct backend *be, sasl_callback_t *cb, int do_daemon_fork) { int r = 0; pid_t pid; @@ -3431,20 +3456,16 @@ int restart; /* fork a child so we can release from master */ - if ((pid=fork()) < 0) fatal("fork failed", EC_SOFTWARE); + if ( do_daemon_fork ){ + if ((pid=fork()) < 0) fatal("fork failed", EC_SOFTWARE); - if (pid != 0) { /* parent */ - cyrus_done(); - exit(0); + if (pid != 0) { /* parent */ + cyrus_done(); + exit(0); + } } /* child */ - if (timeout == 0) { - do_daemon_work(sync_log_file, sync_shutdown_file, - timeout, min_delta, &restart); - return; - } - signal(SIGPIPE, SIG_IGN); /* don't fail on server disconnects */ do { @@ -3452,8 +3473,7 @@ if ((pid=fork()) < 0) fatal("fork failed", EC_SOFTWARE); if (pid == 0) { /* child */ - - if (be->sock == -1) { + if (!be || be->sock == -1) { /* Reopen up connection to server */ be = replica_connect(be, servername, cb); @@ -3473,6 +3493,8 @@ r = do_daemon_work(sync_log_file, sync_shutdown_file, timeout, min_delta, &restart); + if (timeout == 0) _exit(0); + if (r) { /* See if we're still connected to the server. * If we are, we had some type of error, so we exit. @@ -3540,6 +3562,7 @@ struct backend *be = NULL; sasl_callback_t *cb; int config_virtdomains; + int do_daemon_fork = 1; /* Global list */ msgid_onserver = sync_msgid_list_create(SYNC_MSGID_LIST_HASH_SIZE); @@ -3550,7 +3573,7 @@ setbuf(stdout, NULL); - while ((opt = getopt(argc, argv, "C:vlS:F:f:w:t:d:rumso")) != EOF) { + while ((opt = getopt(argc, argv, "C:vlS:F:f:w:t:d:rumsoD")) != EOF) { switch (opt) { case 'C': /* alt config file */ alt_config = optarg; @@ -3616,6 +3639,10 @@ fatal("Mutually exclusive options defined", EC_USAGE); mode = MODE_SIEVE; break; + + case 'D': + do_daemon_fork = 0; + break; default: usage("sync_client"); @@ -3669,16 +3696,18 @@ config_getstring(IMAPOPT_SYNC_PASSWORD)); /* Open up connection to server */ - be = replica_connect(NULL, servername, cb); + if ( mode != MODE_REPEAT ) { + be = replica_connect(NULL, servername, cb); - if (!be) { - fprintf(stderr, "Can not connect to server '%s'\n", servername); - exit(1); + if (!be) { + fprintf(stderr, "Can not connect to server '%s'\n", servername); + exit(1); + } + /* XXX hack. should just pass 'be' around */ + fromserver = be->in; + toserver = be->out; } - /* XXX hack. should just pass 'be' around */ - fromserver = be->in; - toserver = be->out; switch (mode) { case MODE_USER: @@ -3843,7 +3872,7 @@ min_delta = config_getint(IMAPOPT_SYNC_REPEAT_INTERVAL); do_daemon(sync_log_file, sync_shutdown_file, timeout, min_delta, - be, cb); + be, cb, do_daemon_fork); } break;