summaryrefslogtreecommitdiffstats
path: root/xlators/mgmt/glusterd/src/glusterd-proc-mgmt.c
blob: d96adcae89e720202a4260fbc309e0a3cd19f697 (plain)
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
/*
   Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/

#include <stdio.h>
#include <limits.h>
#include <signal.h>

#include "glusterd.h"
#include "glusterd-utils.h"
#include <glusterfs/common-utils.h>
#include <glusterfs/xlator.h>
#include <glusterfs/logging.h>
#include "glusterd-messages.h"
#include "glusterd-proc-mgmt.h"

int
glusterd_proc_init(glusterd_proc_t *proc, char *name, char *pidfile,
                   char *logdir, char *logfile, char *volfile, char *volfileid,
                   char *volfileserver)
{
    int ret = -1;

    ret = snprintf(proc->name, sizeof(proc->name), "%s", name);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->pidfile, sizeof(proc->pidfile), "%s", pidfile);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->logdir, sizeof(proc->logdir), "%s", logdir);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->logfile, sizeof(proc->logfile), "%s", logfile);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->volfile, sizeof(proc->volfile), "%s", volfile);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->volfileid, sizeof(proc->volfileid), "%s", volfileid);
    if (ret < 0)
        goto out;

    ret = snprintf(proc->volfileserver, sizeof(proc->volfileserver), "%s",
                   volfileserver);
    if (ret < 0)
        goto out;

out:
    if (ret > 0)
        ret = 0;

    return ret;
}

int
glusterd_proc_stop(glusterd_proc_t *proc, int sig, int flags)
{
    /* NB: Copy-paste code from glusterd_service_stop, the source may be
     * removed once all daemon management use proc */

    int32_t ret = -1;
    pid_t pid = -1;
    xlator_t *this = NULL;
    glusterd_conf_t *conf = NULL;

    this = THIS;
    GF_ASSERT(this);

    conf = this->private;
    GF_ASSERT(conf);

    if (!gf_is_service_running(proc->pidfile, &pid)) {
        ret = 0;
        gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_ALREADY_STOPPED,
               "%s already stopped", proc->name);
        goto out;
    }
    gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_SVC_STOP_SUCCESS,
           "Stopping %s daemon running in pid: "
           "%d",
           proc->name, pid);

    ret = kill(pid, sig);
    if (ret) {
        switch (errno) {
            case ESRCH:
                gf_msg_debug(this->name, 0,
                             "%s is already "
                             "stopped",
                             proc->name);
                ret = 0;
                goto out;
            default:
                gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_SVC_KILL_FAIL,
                       "Unable to kill %s "
                       "service, reason:%s",
                       proc->name, strerror(errno));
        }
    } else {
        (void)glusterd_unlink_file(proc->pidfile);
    }
    if (flags != PROC_STOP_FORCE)
        goto out;

    synclock_unlock(&conf->big_lock);
    sleep(1);
    synclock_lock(&conf->big_lock);
    if (gf_is_service_running(proc->pidfile, &pid)) {
        ret = kill(pid, SIGKILL);
        if (ret) {
            gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_PID_KILL_FAIL,
                   "Unable to kill pid:%d, "
                   "reason:%s",
                   pid, strerror(errno));
            goto out;
        }
        ret = glusterd_unlink_file(proc->pidfile);
        if (ret)
            goto out;
    }

    ret = 0;
out:
    return ret;
}

int
glusterd_proc_get_pid(glusterd_proc_t *proc)
{
    int pid = -1;
    (void)gf_is_service_running(proc->pidfile, &pid);
    return pid;
}

int
glusterd_proc_is_running(glusterd_proc_t *proc)
{
    int pid = -1;

    return gf_is_service_running(proc->pidfile, &pid);
}