~james-page/ubuntu/raring/dovecot/autopkgtest

« back to all changes in this revision

Viewing changes to src/stats/mail-domain.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 11:11:54 UTC
  • mfrom: (1.15.2) (4.1.27 sid)
  • Revision ID: package-import@ubuntu.com-20120611111154-678cwbdj6ktgsv1h
Tags: 1:2.1.7-1ubuntu1
* Merge from Debian unstable, remaining changes:
  + Add mail-stack-delivery package:
    - Update d/rules
    - d/control: convert existing dovecot-postfix package to a dummy
      package and add new mail-stack-delivery package.
    - Update maintainer scripts.
    - Rename d/dovecot-postfix.* to debian/mail-stack-delivery.*
    - d/mail-stack-delivery.preinst: Move previously installed backups and
      config files to a new package namespace.
    - d/mail-stack-delivery.prerm: Added to handle downgrades.
  + Use Snakeoil SSL certificates by default:
    - d/control: Depend on ssl-cert.
    - d/dovecot-core.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - d/dovecot-core.ufw.profile: new ufw profile.
    - d/rules: install profile in dovecot-core.
    - d/control: dovecot-core - suggest ufw.
  + d/{control,rules}: enable PIE hardening.
  + d/dovecot-core.dirs: Added usr/share/doc/dovecot-core
  + Add apport hook:
    - d/rules, d/source_dovecot.py
  + Add upstart job:
    - d/rules, d/dovecot-core.dovecot.upstart, d/control,
      d/dovecot-core.dirs, dovecot-imapd.{postrm, postinst, prerm},
      d/dovecot-pop3d.{postinst, postrm, prerm}.
      d/mail-stack-deliver.postinst: Convert init script to upstart.
  + d/control: Added Pre-Depends: dpkg (>= 1.15.6) to dovecot-dbg to support
    xz compression in Ubuntu.
  + d/control: Demote dovecot-common Recommends: to Suggests: to prevent
    install of extra packages on upgrade.
  + d/patches/dovecot-drac.patch: Updated with version for dovecot >= 2.0.0.
  + d/control: Drop B-D on systemd.
* Dropped changes:
  + d/patches/fix-racey-restart.patch: part of 2.1.x, no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2011-2012 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "ioloop.h"
 
5
#include "hash.h"
 
6
#include "llist.h"
 
7
#include "global-memory.h"
 
8
#include "stats-settings.h"
 
9
#include "mail-stats.h"
 
10
#include "mail-domain.h"
 
11
 
 
12
static struct hash_table *mail_domains_hash;
 
13
/* domains are sorted by their last_update timestamp, oldest first */
 
14
static struct mail_domain *mail_domains_head, *mail_domains_tail;
 
15
struct mail_domain *stable_mail_domains;
 
16
 
 
17
static size_t mail_domain_memsize(const struct mail_domain *domain)
 
18
{
 
19
        return sizeof(*domain) + strlen(domain->name) + 1;
 
20
}
 
21
 
 
22
struct mail_domain *mail_domain_login(const char *name)
 
23
{
 
24
        struct mail_domain *domain;
 
25
 
 
26
        domain = hash_table_lookup(mail_domains_hash, name);
 
27
        if (domain != NULL) {
 
28
                domain->num_logins++;
 
29
                mail_domain_refresh(domain, NULL);
 
30
                return domain;
 
31
        }
 
32
 
 
33
        domain = i_new(struct mail_domain, 1);
 
34
        domain->name = i_strdup(name);
 
35
        domain->reset_timestamp = ioloop_time;
 
36
 
 
37
        hash_table_insert(mail_domains_hash, domain->name, domain);
 
38
        DLLIST_PREPEND_FULL(&stable_mail_domains, domain,
 
39
                            stable_prev, stable_next);
 
40
        DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain,
 
41
                            sorted_prev, sorted_next);
 
42
        domain->num_logins++;
 
43
        domain->last_update = ioloop_timeval;
 
44
        global_memory_alloc(mail_domain_memsize(domain));
 
45
        return domain;
 
46
}
 
47
 
 
48
struct mail_domain *mail_domain_lookup(const char *name)
 
49
{
 
50
        return hash_table_lookup(mail_domains_hash, name);
 
51
}
 
52
 
 
53
void mail_domain_ref(struct mail_domain *domain)
 
54
{
 
55
        domain->refcount++;
 
56
}
 
57
 
 
58
void mail_domain_unref(struct mail_domain **_domain)
 
59
{
 
60
        struct mail_domain *domain = *_domain;
 
61
 
 
62
        i_assert(domain->refcount > 0);
 
63
        domain->refcount--;
 
64
 
 
65
        *_domain = NULL;
 
66
}
 
67
 
 
68
static void mail_domain_free(struct mail_domain *domain)
 
69
{
 
70
        i_assert(domain->refcount == 0);
 
71
        i_assert(domain->users == NULL);
 
72
 
 
73
        global_memory_free(mail_domain_memsize(domain));
 
74
        hash_table_remove(mail_domains_hash, domain->name);
 
75
        DLLIST_REMOVE_FULL(&stable_mail_domains, domain,
 
76
                           stable_prev, stable_next);
 
77
        DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain,
 
78
                            sorted_prev, sorted_next);
 
79
 
 
80
        i_free(domain->name);
 
81
        i_free(domain);
 
82
}
 
83
 
 
84
void mail_domain_refresh(struct mail_domain *domain,
 
85
                         const struct mail_stats *diff_stats)
 
86
{
 
87
        if (diff_stats != NULL)
 
88
                mail_stats_add(&domain->stats, diff_stats);
 
89
        domain->last_update = ioloop_timeval;
 
90
        DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain,
 
91
                            sorted_prev, sorted_next);
 
92
        DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain,
 
93
                            sorted_prev, sorted_next);
 
94
}
 
95
 
 
96
void mail_domains_free_memory(void)
 
97
{
 
98
        unsigned int diff;
 
99
 
 
100
        while (mail_domains_head != NULL && mail_domains_head->refcount == 0) {
 
101
                mail_domain_free(mail_domains_head);
 
102
 
 
103
                if (global_used_memory < stats_settings->memory_limit ||
 
104
                    mail_domains_head == NULL)
 
105
                        break;
 
106
 
 
107
                diff = ioloop_time - mail_domains_head->last_update.tv_sec;
 
108
                if (diff < stats_settings->domain_min_time)
 
109
                        break;
 
110
        }
 
111
}
 
112
 
 
113
void mail_domains_init(void)
 
114
{
 
115
        mail_domains_hash =
 
116
                hash_table_create(default_pool, default_pool, 0,
 
117
                                  str_hash, (hash_cmp_callback_t *)strcmp);
 
118
}
 
119
 
 
120
void mail_domains_deinit(void)
 
121
{
 
122
        while (mail_domains_head != NULL)
 
123
                mail_domain_free(mail_domains_head);
 
124
        hash_table_destroy(&mail_domains_hash);
 
125
}