#define __LIBCFS_UTIL_STRING_H__
#include <stddef.h>
+#include <stdarg.h>
#include <linux/types.h>
#include <libcfs/util/list.h>
+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.
*/
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;
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);
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,
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;
}
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");
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,
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);
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);
}
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);
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);
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++;
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 */
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)
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);
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;
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);
}
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;
goto out;
for (i = 0;; i++) {
- pos = str_buf;
__u32 rc_net;
memset(buf, 0, buf_size);
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",
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;
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;
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) {
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;
}
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;
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
+#include <libcfs/util/string.h>
#define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
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;
{
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)
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,
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <libcfs/util/string.h>
#include "err_util.h"
void qword_add(char **bpp, int *lp, const char *str)
{
int len;
- len = snprintf(*bpp, *lp, "%d ", n);
- if (len > *lp)
- len = *lp;
+ len = scnprintf(*bpp, *lp, "%d ", n);
*bpp += len;
*lp -= len;
}
{
int len;
- len = snprintf(*bpp, *lp, "%u ", n);
- if (len > *lp)
- len = *lp;
+ len = scnprintf(*bpp, *lp, "%u ", n);
*bpp += len;
*lp -= len;
}
#ifdef HAVE_COM_ERR_H
#include <com_err.h>
#endif
+#include <libcfs/util/string.h>
#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,
};
/****************************************
****************************************/
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;
}
/****************************************
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);
}
/****************************************
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,
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;
}
-
#include <openssl/hmac.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <libcfs/util/string.h>
#include "sk_utils.h"
#include "write_bytes.h"
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 */
#include <libcfs/util/string.h>
#include <libcfs/util/ioctl.h>
#include <libcfs/util/parser.h>
+#include <libcfs/util/string.h>
#include <lustre/lustreapi.h>
#include <linux/lustre/lustre_ver.h>
#include <linux/lustre/lustre_param.h>
*/
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)
#include <libcfs/util/list.h>
#include <libcfs/util/ioctl.h>
+#include <libcfs/util/string.h>
#include <linux/lustre/lustre_ioctl.h>
#include <linux/lustre/lustre_barrier_user.h>
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;
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);
#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;
}
/*****************************************************************************