Skip to content

Commit 2e03bc7

Browse files
committed
CLEANUP: refactored some command logging codes.
1 parent 9a95d2e commit 2e03bc7

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

cmdlog.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
#define CMDLOG_WRITE_SIZE (4 * 1024) /* 4KB */
3636

3737
#define CMDLOG_FILE_MAXSIZE (10 * 1024 * 1024) /* 10MB : log at most CMDLOG_INPUT_SIZE * N commands in one file */
38-
#define CMDLOG_FILE_MAXNUM 10 /* number of log files */
38+
#define CMDLOG_FILE_MAXNUM 10 /* # of cmdlog files */
39+
#define CMDLOG_DIRPATH_LENGTH 128 /* directory path's length */
3940
#define CMDLOG_FILENAME_LENGTH CMDLOG_DIRPATH_LENGTH + 128
4041
#define CMDLOG_FILENAME_FORMAT "%s/command_%d_%d_%d_%d.log"
4142

@@ -282,6 +283,12 @@ int cmdlog_start(char *file_path, bool *already_started)
282283
*already_started = true;
283284
break;
284285
}
286+
/* check the length of file_path */
287+
if (file_path != NULL && strlen(file_path) > CMDLOG_DIRPATH_LENGTH) {
288+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
289+
"Too long cmdlog file path.\n");
290+
ret = -1; break;
291+
}
285292
/* prepare command logging buffer */
286293
if (cmdlog.buffer.data == NULL) {
287294
if ((cmdlog.buffer.data = malloc(CMDLOG_BUFFER_SIZE)) == NULL) {

cmdlog.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "memcached/extension_loggers.h"
2121

2222
#define COMMAND_LOGGING
23-
#define CMDLOG_DIRPATH_LENGTH 128 /* directory path's length */
2423

2524
void cmdlog_init(int port, EXTENSION_LOGGER_DESCRIPTOR *logger);
2625
void cmdlog_final(void);

memcached.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9943,22 +9943,16 @@ static void process_scan_command(conn *c, token_t *tokens, const size_t ntokens)
99439943
#endif
99449944

99459945
#ifdef COMMAND_LOGGING
9946-
static void process_logging_command(conn *c, token_t *tokens, const size_t ntokens)
9946+
static void process_cmdlog_command(conn *c, token_t *tokens, const size_t ntokens)
99479947
{
9948-
char *type = tokens[COMMAND_TOKEN+1].value;
9948+
char *type = tokens[SUBCOMMAND_TOKEN].value;
99499949
bool already_check = false;
99509950

99519951
if (ntokens > 2 && strcmp(type, "start") == 0) {
99529952
char *fpath = NULL;
99539953
if (ntokens > 3) {
9954-
if (tokens[SUBCOMMAND_TOKEN+1].length > CMDLOG_DIRPATH_LENGTH) {
9955-
out_string(c, "\tcommand logging failed to start, path exceeds 128.\n");
9956-
cmdlog_in_use = false;
9957-
return;
9958-
}
99599954
fpath = tokens[SUBCOMMAND_TOKEN+1].value;
99609955
}
9961-
99629956
int ret = cmdlog_start(fpath, &already_check);
99639957
if (already_check) {
99649958
out_string(c, "\tcommand logging already started.\n");
@@ -13258,7 +13252,7 @@ static void process_command_ascii(conn *c, char *command, int cmdlen)
1325813252
#ifdef COMMAND_LOGGING
1325913253
else if ((ntokens >= 2) && (strcmp(tokens[COMMAND_TOKEN].value, "cmdlog") == 0))
1326013254
{
13261-
process_logging_command(c, tokens, ntokens);
13255+
process_cmdlog_command(c, tokens, ntokens);
1326213256
}
1326313257
#endif
1326413258
#ifdef DETECT_LONG_QUERY

0 commit comments

Comments
 (0)