@@ -253,8 +253,7 @@ find_key (const char * str)
* ESRCH: command not found
* EINVAL: argument missing for command
*/
-static int
-get_cmdvec (char * cmd, vector *v)
+int get_cmdvec (char *cmd, vector *v)
{
int i;
int r = 0;
@@ -319,7 +318,7 @@ out:
}
static uint64_t
-fingerprint(vector vec)
+fingerprint(const struct _vector *vec)
{
int i;
uint64_t fp = 0;
@@ -334,6 +333,11 @@ fingerprint(vector vec)
return fp;
}
+struct handler *find_handler_for_cmdvec(const struct _vector *v)
+{
+ return find_handler(fingerprint(v));
+}
+
int
alloc_handlers (void)
{
@@ -412,8 +416,7 @@ do_genhelp(struct strbuf *reply, const char *cmd, int error) {
}
-static char *
-genhelp_handler (const char *cmd, int error)
+char *genhelp_handler(const char *cmd, int error)
{
STRBUF_ON_STACK(reply);
@@ -422,67 +425,6 @@ genhelp_handler (const char *cmd, int error)
return steal_strbuf_str(&reply);
}
-int
-parse_cmd (char * cmd, char ** reply, int * len, void * data, int timeout )
-{
- int r;
- struct handler * h;
- vector cmdvec = NULL;
- struct timespec tmo;
-
- r = get_cmdvec(cmd, &cmdvec);
-
- if (r) {
- *reply = genhelp_handler(cmd, r);
- if (*reply == NULL)
- return EINVAL;
- *len = strlen(*reply) + 1;
- return 0;
- }
-
- h = find_handler(fingerprint(cmdvec));
-
- if (!h || !h->fn) {
- free_keys(cmdvec);
- *reply = genhelp_handler(cmd, EINVAL);
- if (*reply == NULL)
- return EINVAL;
- *len = strlen(*reply) + 1;
- return 0;
- }
-
- /*
- * execute handler
- */
- if (clock_gettime(CLOCK_REALTIME, &tmo) == 0) {
- tmo.tv_sec += timeout;
- } else {
- tmo.tv_sec = 0;
- }
- if (h->locked) {
- int locked = 0;
- struct vectors * vecs = (struct vectors *)data;
-
- pthread_cleanup_push(cleanup_lock, &vecs->lock);
- if (tmo.tv_sec) {
- r = timedlock(&vecs->lock, &tmo);
- } else {
- lock(&vecs->lock);
- r = 0;
- }
- if (r == 0) {
- locked = 1;
- pthread_testcancel();
- r = h->fn(cmdvec, reply, len, data);
- }
- pthread_cleanup_pop(locked);
- } else
- r = h->fn(cmdvec, reply, len, data);
- free_keys(cmdvec);
-
- return r;
-}
-
char *
get_keyparam (vector v, uint64_t code)
{
@@ -137,7 +137,10 @@ int __set_handler_callback (uint64_t fp, cli_handler *fn, bool locked);
#define set_handler_callback(fp, fn) __set_handler_callback(fp, fn, true)
#define set_unlocked_handler_callback(fp, fn) __set_handler_callback(fp, fn, false)
-int parse_cmd (char * cmd, char ** reply, int * len, void *, int);
+int get_cmdvec (char *cmd, vector *v);
+struct handler *find_handler_for_cmdvec(const struct _vector *v);
+char *genhelp_handler (const char *cmd, int error);
+
int load_keys (void);
char * get_keyparam (vector v, uint64_t code);
void free_keys (vector vec);
@@ -311,6 +311,67 @@ static void handle_inotify(int fd, struct watch_descriptors *wds)
condlog(1, "Multipath configuration updated.\nReload multipathd for changes to take effect");
}
+static int parse_cmd (char *cmd, char **reply, int *len, void *data,
+ int timeout)
+{
+ int r;
+ struct handler * h;
+ vector cmdvec = NULL;
+ struct timespec tmo;
+
+ r = get_cmdvec(cmd, &cmdvec);
+
+ if (r) {
+ *reply = genhelp_handler(cmd, r);
+ if (*reply == NULL)
+ return EINVAL;
+ *len = strlen(*reply) + 1;
+ return 0;
+ }
+
+ h = find_handler_for_cmdvec(cmdvec);
+
+ if (!h || !h->fn) {
+ free_keys(cmdvec);
+ *reply = genhelp_handler(cmd, EINVAL);
+ if (*reply == NULL)
+ return EINVAL;
+ *len = strlen(*reply) + 1;
+ return 0;
+ }
+
+ /*
+ * execute handler
+ */
+ if (clock_gettime(CLOCK_REALTIME, &tmo) == 0) {
+ tmo.tv_sec += timeout;
+ } else {
+ tmo.tv_sec = 0;
+ }
+ if (h->locked) {
+ int locked = 0;
+ struct vectors * vecs = (struct vectors *)data;
+
+ pthread_cleanup_push(cleanup_lock, &vecs->lock);
+ if (tmo.tv_sec) {
+ r = timedlock(&vecs->lock, &tmo);
+ } else {
+ lock(&vecs->lock);
+ r = 0;
+ }
+ if (r == 0) {
+ locked = 1;
+ pthread_testcancel();
+ r = h->fn(cmdvec, reply, len, data);
+ }
+ pthread_cleanup_pop(locked);
+ } else
+ r = h->fn(cmdvec, reply, len, data);
+ free_keys(cmdvec);
+
+ return r;
+}
+
static int uxsock_trigger(char *str, char **reply, int *len,
bool is_root, void *trigger_data)
{