2
Copyright (c) 2011-2012 Red Hat, Inc. <http://www.redhat.com>
3
This file is part of GlusterFS.
5
This file is licensed to you under your choice of the GNU Lesser
6
General Public License, version 3 or any later version (LGPLv3 or
7
later), or the GNU General Public License, version 2 (GPLv2), in all
8
cases as published by the Free Software Foundation.
10
#include <glusterfs/common-utils.h>
11
#include "glusterd-op-sm.h"
12
#include "glusterd-store.h"
13
#include "glusterd-utils.h"
14
#include "glusterd-volgen.h"
15
#include "glusterd-messages.h"
16
#include <glusterfs/syscall.h>
21
__glusterd_handle_log_rotate(rpcsvc_request_t *req)
24
gf_cli_req cli_req = {{
28
glusterd_op_t cli_op = GD_OP_LOG_ROTATE;
33
xlator_t *this = THIS;
37
ret = xdr_to_generic(req->msg[0], &cli_req, (xdrproc_t)xdr_gf_cli_req);
39
// failed to decode msg;
40
req->rpc_err = GARBAGE_ARGS;
41
gf_smsg(this->name, GF_LOG_ERROR, errno, GD_MSG_GARBAGE_ARGS, NULL);
45
if (cli_req.dict.dict_len) {
46
/* Unserialize the dictionary */
49
ret = dict_unserialize(cli_req.dict.dict_val, cli_req.dict.dict_len,
52
gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_UNSERIALIZE_FAIL,
54
"unserialize req-buffer to dictionary");
55
snprintf(msg, sizeof(msg),
56
"Unable to decode the "
62
ret = dict_get_str(dict, "volname", &volname);
64
snprintf(msg, sizeof(msg), "Failed to get volume name");
65
gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED, "%s", msg);
69
gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_LOG_ROTATE_REQ_RECVD,
70
"Received log rotate req "
74
ret = dict_set_uint64(dict, "rotate-key", (uint64_t)gf_time());
78
ret = glusterd_op_begin_synctask(req, GD_OP_LOG_ROTATE, dict);
83
snprintf(msg, sizeof(msg), "Operation failed");
84
ret = glusterd_op_send_cli_response(cli_op, ret, 0, req, dict, msg);
87
free(cli_req.dict.dict_val);
92
glusterd_handle_log_rotate(rpcsvc_request_t *req)
94
return glusterd_big_locked_handler(req, __glusterd_handle_log_rotate);
99
glusterd_op_stage_log_rotate(dict_t *dict, char **op_errstr)
102
char *volname = NULL;
103
glusterd_volinfo_t *volinfo = NULL;
104
char msg[2048] = {0};
107
ret = dict_get_str(dict, "volname", &volname);
109
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
110
"Unable to get volume name");
114
ret = glusterd_volinfo_find(volname, &volinfo);
116
snprintf(msg, sizeof(msg), "Volume %s does not exist", volname);
117
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_VOL_NOT_FOUND, "%s", msg);
118
*op_errstr = gf_strdup(msg);
122
if (_gf_false == glusterd_is_volume_started(volinfo)) {
123
snprintf(msg, sizeof(msg),
124
"Volume %s needs to be started before"
127
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_VOL_NOT_STARTED, "%s", msg);
128
*op_errstr = gf_strdup(msg);
133
ret = dict_get_str(dict, "brick", &brick);
134
/* If no brick is specified, do log-rotate for
135
all the bricks in the volume */
137
gf_smsg("glusterd", GF_LOG_ERROR, -ret, GD_MSG_DICT_GET_FAILED,
143
ret = glusterd_volume_brickinfo_get_by_brick(brick, volinfo, NULL,
146
snprintf(msg, sizeof(msg),
147
"Incorrect brick %s "
150
gf_msg("glusterd", GF_LOG_ERROR, EINVAL, GD_MSG_INVALID_ENTRY, "%s",
152
*op_errstr = gf_strdup(msg);
156
gf_msg_debug("glusterd", 0, "Returning %d", ret);
162
glusterd_op_log_rotate(dict_t *dict)
165
glusterd_conf_t *priv = NULL;
166
glusterd_volinfo_t *volinfo = NULL;
167
glusterd_brickinfo_t *brickinfo = NULL;
168
char *volname = NULL;
170
char logfile[PATH_MAX] = {
173
char pidfile[PATH_MAX] = {
180
glusterd_brickinfo_t *tmpbrkinfo = NULL;
182
priv = THIS->private;
185
ret = dict_get_str(dict, "volname", &volname);
187
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
188
"volname not found");
192
ret = dict_get_uint64(dict, "rotate-key", &key);
194
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
195
"rotate key not found");
199
ret = dict_get_str(dict, "brick", &brick);
200
/* If no brick is specified, do log-rotate for
201
all the bricks in the volume */
203
gf_smsg("glusterd", GF_LOG_ERROR, -ret, GD_MSG_DICT_GET_FAILED,
208
ret = glusterd_brickinfo_new_from_brick(brick, &tmpbrkinfo, _gf_false,
211
gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_BRICK_NOT_FOUND,
212
"cannot get brickinfo from brick");
217
ret = glusterd_volinfo_find(volname, &volinfo);
222
cds_list_for_each_entry(brickinfo, &volinfo->bricks, brick_list)
224
if (gf_uuid_compare(brickinfo->uuid, MY_UUID))
227
if (tmpbrkinfo && brick &&
228
(strcmp(tmpbrkinfo->hostname, brickinfo->hostname) ||
229
strcmp(tmpbrkinfo->path, brickinfo->path)))
234
GLUSTERD_GET_BRICK_PIDFILE(pidfile, volinfo, brickinfo, priv);
235
file = fopen(pidfile, "r+");
237
gf_msg("glusterd", GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
238
"Unable to open pidfile: %s", pidfile);
243
ret = fscanf(file, "%d", &pid);
246
gf_msg("glusterd", GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
247
"Unable to read pidfile: %s", pidfile);
254
snprintf(logfile, PATH_MAX, "%s.%" PRIu64, brickinfo->logfile, key);
256
ret = sys_rename(brickinfo->logfile, logfile);
258
gf_msg("glusterd", GF_LOG_WARNING, errno, GD_MSG_FILE_OP_FAILED,
261
ret = kill(pid, SIGHUP);
263
gf_msg("glusterd", GF_LOG_ERROR, errno, GD_MSG_PID_KILL_FAIL,
264
"Unable to SIGHUP to %d", pid);
269
/* If request was for brick, only one iteration is enough */
274
if (ret && !valid_brick)
279
glusterd_brickinfo_delete(tmpbrkinfo);