From 8679ed648f6da820877a735b4b6e6d0676ddc324 Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Thu, 11 Aug 2016 16:37:46 +0900 Subject: [PATCH] sbin/hammer: Move score_printf()/hammer_check_restrict() to cmd_mirror.c Make sbin/hammer/misc.c independent of /sbin/hammer's global variables (used by two functions score_printf()/hammer_check_restrict()) that aren't accessible from other programs such as /sbin/newfs_hammer which already links to some of sbin/hammer/*.o's. /sbin/newfs_hammer could make use of misc.o, but globals have been preventing from linking against sbin/hammer/misc.o. If these are to be used by other hammer commands, they need to be reimplemented without dependencies. --- sbin/hammer/cmd_mirror.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ sbin/hammer/hammer.c | 1 - sbin/hammer/hammer.h | 3 +- sbin/hammer/hammer_util.h | 4 --- sbin/hammer/misc.c | 67 --------------------------------------------- 5 files changed, 72 insertions(+), 73 deletions(-) diff --git a/sbin/hammer/cmd_mirror.c b/sbin/hammer/cmd_mirror.c index 1d23996109..03022249eb 100644 --- a/sbin/hammer/cmd_mirror.c +++ b/sbin/hammer/cmd_mirror.c @@ -45,6 +45,9 @@ typedef struct histogram { uint64_t bytes; } *histogram_t; +const char *ScoreBoardFile; +const char *RestrictTarget; + static int read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup); static int generate_histogram(int fd, const char *filesystem, @@ -64,6 +67,8 @@ static void update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id); static ssize_t writebw(int fd, const void *buf, size_t nbytes, uint64_t *bwcount, struct timeval *tv1); static int getyntty(void); +static void score_printf(size_t i, size_t w, const char *ctl, ...); +static void hammer_check_restrict(const char *filesystem); static void mirror_usage(int code); /* @@ -1721,6 +1726,71 @@ getyntty(void) } static void +score_printf(size_t i, size_t w, const char *ctl, ...) +{ + va_list va; + size_t n; + static size_t SSize; + static int SFd = -1; + static char ScoreBuf[1024]; + + if (ScoreBoardFile == NULL) + return; + assert(i + w < sizeof(ScoreBuf)); + if (SFd < 0) { + SFd = open(ScoreBoardFile, O_RDWR|O_CREAT|O_TRUNC, 0644); + if (SFd < 0) + return; + SSize = 0; + } + for (n = 0; n < i; ++n) { + if (ScoreBuf[n] == 0) + ScoreBuf[n] = ' '; + } + va_start(va, ctl); + vsnprintf(ScoreBuf + i, w - 1, ctl, va); + va_end(va); + n = strlen(ScoreBuf + i); + while (n < w - 1) { + ScoreBuf[i + n] = ' '; + ++n; + } + ScoreBuf[i + n] = '\n'; + if (SSize < i + w) + SSize = i + w; + pwrite(SFd, ScoreBuf, SSize, 0); +} + +static void +hammer_check_restrict(const char *filesystem) +{ + size_t rlen; + int atslash; + + if (RestrictTarget == NULL) + return; + rlen = strlen(RestrictTarget); + if (strncmp(filesystem, RestrictTarget, rlen) != 0) { + fprintf(stderr, "hammer-remote: restricted target\n"); + exit(1); + } + atslash = 1; + while (filesystem[rlen]) { + if (atslash && + filesystem[rlen] == '.' && + filesystem[rlen+1] == '.') { + fprintf(stderr, "hammer-remote: '..' not allowed\n"); + exit(1); + } + if (filesystem[rlen] == '/') + atslash = 1; + else + atslash = 0; + ++rlen; + } +} + +static void mirror_usage(int code) { fprintf(stderr, diff --git a/sbin/hammer/hammer.c b/sbin/hammer/hammer.c index dbdd15d304..0b069e407b 100644 --- a/sbin/hammer/hammer.c +++ b/sbin/hammer/hammer.c @@ -59,7 +59,6 @@ uint64_t SplitupOpt = 4ULL * 1024ULL * 1024ULL * 1024ULL; uint64_t MemoryLimit = 1024LLU * 1024 * 1024; const char *SplitupOptStr; const char *CyclePath; -const char *RestrictTarget; int main(int ac, char **av) diff --git a/sbin/hammer/hammer.h b/sbin/hammer/hammer.h index 706c45e78e..34b200cec0 100644 --- a/sbin/hammer/hammer.h +++ b/sbin/hammer/hammer.h @@ -82,6 +82,8 @@ extern uint64_t SplitupOpt; extern uint64_t MemoryLimit; extern const char *SplitupOptStr; extern const char *CyclePath; +extern const char *ScoreBoardFile; +extern const char *RestrictTarget; void hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent); void hammer_cmd_show_undo(void); @@ -132,7 +134,6 @@ void hammer_reset_cycle(void); int getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path); void relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs); void dump_pfsd(hammer_pseudofs_data_t, int); -void hammer_check_restrict(const char *path); int getyn(void); const char *sizetostr(off_t size); int hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *iocp); diff --git a/sbin/hammer/hammer_util.h b/sbin/hammer/hammer_util.h index 4c7a1fadd7..cd29e4c40c 100644 --- a/sbin/hammer/hammer_util.h +++ b/sbin/hammer/hammer_util.h @@ -138,8 +138,6 @@ struct zone_stat { extern uuid_t Hammer_FSType; extern uuid_t Hammer_FSId; extern int DebugOpt; -extern const char *ScoreBoardFile; -extern const char *RestrictTarget; extern struct volume_list VolList; extern int UseReadBehind; extern int UseReadAhead; @@ -201,8 +199,6 @@ void hammer_cache_del(struct cache_info *cache); void hammer_cache_used(struct cache_info *cache); void hammer_cache_flush(void); -void score_printf(size_t i, size_t w, const char *ctl, ...) __printflike(3, 4); - struct zone_stat *hammer_init_zone_stat(void); struct zone_stat *hammer_init_zone_stat_bits(void); void hammer_cleanup_zone_stat(struct zone_stat *stats); diff --git a/sbin/hammer/misc.c b/sbin/hammer/misc.c index 15e4b357df..f1568d52e5 100644 --- a/sbin/hammer/misc.c +++ b/sbin/hammer/misc.c @@ -36,8 +36,6 @@ #include "hammer.h" -const char *ScoreBoardFile; - /* * (taken from /usr/src/sys/vfs/hammer/hammer_btree.c) * @@ -132,71 +130,6 @@ hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf) return (leaf->data_crc == crc); } -void -score_printf(size_t i, size_t w, const char *ctl, ...) -{ - va_list va; - size_t n; - static size_t SSize; - static int SFd = -1; - static char ScoreBuf[1024]; - - if (ScoreBoardFile == NULL) - return; - assert(i + w < sizeof(ScoreBuf)); - if (SFd < 0) { - SFd = open(ScoreBoardFile, O_RDWR|O_CREAT|O_TRUNC, 0644); - if (SFd < 0) - return; - SSize = 0; - } - for (n = 0; n < i; ++n) { - if (ScoreBuf[n] == 0) - ScoreBuf[n] = ' '; - } - va_start(va, ctl); - vsnprintf(ScoreBuf + i, w - 1, ctl, va); - va_end(va); - n = strlen(ScoreBuf + i); - while (n < w - 1) { - ScoreBuf[i + n] = ' '; - ++n; - } - ScoreBuf[i + n] = '\n'; - if (SSize < i + w) - SSize = i + w; - pwrite(SFd, ScoreBuf, SSize, 0); -} - -void -hammer_check_restrict(const char *filesystem) -{ - size_t rlen; - int atslash; - - if (RestrictTarget == NULL) - return; - rlen = strlen(RestrictTarget); - if (strncmp(filesystem, RestrictTarget, rlen) != 0) { - fprintf(stderr, "hammer-remote: restricted target\n"); - exit(1); - } - atslash = 1; - while (filesystem[rlen]) { - if (atslash && - filesystem[rlen] == '.' && - filesystem[rlen+1] == '.') { - fprintf(stderr, "hammer-remote: '..' not allowed\n"); - exit(1); - } - if (filesystem[rlen] == '/') - atslash = 1; - else - atslash = 0; - ++rlen; - } -} - int getyn(void) { -- 2.11.4.GIT