From 998a494fa9a46b92d4c7fb3485a0faeb78beb2cd Mon Sep 17 00:00:00 2001 From: Shaun Tancheff Date: Tue, 29 Oct 2019 17:14:18 -0500 Subject: [PATCH] LU-12861 libcfs: provide an scnprintf and start using it snprintf() returns the number of chars that would be needed to hold the complete result, which may be larger that the buffer size. scnprintf differs in it's return value is number of chars actually written (not including the terminating null). Correct the few patterns where the return from snprintf() is used and expected not to exceed the passed buffer size. Test-Parameters: trivial Cray-bug-id: LUS-7999 Signed-off-by: Shaun Tancheff Change-Id: Ie42458be16e8c0ba1cb6d688fd418683f18de21e Reviewed-on: https://review.whamcloud.com/36453 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Sebastien Buisson Reviewed-by: James Simmons Reviewed-by: Petros Koutoupis Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/util/string.h | 32 +++ libcfs/libcfs/util/nidstrings.c | 8 +- libcfs/libcfs/util/string.c | 6 +- lnet/lnet/config.c | 6 +- lnet/lnet/router_proc.c | 128 +++++------ lnet/utils/lnetconfig/liblnetconfig.c | 12 +- lustre/lod/lod_dev.c | 6 +- lustre/lod/lod_object.c | 4 +- lustre/mgs/mgs_llog.c | 4 +- lustre/osc/lproc_osc.c | 6 +- lustre/tests/fsx.c | 5 +- lustre/utils/debug.c | 4 +- lustre/utils/gss/cacheio.c | 9 +- lustre/utils/gss/lgss_utils.c | 420 +++++++++++++++++----------------- lustre/utils/gss/sk_utils.c | 3 +- lustre/utils/lfs.c | 26 ++- lustre/utils/lsnapshot.c | 27 +-- lustre/utils/mount_lustre.c | 62 ++--- 18 files changed, 401 insertions(+), 367 deletions(-) diff --git a/libcfs/include/libcfs/util/string.h b/libcfs/include/libcfs/util/string.h index 065829b..f73fd79 100644 --- a/libcfs/include/libcfs/util/string.h +++ b/libcfs/include/libcfs/util/string.h @@ -40,10 +40,42 @@ #define __LIBCFS_UTIL_STRING_H__ #include +#include #include #include +static inline +int vscnprintf(char *buf, size_t bufsz, const char *format, va_list args) +{ + int ret; + + if (!bufsz) + return 0; + + ret = vsnprintf(buf, bufsz, format, args); + return (bufsz > ret) ? ret : bufsz - 1; +} + +/* __printf from linux kernel */ +#ifndef __printf +#define __printf(a, b) __attribute__((__format__(printf, a, b))) +#endif + +__printf(3, 4) +static inline int scnprintf(char *buf, size_t bufsz, const char *format, ...) +{ + int ret; + va_list args; + + va_start(args, format); + ret = vscnprintf(buf, bufsz, format, args); + va_end(args); + + return ret; +} + + /** * Structure to represent NULL-less strings. */ diff --git a/libcfs/libcfs/util/nidstrings.c b/libcfs/libcfs/util/nidstrings.c index 69641a4..b3380d9 100644 --- a/libcfs/libcfs/util/nidstrings.c +++ b/libcfs/libcfs/util/nidstrings.c @@ -238,7 +238,7 @@ libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list) list_for_each_entry(el, list, el_link) { assert(j++ < 4); if (i != 0) - i += snprintf(buffer + i, count - i, "."); + i += scnprintf(buffer + i, count - i, "."); i += cfs_expr_list_print(buffer + i, count - i, el); } return i; @@ -1061,7 +1061,7 @@ cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges, list_for_each_entry(ar, addrranges, ar_link) { if (i != 0) - i += snprintf(buffer + i, count - i, " "); + i += scnprintf(buffer + i, count - i, " "); i += nf->nf_print_addrlist(buffer + i, count - i, &ar->ar_numaddr_ranges); i += cfs_print_network(buffer + i, count - i, nr); @@ -1086,11 +1086,11 @@ int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist) list_for_each_entry(nr, nidlist, nr_link) { if (i != 0) - i += snprintf(buffer + i, count - i, " "); + i += scnprintf(buffer + i, count - i, " "); if (nr->nr_all != 0) { assert(list_empty(&nr->nr_addrranges)); - i += snprintf(buffer + i, count - i, "*"); + i += scnprintf(buffer + i, count - i, "*"); i += cfs_print_network(buffer + i, count - i, nr); } else { i += cfs_print_addrranges(buffer + i, count - i, diff --git a/libcfs/libcfs/util/string.c b/libcfs/libcfs/util/string.c index 2c1a24c..ce96090 100644 --- a/libcfs/libcfs/util/string.c +++ b/libcfs/libcfs/util/string.c @@ -255,17 +255,17 @@ cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list) numexprs++; if (numexprs > 1) - i += snprintf(buffer + i, count - i, "["); + i += scnprintf(buffer + i, count - i, "["); list_for_each_entry(expr, &expr_list->el_exprs, re_link) { if (j++ != 0) - i += snprintf(buffer + i, count - i, ","); + i += scnprintf(buffer + i, count - i, ","); i += cfs_range_expr_print(buffer + i, count - i, expr, numexprs > 1); } if (numexprs > 1) - i += snprintf(buffer + i, count - i, "]"); + i += scnprintf(buffer + i, count - i, "]"); return i; } diff --git a/lnet/lnet/config.c b/lnet/lnet/config.c index 412bd50..8606f48 100644 --- a/lnet/lnet/config.c +++ b/lnet/lnet/config.c @@ -1576,9 +1576,9 @@ lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip) list_move_tail(&tb->ltb_list, &matched_nets); - len += snprintf(networks + len, sizeof(networks) - len, - "%s%s", (len == 0) ? "" : ",", - tb->ltb_text); + len += scnprintf(networks + len, sizeof(networks) - len, + "%s%s", (len == 0) ? "" : ",", + tb->ltb_text); if (len >= sizeof(networks)) { CERROR("Too many matched networks\n"); diff --git a/lnet/lnet/router_proc.c b/lnet/lnet/router_proc.c index 2a47ee6..22784fb 100644 --- a/lnet/lnet/router_proc.c +++ b/lnet/lnet/router_proc.c @@ -107,17 +107,17 @@ static int __proc_lnet_stats(void *data, int write, lnet_counters_get(ctrs); common = ctrs->lct_common; - len = snprintf(tmpstr, tmpsiz, - "%u %u %u %u %u %u %u %llu %llu " - "%llu %llu", - common.lcc_msgs_alloc, common.lcc_msgs_max, - common.lcc_errors, - common.lcc_send_count, common.lcc_recv_count, - common.lcc_route_count, common.lcc_drop_count, - common.lcc_send_length, common.lcc_recv_length, - common.lcc_route_length, common.lcc_drop_length); - - if (pos >= min_t(int, len, strlen(tmpstr))) + len = scnprintf(tmpstr, tmpsiz, + "%u %u %u %u %u %u %u %llu %llu " + "%llu %llu", + common.lcc_msgs_alloc, common.lcc_msgs_max, + common.lcc_errors, + common.lcc_send_count, common.lcc_recv_count, + common.lcc_route_count, common.lcc_drop_count, + common.lcc_send_length, common.lcc_recv_length, + common.lcc_route_length, common.lcc_drop_length); + + if (pos >= len) rc = 0; else rc = cfs_trace_copyout_string(buffer, nob, @@ -165,12 +165,12 @@ proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer, s = tmpstr; /* points to current position in tmpstr[] */ if (*ppos == 0) { - s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n", - the_lnet.ln_routing ? "enabled" : "disabled"); + s += scnprintf(s, tmpstr + tmpsiz - s, "Routing %s\n", + the_lnet.ln_routing ? "enabled" : "disabled"); LASSERT(tmpstr + tmpsiz - s > 0); - s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n", - "net", "hops", "priority", "state", "router"); + s += scnprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n", + "net", "hops", "priority", "state", "router"); LASSERT(tmpstr + tmpsiz - s > 0); lnet_net_lock(0); @@ -229,12 +229,12 @@ proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer, unsigned int priority = route->lr_priority; int alive = lnet_is_route_alive(route); - s += snprintf(s, tmpstr + tmpsiz - s, - "%-8s %4d %8u %7s %s\n", - libcfs_net2str(net), hops, - priority, - alive ? "up" : "down", - libcfs_nid2str(route->lr_nid)); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-8s %4d %8u %7s %s\n", + libcfs_net2str(net), hops, + priority, + alive ? "up" : "down", + libcfs_nid2str(route->lr_nid)); LASSERT(tmpstr + tmpsiz - s > 0); } @@ -289,9 +289,9 @@ proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer, s = tmpstr; /* points to current position in tmpstr[] */ if (*ppos == 0) { - s += snprintf(s, tmpstr + tmpsiz - s, - "%-4s %7s %5s %s\n", - "ref", "rtr_ref", "alive", "router"); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-4s %7s %5s %s\n", + "ref", "rtr_ref", "alive", "router"); LASSERT(tmpstr + tmpsiz - s > 0); lnet_net_lock(0); @@ -334,11 +334,11 @@ proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer, int nrtrrefs = peer->lp_rtr_refcount; int alive = lnet_is_gateway_alive(peer); - s += snprintf(s, tmpstr + tmpsiz - s, - "%-4d %7d %5s %s\n", - nrefs, nrtrrefs, - alive ? "up" : "down", - libcfs_nid2str(nid)); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-4d %7d %5s %s\n", + nrefs, nrtrrefs, + alive ? "up" : "down", + libcfs_nid2str(nid)); } lnet_net_unlock(0); @@ -421,10 +421,10 @@ proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer, s = tmpstr; /* points to current position in tmpstr[] */ if (*ppos == 0) { - s += snprintf(s, tmpstr + tmpsiz - s, - "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n", - "nid", "refs", "state", "last", "max", - "rtr", "min", "tx", "min", "queue"); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n", + "nid", "refs", "state", "last", "max", + "rtr", "min", "tx", "min", "queue"); LASSERT(tmpstr + tmpsiz - s > 0); hoff++; @@ -506,11 +506,11 @@ proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer, lnet_net_unlock(cpt); - s += snprintf(s, tmpstr + tmpsiz - s, - "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n", - libcfs_nid2str(nid), nrefs, aliveness, - lastalive, maxcr, rtrcr, minrtrcr, txcr, - mintxcr, txqnob); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n", + libcfs_nid2str(nid), nrefs, aliveness, + lastalive, maxcr, rtrcr, minrtrcr, txcr, + mintxcr, txqnob); LASSERT(tmpstr + tmpsiz - s > 0); } else { /* peer is NULL */ @@ -566,9 +566,9 @@ static int __proc_lnet_buffers(void *data, int write, s = tmpstr; /* points to current position in tmpstr[] */ - s += snprintf(s, tmpstr + tmpsiz - s, - "%5s %5s %7s %7s\n", - "pages", "count", "credits", "min"); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%5s %5s %7s %7s\n", + "pages", "count", "credits", "min"); LASSERT(tmpstr + tmpsiz - s > 0); if (the_lnet.ln_rtrpools == NULL) @@ -579,12 +579,12 @@ static int __proc_lnet_buffers(void *data, int write, lnet_net_lock(LNET_LOCK_EX); cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) { - s += snprintf(s, tmpstr + tmpsiz - s, - "%5d %5d %7d %7d\n", - rbp[idx].rbp_npages, - rbp[idx].rbp_nbuffers, - rbp[idx].rbp_credits, - rbp[idx].rbp_mincredits); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%5d %5d %7d %7d\n", + rbp[idx].rbp_npages, + rbp[idx].rbp_nbuffers, + rbp[idx].rbp_credits, + rbp[idx].rbp_mincredits); LASSERT(tmpstr + tmpsiz - s > 0); } lnet_net_unlock(LNET_LOCK_EX); @@ -667,10 +667,10 @@ proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer, s = tmpstr; /* points to current position in tmpstr[] */ if (*ppos == 0) { - s += snprintf(s, tmpstr + tmpsiz - s, - "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n", - "nid", "status", "alive", "refs", "peer", - "rtr", "max", "tx", "min"); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n", + "nid", "status", "alive", "refs", "peer", + "rtr", "max", "tx", "min"); LASSERT (tmpstr + tmpsiz - s > 0); } else { struct lnet_ni *ni = NULL; @@ -718,14 +718,14 @@ proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer, if (i != 0) lnet_net_lock(i); - s += snprintf(s, tmpstr + tmpsiz - s, - "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n", - libcfs_nid2str(ni->ni_nid), stat, - last_alive, *ni->ni_refs[i], - ni->ni_net->net_tunables.lct_peer_tx_credits, - ni->ni_net->net_tunables.lct_peer_rtr_credits, - tq->tq_credits_max, - tq->tq_credits, tq->tq_credits_min); + s += scnprintf(s, tmpstr + tmpsiz - s, + "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n", + libcfs_nid2str(ni->ni_nid), stat, + last_alive, *ni->ni_refs[i], + ni->ni_net->net_tunables.lct_peer_tx_credits, + ni->ni_net->net_tunables.lct_peer_rtr_credits, + tq->tq_credits_max, + tq->tq_credits, tq->tq_credits_min); if (i != 0) lnet_net_unlock(i); } @@ -815,11 +815,11 @@ static int __proc_lnet_portal_rotor(void *data, int write, LASSERT(portal_rotors[i].pr_value == portal_rotor); lnet_res_unlock(0); - rc = snprintf(buf, buf_len, - "{\n\tportals: all\n" - "\trotor: %s\n\tdescription: %s\n}", - portal_rotors[i].pr_name, - portal_rotors[i].pr_desc); + rc = scnprintf(buf, buf_len, + "{\n\tportals: all\n" + "\trotor: %s\n\tdescription: %s\n}", + portal_rotors[i].pr_name, + portal_rotors[i].pr_desc); if (pos >= min_t(int, rc, buf_len)) { rc = 0; diff --git a/lnet/utils/lnetconfig/liblnetconfig.c b/lnet/utils/lnetconfig/liblnetconfig.c index 2004833..ac48ec3 100644 --- a/lnet/utils/lnetconfig/liblnetconfig.c +++ b/lnet/utils/lnetconfig/liblnetconfig.c @@ -2271,7 +2271,6 @@ int lustre_lnet_show_net(char *nw, int detail, int seq_no, goto out; for (i = 0;; i++) { - pos = str_buf; __u32 rc_net; memset(buf, 0, buf_size); @@ -2502,17 +2501,18 @@ continue_without_msg_stats: goto out; /* out put the CPTs in the format: "[x,x,x,...]" */ + pos = str_buf; limit = str_buf + str_buf_len - 3; - pos += snprintf(pos, limit - pos, "\"["); + pos += scnprintf(pos, limit - pos, "\"["); for (j = 0 ; ni_data->lic_ncpts >= 1 && j < ni_data->lic_ncpts && pos < limit; j++) { - pos += snprintf(pos, limit - pos, - "%d", ni_data->lic_cpts[j]); + pos += scnprintf(pos, limit - pos, + "%d", ni_data->lic_cpts[j]); if ((j + 1) < ni_data->lic_ncpts) - pos += snprintf(pos, limit - pos, ","); + pos += scnprintf(pos, limit - pos, ","); } - pos += snprintf(pos, 3, "]\""); + snprintf(pos, 3, "]\""); if (ni_data->lic_ncpts >= 1 && cYAML_create_string(item, "CPT", diff --git a/lustre/lod/lod_dev.c b/lustre/lod/lod_dev.c index 63f6ce7..e36190f 100644 --- a/lustre/lod/lod_dev.c +++ b/lustre/lod/lod_dev.c @@ -777,7 +777,7 @@ static char *lod_show_update_logs_retrievers(void *data, int *size, int *count) rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i); LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc); - rc = snprintf(buf + len, *size - len, " %04x", i); + rc = scnprintf(buf + len, *size - len, " %04x", i); LASSERT(rc > 0); len += rc; @@ -786,8 +786,8 @@ static char *lod_show_update_logs_retrievers(void *data, int *size, int *count) lod_foreach_mdt(lod, mdt) { if (!mdt->ltd_got_update_log) { - rc = snprintf(buf + len, *size - len, " %04x", - mdt->ltd_index); + rc = scnprintf(buf + len, *size - len, " %04x", + mdt->ltd_index); if (unlikely(rc <= 0)) break; diff --git a/lustre/lod/lod_object.c b/lustre/lod/lod_object.c index 2fcb93b..8e3574f 100644 --- a/lustre/lod/lod_object.c +++ b/lustre/lod/lod_object.c @@ -871,8 +871,8 @@ int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo, goto next; } - len = snprintf(name, sizeof(name), - DFID":", PFID(&ent->lde_fid)); + len = scnprintf(name, sizeof(name), + DFID":", PFID(&ent->lde_fid)); /* The ent->lde_name is composed of ${FID}:${index} */ if (ent->lde_namelen < len + 1 || memcmp(ent->lde_name, name, len) != 0) { diff --git a/lustre/mgs/mgs_llog.c b/lustre/mgs/mgs_llog.c index 0ac9a7a..45f7678 100644 --- a/lustre/mgs/mgs_llog.c +++ b/lustre/mgs/mgs_llog.c @@ -4501,8 +4501,8 @@ int mgs_list_logs(const struct lu_env *env, struct mgs_device *mgs, list_del_init(&dirent->mde_list); suffix = strrchr(dirent->mde_name, '-'); if (suffix != NULL) { - l = snprintf(out, remains, "config_log: %s\n", - dirent->mde_name); + l = scnprintf(out, remains, "config_log: %s\n", + dirent->mde_name); out += l; remains -= l; } diff --git a/lustre/osc/lproc_osc.c b/lustre/osc/lproc_osc.c index 2d15ebb..575e6bc 100644 --- a/lustre/osc/lproc_osc.c +++ b/lustre/osc/lproc_osc.c @@ -702,9 +702,9 @@ static ssize_t grant_shrink_show(struct kobject *kobj, struct attribute *attr, LPROCFS_CLIMP_CHECK(obd); imp = obd->u.cli.cl_import; - len = snprintf(buf, PAGE_SIZE, "%d\n", - !imp->imp_grant_shrink_disabled && - OCD_HAS_FLAG(&imp->imp_connect_data, GRANT_SHRINK)); + len = scnprintf(buf, PAGE_SIZE, "%d\n", + !imp->imp_grant_shrink_disabled && + OCD_HAS_FLAG(&imp->imp_connect_data, GRANT_SHRINK)); LPROCFS_CLIMP_EXIT(obd); return len; diff --git a/lustre/tests/fsx.c b/lustre/tests/fsx.c index 71711ab..fb475ce 100644 --- a/lustre/tests/fsx.c +++ b/lustre/tests/fsx.c @@ -57,6 +57,7 @@ #include #include #include +#include #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */ @@ -227,10 +228,10 @@ fill_tf_buf(const struct test_file *tf) return ""; if (max_tf_len == 0) - max_tf_len = snprintf(tf_buf, sizeof(tf_buf) - 1, + max_tf_len = scnprintf(tf_buf, sizeof(tf_buf) - 1, "%u", num_test_files - 1); - sprintf(tf_buf, "[%0*lu]", max_tf_len, + snprintf(tf_buf, sizeof(tf_buf), "[%0*lu]", max_tf_len, (unsigned long)(tf - test_files)); return tf_buf; diff --git a/lustre/utils/debug.c b/lustre/utils/debug.c index d9fe439..5681870 100644 --- a/lustre/utils/debug.c +++ b/lustre/utils/debug.c @@ -197,7 +197,7 @@ static int applymask(char *param, int value) { int rc; char buf[64]; - int len = snprintf(buf, 64, "%d", value); + int len = scnprintf(buf, sizeof(buf), "%d", value); int fd = dbg_open_ctlhandle(param); if (fd < 0) @@ -286,7 +286,7 @@ static void print_rec(struct dbg_line ***linevp, int used, int fdout) int bytes; ssize_t bytes_written; - bytes = snprintf(out, sizeof(out), + bytes = scnprintf(out, sizeof(out), "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:" "(%s:%u:%s()) %s", hdr->ph_subsys, hdr->ph_mask, diff --git a/lustre/utils/gss/cacheio.c b/lustre/utils/gss/cacheio.c index bbb03d7..d3247cd 100644 --- a/lustre/utils/gss/cacheio.c +++ b/lustre/utils/gss/cacheio.c @@ -56,6 +56,7 @@ #include #include #include +#include #include "err_util.h" void qword_add(char **bpp, int *lp, const char *str) @@ -125,9 +126,7 @@ void qword_addint(char **bpp, int *lp, int n) { int len; - len = snprintf(*bpp, *lp, "%d ", n); - if (len > *lp) - len = *lp; + len = scnprintf(*bpp, *lp, "%d ", n); *bpp += len; *lp -= len; } @@ -136,9 +135,7 @@ void qword_adduint(char **bpp, int *lp, unsigned int n) { int len; - len = snprintf(*bpp, *lp, "%u ", n); - if (len > *lp) - len = *lp; + len = scnprintf(*bpp, *lp, "%u ", n); *bpp += len; *lp -= len; } diff --git a/lustre/utils/gss/lgss_utils.c b/lustre/utils/gss/lgss_utils.c index b87d5d6..825c421 100644 --- a/lustre/utils/gss/lgss_utils.c +++ b/lustre/utils/gss/lgss_utils.c @@ -96,15 +96,16 @@ #ifdef HAVE_COM_ERR_H #include #endif +#include #include "lsupport.h" #include "lgss_utils.h" #include "lgss_krb5_utils.h" const char *lgss_svc_str[LGSS_SVC_MAX] = { - [LGSS_SVC_MGS] = LGSS_SVC_MGS_STR, - [LGSS_SVC_MDS] = LGSS_SVC_MDS_STR, - [LGSS_SVC_OSS] = LGSS_SVC_OSS_STR, + [LGSS_SVC_MGS] = LGSS_SVC_MGS_STR, + [LGSS_SVC_MDS] = LGSS_SVC_MDS_STR, + [LGSS_SVC_OSS] = LGSS_SVC_OSS_STR, }; /**************************************** @@ -112,111 +113,111 @@ const char *lgss_svc_str[LGSS_SVC_MAX] = { ****************************************/ static struct lgss_mutex_s { - char *sem_name; - key_t sem_key; - int sem_id; + char *sem_name; + key_t sem_key; + int sem_id; } lgss_mutexes[LGSS_MUTEX_MAX] = { [LGSS_MUTEX_KRB5] = { "keyring", 0x4292d473, 0 }, }; static int lgss_mutex_get(struct lgss_mutex_s *mutex) { - mutex->sem_id = semget(mutex->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700); - if (mutex->sem_id != -1) { - if (semctl(mutex->sem_id, 0, SETVAL, 1) == -1) { - logmsg(LL_ERR, "initialize sem %x: %s\n", - mutex->sem_key, strerror(errno)); - return -1; - } - - logmsg(LL_DEBUG, "created & initialized sem %x id %d for %s\n", - mutex->sem_key, mutex->sem_id, mutex->sem_name); - } else { - if (errno != EEXIST) { - logmsg(LL_ERR, "create sem %x: %s\n", - mutex->sem_key, strerror(errno)); - return -1; - } - - /* already created by someone else, simply get it. - * Note there's still a small window of racing between create - * and initialize, a flaw in semaphore semantics */ - mutex->sem_id = semget(mutex->sem_key, 0, 0700); - if (mutex->sem_id == -1) { - if (errno == ENOENT) { - logmsg(LL_WARN, "sem %x just disappeared " - "under us, try again\n", mutex->sem_key); - return 1; - } - - logmsg(LL_ERR, "get sem %x: %s\n", mutex->sem_key, - strerror(errno)); - return -1; - } - - logmsg(LL_TRACE, "got sem %x id %d for %s\n", - mutex->sem_key, mutex->sem_id, mutex->sem_name); - } - - return 0; + mutex->sem_id = semget(mutex->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700); + if (mutex->sem_id != -1) { + if (semctl(mutex->sem_id, 0, SETVAL, 1) == -1) { + logmsg(LL_ERR, "initialize sem %x: %s\n", + mutex->sem_key, strerror(errno)); + return -1; + } + + logmsg(LL_DEBUG, "created & initialized sem %x id %d for %s\n", + mutex->sem_key, mutex->sem_id, mutex->sem_name); + } else { + if (errno != EEXIST) { + logmsg(LL_ERR, "create sem %x: %s\n", + mutex->sem_key, strerror(errno)); + return -1; + } + + /* already created by someone else, simply get it. + * Note there's still a small window of racing between create + * and initialize, a flaw in semaphore semantics */ + mutex->sem_id = semget(mutex->sem_key, 0, 0700); + if (mutex->sem_id == -1) { + if (errno == ENOENT) { + logmsg(LL_WARN, "sem %x just disappeared " + "under us, try again\n", mutex->sem_key); + return 1; + } + + logmsg(LL_ERR, "get sem %x: %s\n", mutex->sem_key, + strerror(errno)); + return -1; + } + + logmsg(LL_TRACE, "got sem %x id %d for %s\n", + mutex->sem_key, mutex->sem_id, mutex->sem_name); + } + + return 0; } int lgss_mutex_lock(lgss_mutex_id_t mid) { - struct lgss_mutex_s *sem = &lgss_mutexes[mid]; - struct sembuf sembuf; - int rc; - - lassert(mid < LGSS_MUTEX_MAX); - - logmsg(LL_TRACE, "locking mutex %x for %s\n", - sem->sem_key, sem->sem_name); - - do { - rc = lgss_mutex_get(sem); - if (rc < 0) - return rc; - } while (rc); - - sembuf.sem_num = 0; - sembuf.sem_op = -1; - sembuf.sem_flg = SEM_UNDO; - - if (semop(sem->sem_id, &sembuf, 1) != 0) { - logmsg(LL_ERR, "lock mutex %x: %s\n", sem->sem_key, - strerror(errno)); - return -1; - } - - logmsg(LL_DEBUG, "locked mutex %x for %s\n", - sem->sem_key, sem->sem_name); - return 0; + struct lgss_mutex_s *sem = &lgss_mutexes[mid]; + struct sembuf sembuf; + int rc; + + lassert(mid < LGSS_MUTEX_MAX); + + logmsg(LL_TRACE, "locking mutex %x for %s\n", + sem->sem_key, sem->sem_name); + + do { + rc = lgss_mutex_get(sem); + if (rc < 0) + return rc; + } while (rc); + + sembuf.sem_num = 0; + sembuf.sem_op = -1; + sembuf.sem_flg = SEM_UNDO; + + if (semop(sem->sem_id, &sembuf, 1) != 0) { + logmsg(LL_ERR, "lock mutex %x: %s\n", sem->sem_key, + strerror(errno)); + return -1; + } + + logmsg(LL_DEBUG, "locked mutex %x for %s\n", + sem->sem_key, sem->sem_name); + return 0; } int lgss_mutex_unlock(lgss_mutex_id_t mid) { - struct lgss_mutex_s *sem = &lgss_mutexes[mid]; - struct sembuf sembuf; + struct lgss_mutex_s *sem = &lgss_mutexes[mid]; + struct sembuf sembuf; - lassert(mid < LGSS_MUTEX_MAX); - lassert(sem->sem_id >= 0); + lassert(mid < LGSS_MUTEX_MAX); + lassert(sem->sem_id >= 0); - logmsg(LL_TRACE, "unlocking mutex %x for %s\n", - sem->sem_key, sem->sem_name); + logmsg(LL_TRACE, "unlocking mutex %x for %s\n", + sem->sem_key, sem->sem_name); - sembuf.sem_num = 0; - sembuf.sem_op = 1; - sembuf.sem_flg = SEM_UNDO; + sembuf.sem_num = 0; + sembuf.sem_op = 1; + sembuf.sem_flg = SEM_UNDO; - if (semop(sem->sem_id, &sembuf, 1) != 0) { - logmsg(LL_ERR, "unlock mutex %x: %s\n", sem->sem_key, - strerror(errno)); - return -1; - } + if (semop(sem->sem_id, &sembuf, 1) != 0) { + logmsg(LL_ERR, "unlock mutex %x: %s\n", sem->sem_key, + strerror(errno)); + return -1; + } - logmsg(LL_DEBUG, "unlocked mutex %x for %s\n", - sem->sem_key, sem->sem_name); - return 0; + logmsg(LL_DEBUG, "unlocked mutex %x for %s\n", + sem->sem_key, sem->sem_name); + return 0; } /**************************************** @@ -251,87 +252,87 @@ gss_OID_desc skoid = { loglevel_t g_log_level = LL_WARN; static const char *log_prefix[] = { - [LL_ERR] = "ERROR", - [LL_WARN] = "WARNING", - [LL_INFO] = "INFO", - [LL_DEBUG] = "DEBUG", - [LL_TRACE] = "TRACE", + [LL_ERR] = "ERROR", + [LL_WARN] = "WARNING", + [LL_INFO] = "INFO", + [LL_DEBUG] = "DEBUG", + [LL_TRACE] = "TRACE", }; void lgss_set_loglevel(loglevel_t level) { - lassert(level < LL_MAX); - g_log_level = level; + lassert(level < LL_MAX); + g_log_level = level; } void __logmsg(loglevel_t level, const char *func, const char *format, ...) { - va_list ap; - int offset; - char buf[1024]; + va_list ap; + int offset; + char buf[1024]; - offset = snprintf(buf, sizeof(buf), "[%d]:%s:%s(): ", - getpid(), log_prefix[level], func); + offset = scnprintf(buf, sizeof(buf), "[%d]:%s:%s(): ", + getpid(), log_prefix[level], func); - va_start(ap, format); - vsnprintf(buf + offset, sizeof(buf) - offset, format, ap); - va_end(ap); + va_start(ap, format); + vsnprintf(buf + offset, sizeof(buf) - offset, format, ap); + va_end(ap); - syslog(LOG_INFO, "%s", buf); + syslog(LOG_INFO, "%s", buf); } void __logmsg_gss(loglevel_t level, const char *func, const gss_OID mech, uint32_t major, uint32_t minor, const char *format, ...) { - va_list ap; - u_int32_t maj_stat1, min_stat1; - u_int32_t maj_stat2, min_stat2; - gss_buffer_desc maj_gss_buf = GSS_C_EMPTY_BUFFER; - gss_buffer_desc min_gss_buf = GSS_C_EMPTY_BUFFER; - char buf[1024]; - char maj_buf[30], min_buf[30]; - char *maj_msg, *min_msg; - int offset; - uint32_t msg_ctx = 0; - - /* Get major status message */ - maj_stat1 = gss_display_status(&min_stat1, major, GSS_C_GSS_CODE, - mech, &msg_ctx, &maj_gss_buf); - if (maj_stat1 != GSS_S_COMPLETE) { - snprintf(maj_buf, sizeof(maj_buf), "(0x%08x)", major); - maj_msg = &maj_buf[0]; - } else { - maj_msg = maj_gss_buf.value; - } - - /* Get minor status message */ - maj_stat2 = gss_display_status(&min_stat2, minor, GSS_C_MECH_CODE, - mech, &msg_ctx, &min_gss_buf); - if (maj_stat2 != GSS_S_COMPLETE) { - snprintf(min_buf, sizeof(min_buf), "(0x%08x)", minor); - min_msg = &min_buf[0]; - } else { - min_msg = min_gss_buf.value; - } - - /* arrange & log message */ - offset = snprintf(buf, sizeof(buf), "[%d]:%s:%s(): ", - getpid(), log_prefix[level], func); - - va_start(ap, format); - offset += vsnprintf(buf + offset, sizeof(buf) - offset, format, ap); - va_end(ap); - - snprintf(buf + offset, sizeof(buf) - offset, ": GSSAPI: %s - %s\n", - maj_msg, min_msg); - - syslog(LOG_INFO, "%s", buf); - - /* release buffers */ - if (maj_gss_buf.length != 0) - gss_release_buffer(&min_stat1, &maj_gss_buf); - if (min_gss_buf.length != 0) - gss_release_buffer(&min_stat2, &min_gss_buf); + va_list ap; + uint32_t maj_stat1, min_stat1; + uint32_t maj_stat2, min_stat2; + gss_buffer_desc maj_gss_buf = GSS_C_EMPTY_BUFFER; + gss_buffer_desc min_gss_buf = GSS_C_EMPTY_BUFFER; + char buf[1024]; + char maj_buf[30], min_buf[30]; + char *maj_msg, *min_msg; + int offset; + uint32_t msg_ctx = 0; + + /* Get major status message */ + maj_stat1 = gss_display_status(&min_stat1, major, GSS_C_GSS_CODE, + mech, &msg_ctx, &maj_gss_buf); + if (maj_stat1 != GSS_S_COMPLETE) { + snprintf(maj_buf, sizeof(maj_buf), "(0x%08x)", major); + maj_msg = &maj_buf[0]; + } else { + maj_msg = maj_gss_buf.value; + } + + /* Get minor status message */ + maj_stat2 = gss_display_status(&min_stat2, minor, GSS_C_MECH_CODE, + mech, &msg_ctx, &min_gss_buf); + if (maj_stat2 != GSS_S_COMPLETE) { + snprintf(min_buf, sizeof(min_buf), "(0x%08x)", minor); + min_msg = &min_buf[0]; + } else { + min_msg = min_gss_buf.value; + } + + /* arrange & log message */ + offset = scnprintf(buf, sizeof(buf), "[%d]:%s:%s(): ", + getpid(), log_prefix[level], func); + + va_start(ap, format); + offset += vscnprintf(buf + offset, sizeof(buf) - offset, format, ap); + va_end(ap); + + snprintf(buf + offset, sizeof(buf) - offset, ": GSSAPI: %s - %s\n", + maj_msg, min_msg); + + syslog(LOG_INFO, "%s", buf); + + /* release buffers */ + if (maj_gss_buf.length != 0) + gss_release_buffer(&min_stat1, &maj_gss_buf); + if (min_gss_buf.length != 0) + gss_release_buffer(&min_stat2, &min_gss_buf); } /**************************************** @@ -353,83 +354,83 @@ struct lgss_mech_type *lgss_name2mech(const char *mech_name) int lgss_mech_initialize(struct lgss_mech_type *mech) { - logmsg(LL_TRACE, "initialize mech %s\n", mech->lmt_name); - if (mech->lmt_init) - return mech->lmt_init(); - return 0; + logmsg(LL_TRACE, "initialize mech %s\n", mech->lmt_name); + if (mech->lmt_init) + return mech->lmt_init(); + return 0; } void lgss_mech_finalize(struct lgss_mech_type *mech) { - logmsg(LL_TRACE, "finalize mech %s\n", mech->lmt_name); - if (mech->lmt_fini) - mech->lmt_fini(); + logmsg(LL_TRACE, "finalize mech %s\n", mech->lmt_name); + if (mech->lmt_fini) + mech->lmt_fini(); } struct lgss_cred * lgss_create_cred(struct lgss_mech_type *mech) { - struct lgss_cred *cred; + struct lgss_cred *cred; - cred = malloc(sizeof(*cred)); - if (cred) { - memset(cred, 0, sizeof(*cred)); - cred->lc_mech = mech; - } + cred = malloc(sizeof(*cred)); + if (cred) { + memset(cred, 0, sizeof(*cred)); + cred->lc_mech = mech; + } - logmsg(LL_TRACE, "create a %s cred at %p\n", mech->lmt_name, cred); - return cred; + logmsg(LL_TRACE, "create a %s cred at %p\n", mech->lmt_name, cred); + return cred; } void lgss_destroy_cred(struct lgss_cred *cred) { lassert(cred->lc_mech != NULL); - lassert(cred->lc_mech_cred == NULL); + lassert(cred->lc_mech_cred == NULL); - logmsg(LL_TRACE, "destroying a %s cred at %p\n", - cred->lc_mech->lmt_name, cred); - free(cred); + logmsg(LL_TRACE, "destroying a %s cred at %p\n", + cred->lc_mech->lmt_name, cred); + free(cred); } int lgss_prepare_cred(struct lgss_cred *cred) { - struct lgss_mech_type *mech = cred->lc_mech; + struct lgss_mech_type *mech = cred->lc_mech; lassert(mech != NULL); - logmsg(LL_TRACE, "preparing %s cred %p\n", mech->lmt_name, cred); + logmsg(LL_TRACE, "preparing %s cred %p\n", mech->lmt_name, cred); - if (mech->lmt_prepare_cred) - return mech->lmt_prepare_cred(cred); - return 0; + if (mech->lmt_prepare_cred) + return mech->lmt_prepare_cred(cred); + return 0; } void lgss_release_cred(struct lgss_cred *cred) { - struct lgss_mech_type *mech = cred->lc_mech; + struct lgss_mech_type *mech = cred->lc_mech; lassert(mech != NULL); - logmsg(LL_TRACE, "releasing %s cred %p\n", mech->lmt_name, cred); + logmsg(LL_TRACE, "releasing %s cred %p\n", mech->lmt_name, cred); - if (cred->lc_mech_cred) { - lassert(cred->lc_mech != NULL); + if (cred->lc_mech_cred) { + lassert(cred->lc_mech != NULL); lassert(cred->lc_mech->lmt_release_cred != NULL); - cred->lc_mech->lmt_release_cred(cred); - } + cred->lc_mech->lmt_release_cred(cred); + } } int lgss_using_cred(struct lgss_cred *cred) { - struct lgss_mech_type *mech = cred->lc_mech; + struct lgss_mech_type *mech = cred->lc_mech; lassert(mech != NULL); - logmsg(LL_TRACE, "using %s cred %p\n", mech->lmt_name, cred); + logmsg(LL_TRACE, "using %s cred %p\n", mech->lmt_name, cred); - if (mech->lmt_using_cred) - return mech->lmt_using_cred(cred); - return 0; + if (mech->lmt_using_cred) + return mech->lmt_using_cred(cred); + return 0; } int lgss_validate_cred(struct lgss_cred *cred, gss_buffer_desc *token, @@ -454,35 +455,34 @@ int lgss_validate_cred(struct lgss_cred *cred, gss_buffer_desc *token, int lgss_get_service_str(char **string, uint32_t lsvc, uint64_t tgt_nid) { - const int max_namelen = 512; - char namebuf[max_namelen]; - int alloc_size; + const int max_namelen = 512; + char namebuf[max_namelen]; + int alloc_size; - lassert(*string == NULL); + lassert(*string == NULL); - if (lsvc >= LGSS_SVC_MAX) { - logmsg(LL_ERR, "invalid lgss service %d\n", lsvc); - return -1; - } + if (lsvc >= LGSS_SVC_MAX) { + logmsg(LL_ERR, "invalid lgss service %d\n", lsvc); + return -1; + } if (lnet_nid2hostname(tgt_nid, namebuf, max_namelen)) { logmsg(LL_ERR, "cannot resolve hostname from nid %"PRIx64"\n", tgt_nid); - return -1; - } + return -1; + } - alloc_size = 32 + strlen(namebuf); + alloc_size = 32 + strlen(namebuf); - *string = malloc(alloc_size); - if (*string == NULL) { - logmsg(LL_ERR, "can't malloc %d bytes\n", alloc_size); - return 1; - } + *string = malloc(alloc_size); + if (*string == NULL) { + logmsg(LL_ERR, "can't malloc %d bytes\n", alloc_size); + return 1; + } - snprintf(*string, alloc_size, "%s@%s", - lgss_svc_str[lsvc], namebuf); + snprintf(*string, alloc_size, "%s@%s", + lgss_svc_str[lsvc], namebuf); - logmsg(LL_DEBUG, "constructed service string: %s\n", *string); - return 0; + logmsg(LL_DEBUG, "constructed service string: %s\n", *string); + return 0; } - diff --git a/lustre/utils/gss/sk_utils.c b/lustre/utils/gss/sk_utils.c index f20a4ab..a5c09df 100644 --- a/lustre/utils/gss/sk_utils.c +++ b/lustre/utils/gss/sk_utils.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "sk_utils.h" #include "write_bytes.h" @@ -1328,7 +1329,7 @@ int sk_encode_netstring(gss_buffer_desc *bufs, int numbufs, ptr = ns->value; for (i = 0; i < numbufs; i++) { /* size */ - rc = snprintf((char *) ptr, size, "%zu:", bufs[i].length); + rc = scnprintf((char *) ptr, size, "%zu:", bufs[i].length); ptr += rc; /* contents */ diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index dc1f0e6..2edb399 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -71,6 +71,7 @@ #include #include #include +#include #include #include #include @@ -6810,22 +6811,23 @@ quota_type_def: */ static char * __sec2str(time_t seconds, char *buf) { - const char spec[] = "smhdw"; - const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60}; - unsigned long c; - char *tail = buf; - int i; + const char spec[] = "smhdw"; + const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60}; + unsigned long c; + char *tail = buf; + int i; - for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) { - c = seconds / mult[i]; + for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) { + c = seconds / mult[i]; - if (c > 0 || (i == 0 && buf == tail)) - tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]); + if (c > 0 || (i == 0 && buf == tail)) + tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c, + spec[i]); - seconds %= mult[i]; - } + seconds %= mult[i]; + } - return tail; + return tail; } static void sec2str(time_t seconds, char *buf, int rc) diff --git a/lustre/utils/lsnapshot.c b/lustre/utils/lsnapshot.c index 5de39b2..8ad6acc 100644 --- a/lustre/utils/lsnapshot.c +++ b/lustre/utils/lsnapshot.c @@ -44,6 +44,7 @@ #include #include +#include #include #include @@ -1186,8 +1187,8 @@ static int snapshot_inherit_prop(struct snapshot_instance *si, if (end) *end = '\0'; - rc = snprintf(cmd + len, size - len - 1, - "-o %s=\"%s\" ", buf, ptr); + rc = scnprintf(cmd + len, size - len - 1, + "-o %s=\"%s\" ", buf, ptr); if (rc <= 0) return -EOVERFLOW; @@ -1229,21 +1230,21 @@ static int __snapshot_create(struct snapshot_instance *si, int len; memset(cmd, 0, sizeof(cmd)); - len = snprintf(cmd, sizeof(cmd) - 1, - DRSH" '"DZFS" snapshot " - "-o lustre:fsname=%s " - "-o lustre:magic=%s " - "-o lustre:ctime=%llu " - "-o lustre:mtime=%llu ", - PRSH(si, st), PZFS(st), fsname, - SNAPSHOT_MAGIC, xtime, xtime); + len = scnprintf(cmd, sizeof(cmd) - 1, + DRSH" '"DZFS" snapshot " + "-o lustre:fsname=%s " + "-o lustre:magic=%s " + "-o lustre:ctime=%llu " + "-o lustre:mtime=%llu ", + PRSH(si, st), PZFS(st), fsname, + SNAPSHOT_MAGIC, xtime, xtime); if (len <= 0) exit(-EOVERFLOW); if (si->si_comment) { - rc = snprintf(cmd + len, sizeof(cmd) - len - 1, - "-o lustre:comment=\"%s\" ", - si->si_comment); + rc = scnprintf(cmd + len, sizeof(cmd) - len - 1, + "-o lustre:comment=\"%s\" ", + si->si_comment); if (rc <= 0) exit(-EOVERFLOW); diff --git a/lustre/utils/mount_lustre.c b/lustre/utils/mount_lustre.c index beb42a8..081f0f2 100644 --- a/lustre/utils/mount_lustre.c +++ b/lustre/utils/mount_lustre.c @@ -138,38 +138,38 @@ void usage(FILE *out) #define MAXNIDSTR 1024 static char *convert_hostnames(char *s1) { - char *converted, *s2 = 0, *c; - char sep; - int left = MAXNIDSTR; - lnet_nid_t nid; - - converted = malloc(left); - if (converted == NULL) { - fprintf(stderr, "out of memory: needed %d bytes\n", - MAXNIDSTR); - return NULL; - } - c = converted; - while ((left > 0) && (*s1 != '/')) { - s2 = strpbrk(s1, ",:"); - if (!s2) - goto out_free; - sep = *s2; - *s2 = '\0'; - nid = libcfs_str2nid(s1); - *s2 = sep; /* back to original string */ - if (nid == LNET_NID_ANY) - goto out_free; - c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep); - left = converted + MAXNIDSTR - c; - s1 = s2 + 1; - } - snprintf(c, left, "%s", s1); - return converted; + char *converted, *s2 = 0, *c; + char sep; + int left = MAXNIDSTR; + lnet_nid_t nid; + + converted = malloc(left); + if (converted == NULL) { + fprintf(stderr, "out of memory: needed %d bytes\n", + MAXNIDSTR); + return NULL; + } + c = converted; + while ((left > 0) && (*s1 != '/')) { + s2 = strpbrk(s1, ",:"); + if (!s2) + goto out_free; + sep = *s2; + *s2 = '\0'; + nid = libcfs_str2nid(s1); + *s2 = sep; /* back to original string */ + if (nid == LNET_NID_ANY) + goto out_free; + c += scnprintf(c, left, "%s%c", libcfs_nid2str(nid), sep); + left = converted + MAXNIDSTR - c; + s1 = s2 + 1; + } + snprintf(c, left, "%s", s1); + return converted; out_free: - fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1); - free(converted); - return NULL; + fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1); + free(converted); + return NULL; } /***************************************************************************** -- 1.8.3.1