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

« back to all changes in this revision

Viewing changes to src/doveadm/doveadm-instance.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) 2012 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "master-instance.h"
 
5
#include "doveadm.h"
 
6
#include "doveadm-print.h"
 
7
 
 
8
#include <unistd.h>
 
9
#include <fcntl.h>
 
10
#include <signal.h>
 
11
 
 
12
extern struct doveadm_cmd doveadm_cmd_instance[];
 
13
 
 
14
static void instance_cmd_help(doveadm_command_t *cmd) ATTR_NORETURN;
 
15
 
 
16
static bool pid_file_read(const char *path)
 
17
{
 
18
        char buf[32];
 
19
        int fd;
 
20
        ssize_t ret;
 
21
        pid_t pid;
 
22
        bool found = FALSE;
 
23
 
 
24
        fd = open(path, O_RDONLY);
 
25
        if (fd == -1) {
 
26
                if (errno != ENOENT)
 
27
                        i_error("open(%s) failed: %m", path);
 
28
                return FALSE;
 
29
        }
 
30
 
 
31
        ret = read(fd, buf, sizeof(buf));
 
32
        if (ret < 0)
 
33
                i_error("read(%s) failed: %m", path);
 
34
        else if (ret > 0 && buf[ret-1] == '\n') {
 
35
                buf[ret-1] = '\0';
 
36
                if (str_to_pid(buf, &pid) == 0) {
 
37
                        found = !(pid == getpid() ||
 
38
                                  (kill(pid, 0) < 0 && errno == ESRCH));
 
39
                }
 
40
        }
 
41
        (void)close(fd);
 
42
        return found;
 
43
}
 
44
 
 
45
static void cmd_instance_list(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
 
46
{
 
47
        struct master_instance_list *list;
 
48
        struct master_instance_list_iter *iter;
 
49
        const struct master_instance *inst;
 
50
        const char *pidfile_path;
 
51
 
 
52
        doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
 
53
        doveadm_print_header("path", "path", DOVEADM_PRINT_HEADER_FLAG_EXPAND);
 
54
        doveadm_print_header_simple("name");
 
55
        doveadm_print_header_simple("last used");
 
56
        doveadm_print_header_simple("running");
 
57
 
 
58
        list = master_instance_list_init(MASTER_INSTANCE_PATH);
 
59
        iter = master_instance_list_iterate_init(list);
 
60
        while ((inst = master_instance_iterate_list_next(iter)) != NULL) {
 
61
                doveadm_print(inst->base_dir);
 
62
                doveadm_print(inst->name);
 
63
                doveadm_print(unixdate2str(inst->last_used));
 
64
                pidfile_path = t_strconcat(inst->base_dir, "/master.pid", NULL);
 
65
                if (pid_file_read(pidfile_path))
 
66
                        doveadm_print("yes");
 
67
                else
 
68
                        doveadm_print("no");
 
69
        }
 
70
        master_instance_iterate_list_deinit(&iter);
 
71
        master_instance_list_deinit(&list);
 
72
}
 
73
 
 
74
static void cmd_instance_remove(int argc, char *argv[])
 
75
{
 
76
        struct master_instance_list *list;
 
77
        const struct master_instance *inst;
 
78
        const char *base_dir;
 
79
        int ret;
 
80
 
 
81
        if (argc != 2)
 
82
                instance_cmd_help(cmd_instance_remove);
 
83
 
 
84
        list = master_instance_list_init(MASTER_INSTANCE_PATH);
 
85
        inst = master_instance_list_find_by_name(list, argv[1]);
 
86
        base_dir = inst != NULL ? inst->base_dir : argv[1];
 
87
        if ((ret = master_instance_list_remove(list, base_dir)) < 0) {
 
88
                i_error("Failed to remove instance");
 
89
                doveadm_exit_code = EX_TEMPFAIL;
 
90
        } else if (ret == 0) {
 
91
                i_error("Instance already didn't exist");
 
92
                doveadm_exit_code = DOVEADM_EX_NOTFOUND;
 
93
        }
 
94
        master_instance_list_deinit(&list);
 
95
}
 
96
 
 
97
struct doveadm_cmd doveadm_cmd_instance[] = {
 
98
        { cmd_instance_list, "instance list", "" },
 
99
        { cmd_instance_remove, "instance remove", "<name> | <base dir>" }
 
100
};
 
101
 
 
102
static void instance_cmd_help(doveadm_command_t *cmd)
 
103
{
 
104
        unsigned int i;
 
105
 
 
106
        for (i = 0; i < N_ELEMENTS(doveadm_cmd_instance); i++) {
 
107
                if (doveadm_cmd_instance[i].cmd == cmd)
 
108
                        help(&doveadm_cmd_instance[i]);
 
109
        }
 
110
        i_unreached();
 
111
}
 
112
 
 
113
void doveadm_register_instance_commands(void)
 
114
{
 
115
        unsigned int i;
 
116
 
 
117
        for (i = 0; i < N_ELEMENTS(doveadm_cmd_instance); i++)
 
118
                doveadm_register_cmd(&doveadm_cmd_instance[i]);
 
119
}