File: idle.c

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (274 lines) | stat: -rw-r--r-- 7,896 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <signal.h>
#include <string.h>
#include <errno.h>

#include "assert.h"
#include "idle.h"
#include "idlemsg.h"
#include "global.h"
#include "util.h"

HIDDEN const char *idle_method_desc = "no";

/* link to idled */
static struct sockaddr_un idle_remote;

/* true if we've successfully told the idled
 * that we want to be notified of changes */
static int idle_started;

/* Send the message 'which' about the mailbox 'mboxname' to the idled.
 * Returns 0 on success or an IMAP error code on failure */
static int idle_send_msg(int which, const char *mboxname)
{
    idle_message_t msg;

    /* maybe the idled came along, so we always send anyway, because
     * polled idle is too awful to contemplate */

    /* fill the structure */
    msg.which = which;
    xstrncpy(msg.mboxname, mboxname ? mboxname : ".", sizeof(msg.mboxname));

    /* send */
    return idle_send(&idle_remote, &msg);
}

/*
 * Notify idled of a mailbox change
 */
static void idle_notify(const char *mboxname)
{
    int r;

    /* We should try to determine if we need to send this
     * (ie, is an imapd is IDLE on 'mailbox'?).
     */
    r = idle_send_msg(IDLE_MSG_NOTIFY, mboxname);
    if (r && (r != ENOENT)) {
        /* ENOENT can happen as result of a race between delivering
         * messages and restarting idled.  It indicates that the
         * idled's socket was unlinked, which means that idled went
         * through it's graceful shutdown path, so don't syslog. */
        syslog(LOG_ERR, "IDLE: error sending message "
                        "NOTIFY to idled for mailbox %s: %s.",
                        mboxname, error_message(r));
    }
    if (errno == ENOENT)
        errno = 0;
}

/*
 * Create connection to idled for sending notifications
 */
EXPORTED void idle_init(void)
{
    struct sockaddr_un local;
    int fdflags;
    int s;
    int r;

    if (!idle_enabled()) return;

    r = idle_make_client_address(&local);
    assert(r);
    r = idle_make_server_address(&idle_remote);
    assert(r);

    idle_method_desc = "poll";

    /* set the mailbox update notifier */
    mailbox_set_updatenotifier(idle_notify);

    if (!idle_init_sock(&local))
        return;

    s = idle_get_sock();

    /* put us in non-blocking mode */
    fdflags = fcntl(s, F_GETFD, 0);
    if (fdflags != -1)
        fdflags = fcntl(s, F_SETFL, O_NONBLOCK | fdflags);
    if (fdflags == -1) {
        idle_done_sock();
        return;
    }

    idle_method_desc = "idled";
}

EXPORTED int idle_enabled(void)
{
    int idle_period = config_getduration(IMAPOPT_IMAPIDLEPOLL, 's');

    /* only enabled if a positive period */
    return (idle_period > 0);
}

EXPORTED void idle_start(const char *mboxname)
{
    int r;

    if (!idle_enabled()) return;

    /* Tell idled that we're idling.  It doesn't
     * matter if it fails, we'll still poll */
    r = idle_send_msg(IDLE_MSG_INIT, mboxname);
    if (r) {
        int idle_timeout = config_getduration(IMAPOPT_IMAPIDLEPOLL, 's');
        syslog(LOG_ERR, "IDLE: error sending message "
                        "INIT to idled for mailbox %s: %s. "
                        "Falling back to polling every %d seconds.",
                        mboxname, error_message(r), idle_timeout);
        return;
    }

    idle_started = 1;
}

EXPORTED int idle_wait(int otherfd)
{
    fd_set rfds;
    int maxfd = -1;
    int s = -1;
    struct timeval timeout;
    int r;
    int flags = 0;
    int idle_timeout = config_getduration(IMAPOPT_IMAPIDLEPOLL, 's');

    if (!idle_enabled()) return 0;

    /* If idled was not contacted, we still listen on the socket,
     * because we might get ALERTs, but we won't get mailbox
     * notifications.  The poll timeout controls how quickly
     * we will notice new mail arriving. */

    FD_ZERO(&rfds);
    s = idle_get_sock();
    if (s >= 0) {
        FD_SET(s, &rfds);
        maxfd = MAX(maxfd, s);
    }
    if (otherfd >= 0) {
        FD_SET(otherfd, &rfds);
        maxfd = MAX(maxfd, otherfd);
    }

    /* Note: it's technically valid for there to be no fds to listen
     * to, in the case where @otherfd is passed as -1 and we failed
     * to talk to idled.  It shouldn't happen though as we're always
     * called with a valid otherfd.  */

    /* maximum possible timeout before we double-check anyway */
    timeout.tv_sec = idle_timeout;
    timeout.tv_usec = 0;

    do {
        r = signals_select(maxfd+1, &rfds, NULL, NULL, &timeout);

        if (r < 0) {
            if (errno == EAGAIN || errno == EINTR) continue;
            syslog(LOG_ERR, "IDLE: select failed: %m");
            return 0;
        }
        if (r == 0) {
            /* timeout */
            flags |= IDLE_MAILBOX|IDLE_ALERT;
        }
        if (r > 0 && s >= 0 && FD_ISSET(s, &rfds)) {
            struct sockaddr_un from;
            idle_message_t msg;

            if (idle_recv(&from, &msg)) {
                switch (msg.which) {
                case IDLE_MSG_NOTIFY:
                    flags |= IDLE_MAILBOX;
                    break;
                case IDLE_MSG_ALERT:
                    flags |= IDLE_ALERT;
                    break;
                }
            }
        }
        if (r > 0 && otherfd >= 0 && FD_ISSET(otherfd, &rfds))
            flags |= IDLE_INPUT;
    } while (!flags);

    return flags;
}

EXPORTED void idle_stop(const char *mboxname)
{
    int r;

    if (!idle_started) return;

    /* Tell idled that we're done idling */
    r = idle_send_msg(IDLE_MSG_DONE, mboxname);
    if (r && (r != ENOENT)) {
        /* See comment in idle_notify() about ENOENT */
        syslog(LOG_ERR, "IDLE: error sending message "
                        "DONE to idled for mailbox %s: %s.",
                        mboxname, error_message(r));
    }

    idle_started = 0;
}

EXPORTED void idle_done(void)
{
    /* close the local socket */
    idle_done_sock();
}