/** * Helper function to run over the sabdb list and perform merocmd for * the value and reporting status on the performed command. Either a * message is printed when success, or when premsg is not NULL, premsg * is printed before the action, and "done" printed afterwards. */ static void simple_argv_cmd(char *cmd, sabdb *dbs, char *merocmd, char *successmsg, char *premsg) { int state = 0; /* return status */ int hadwork = 0; /* if we actually did something */ char *ret; char *out; /* do for each listed database */ for (; dbs != NULL; dbs = dbs->next) { if (premsg != NULL && !monetdb_quiet) { printf("%s '%s'... ", premsg, dbs->dbname); fflush(stdout); } ret = control_send(&out, mero_host, mero_port, dbs->dbname, merocmd, 0, mero_pass); if (ret != NULL) { if (premsg != NULL && !monetdb_quiet) printf("FAILED\n"); fprintf(stderr, "%s: %s\n", cmd, ret); free(ret); exit(2); } if (strcmp(out, "OK") == 0) { if (!monetdb_quiet) { if (premsg != NULL) { printf("done\n"); } else { printf("%s: %s\n", successmsg, dbs->dbname); } } } else { if (premsg != NULL && !monetdb_quiet) printf("FAILED\n"); fprintf(stderr, "%s: %s\n", cmd, out); free(out); state |= 1; } hadwork = 1; } if (hadwork == 0) { char *argv[2] = { "monetdb", cmd }; command_help(2, argv); exit(1); } if (state != 0) exit(state); }
int uwbrdev_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long arg) { struct net_adapter *adapter; struct process_descriptor *process; int ret = 0; adapter = (struct net_adapter *)(file->private_data); if ((adapter == NULL) || adapter->halted) { dump_debug("can't find adapter or Device Removed"); return -ENODEV; } switch (cmd) { case CONTROL_IOCTL_WRITE_REQUEST: { struct eth_header *ctlhdr; memset(&g_tx_buffer, 0x0, sizeof(struct control_tx_buffer)); if ((char *)arg == NULL) { dump_debug("arg == NULL: return -EFAULT"); return -EFAULT; } g_tx_buffer.length = ((struct control_tx_buffer *)arg)->length; if (g_tx_buffer.length < WIMAX_MAX_TOTAL_SIZE) { if (copy_from_user(g_tx_buffer.data, (void *)(arg+sizeof(int)), g_tx_buffer.length)) return -EFAULT; } else return -EFBIG; spin_lock(&adapter->ctl.apps.lock); process = process_by_id(adapter, current->tgid); if (process == NULL) { dump_debug("process %d not found",\ current->tgid); ret = -EFAULT; spin_unlock(&adapter->ctl.apps.lock); break; } ctlhdr = (struct eth_header *)g_tx_buffer.data; process->type = ctlhdr->type; spin_unlock(&adapter->ctl.apps.lock); control_send(adapter, g_tx_buffer.data, g_tx_buffer.length); break; } default: dump_debug("uwbrdev_ioctl: " "unknown ioctl cmd: 0x%x", cmd); break; } /* switch (cmd) */ return ret; }
/** * Returns if the merovingian server at host, port using pass is alive * or not. If alive, 0 is returned. The same rules for host, port and * pass hold as for control_send regarding the use of a UNIX vs TCP * socket. */ char control_ping(char *host, int port, char *pass) { char *res; char *err; if ((err = control_send(&res, host, port, "", "ping", 0, pass)) == NULL) { if (res != NULL) free(res); return(0); } free(err); return(1); }
static void command_discover(int argc, char *argv[]) { char path[8096]; char *buf; char *p, *q; size_t twidth = TERMWIDTH; char *location = NULL; char *match = NULL; size_t numlocs = 50; size_t posloc = 0; size_t loclen = 0; char **locations = malloc(sizeof(char*) * numlocs); if (argc == 0) { exit(2); } else if (argc > 2) { /* print help message for this command */ command_help(2, &argv[-1]); exit(1); } else if (argc == 2) { match = argv[1]; } /* Send the pass phrase to unlock the information available in * merovingian. Anelosimus eximius is a social species of spiders, * which help each other, just like merovingians do among each * other. */ p = control_send(&buf, mero_host, mero_port, "anelosimus", "eximius", 1, mero_pass); if (p != NULL) { printf("%s: %s\n", argv[0], p); free(p); exit(2); } if ((p = strtok(buf, "\n")) != NULL) { if (strcmp(p, "OK") != 0) { fprintf(stderr, "%s: %s\n", argv[0], p); exit(1); } if (twidth > 0) location = malloc(twidth + 1); while ((p = strtok(NULL, "\n")) != NULL) { if ((q = strchr(p, '\t')) == NULL) { /* doesn't look correct */ printf("%s: WARNING: discarding incorrect line: %s\n", argv[0], p); continue; } *q++ = '\0'; snprintf(path, sizeof(path), "%s%s", q, p); if (match == NULL || glob(match, path)) { if (twidth > 0) { /* cut too long location name */ abbreviateString(location, path, twidth); } else { location = path; } /* store what we found */ if (posloc == numlocs) locations = realloc(locations, sizeof(char *) * (numlocs = numlocs * 2)); locations[posloc++] = strdup(location); if (strlen(location) > loclen) loclen = strlen(location); } } if (twidth > 0) free(location); } free(buf); if (posloc > 0) { printf("%*slocation\n", (int)(loclen - 8 /* "location" */ - ((loclen - 8) / 2)), ""); qsort(locations, posloc, sizeof(char *), cmpurl); for (loclen = 0; loclen < posloc; loclen++) { printf("%s\n", locations[loclen]); free(locations[loclen]); } } free(locations); }
/** * Helper function to perform the equivalent of * msab_getStatus(&stats, x) but over the network. */ static char * MEROgetStatus(sabdb **ret, char *database) { sabdb *orig; sabdb *stats; sabdb *w = NULL; size_t swlen = 50; size_t swpos = 0; sabdb **sw; char *p; char *buf; char *e; if (database == NULL) database = "#all"; e = control_send(&buf, mero_host, mero_port, database, "status", 1, mero_pass); if (e != NULL) return(e); sw = malloc(sizeof(sabdb *) * swlen); orig = NULL; if ((p = strtok(buf, "\n")) != NULL) { if (strcmp(p, "OK") != 0) { p = strdup(p); free(buf); free(sw); return(p); } for (swpos = 0; (p = strtok(NULL, "\n")) != NULL; swpos++) { e = msab_deserialise(&stats, p); if (e != NULL) { printf("WARNING: failed to parse response from " "monetdbd: %s\n", e); free(e); swpos--; continue; } if (swpos == swlen) sw = realloc(sw, sizeof(sabdb *) * (swlen = swlen * 2)); sw[swpos] = stats; } } free(buf); if (swpos > 1) { qsort(sw, swpos, sizeof(sabdb *), cmpsabdb); orig = w = sw[0]; for (swlen = 1; swlen < swpos; swlen++) w = w->next = sw[swlen]; } else if (swpos == 1) { orig = sw[0]; orig->next = NULL; } free(sw); *ret = orig; return(NULL); }
static void command_destroy(int argc, char *argv[]) { int i; int force = 0; /* ask for confirmation */ char *e; sabdb *orig = NULL; sabdb *stats = NULL; if (argc == 1) { /* print help message for this command */ command_help(argc + 1, &argv[-1]); exit(1); } /* walk through the arguments and hunt for "options" */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--") == 0) { argv[i] = NULL; break; } if (argv[i][0] == '-') { if (argv[i][1] == 'f') { force = 1; argv[i] = NULL; } else { fprintf(stderr, "destroy: unknown option: %s\n", argv[i]); command_help(argc + 1, &argv[-1]); exit(1); } } } if ((e = MEROgetStatus(&orig, NULL)) != NULL) { fprintf(stderr, "destroy: %s\n", e); free(e); exit(2); } stats = globMatchDBS(argc, argv, &orig, "destroy"); msab_freeStatus(&orig); orig = stats; if (orig == NULL) exit(1); if (force == 0) { char answ; printf("you are about to remove database%s ", orig->next != NULL ? "s" : ""); for (stats = orig; stats != NULL; stats = stats->next) printf("%s'%s'", stats != orig ? ", " : "", stats->dbname); printf("\nALL data in %s will be lost, are you sure? [y/N] ", orig->next != NULL ? "these databases" : "this database"); if (scanf("%c", &answ) >= 1 && (answ == 'y' || answ == 'Y')) { /* do it! */ } else { printf("aborted\n"); exit(1); } } else { char *ret; char *out; for (stats = orig; stats != NULL; stats = stats->next) { if (stats->state == SABdbRunning || stats->state == SABdbStarting) { ret = control_send(&out, mero_host, mero_port, stats->dbname, "stop", 0, mero_pass); if (ret != NULL) free(ret); } } } simple_argv_cmd(argv[0], orig, "destroy", "destroyed database", NULL); msab_freeStatus(&orig); }
static void command_get(int argc, char *argv[]) { char doall = 1; char *p; char *property = NULL; char propall = 0; char vbuf[512]; char *buf = 0; char *e; int i; sabdb *orig, *stats; int twidth = TERMWIDTH; char *source, *value = NULL; confkeyval *kv; confkeyval *defprops = getDefaultProps(); confkeyval *props = getDefaultProps(); if (argc == 1) { /* print help message for this command */ command_help(2, &argv[-1]); exit(1); } else if (argc == 0) { exit(2); } /* time to collect some option flags */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { for (p = argv[i] + 1; *p != '\0'; p++) { switch (*p) { case '-': if (p[1] == '\0') { if (argc - 1 > i) doall = 0; i = argc; break; } default: fprintf(stderr, "get: unknown option: -%c\n", *p); command_help(2, &argv[-1]); exit(1); break; } } /* make this option no longer available, for easy use * lateron */ argv[i] = NULL; } else if (property == NULL) { /* first non-option is property, rest is database */ property = argv[i]; argv[i] = NULL; if (strcmp(property, "all") == 0) propall = 1; } else { doall = 0; } } if (property == NULL) { fprintf(stderr, "get: need a property argument\n"); command_help(2, &argv[-1]); exit(1); } if ((e = MEROgetStatus(&orig, NULL)) != NULL) { fprintf(stderr, "get: %s\n", e); free(e); exit(2); } /* look at the arguments and evaluate them based on a glob (hence we * listed all databases before) */ if (doall != 1) { stats = globMatchDBS(argc, argv, &orig, "get"); msab_freeStatus(&orig); orig = stats; } /* avoid work when there are no results */ if (orig == NULL) { free(props); free(defprops); return; } e = control_send(&buf, mero_host, mero_port, "#defaults", "get", 1, mero_pass); if (e != NULL) { fprintf(stderr, "get: %s\n", e); free(e); exit(2); } else if ( buf && strncmp(buf, "OK\n", 3) != 0) { fprintf(stderr, "get: %s\n", buf); free(buf); exit(1); } readPropsBuf(defprops, buf + 3); if( buf) free(buf); if (twidth > 0) { /* name = 15 */ /* prop = 8 */ /* source = 7 */ twidth -= 15 + 2 + 8 + 2 + 7 + 2; if (twidth < 6) twidth = 6; value = malloc(sizeof(char) * twidth + 1); } stats = orig; while (stats != NULL) { e = control_send(&buf, mero_host, mero_port, stats->dbname, "get", 1, mero_pass); if (e != NULL) { fprintf(stderr, "get: %s\n", e); free(e); exit(2); } else if (strncmp(buf, "OK\n", 3) != 0) { fprintf(stderr, "get: %s\n", buf); free(buf); exit(1); } readPropsBuf(props, buf + 3); free(buf); if (propall == 1) { size_t off = 0; kv = props; off += snprintf(vbuf, sizeof(vbuf), "name"); while (kv->key != NULL) { off += snprintf(vbuf + off, sizeof(vbuf) - off, ",%s", kv->key); kv++; } } else { /* check validity of properties before printing them */ if (stats == orig) { snprintf(vbuf, sizeof(vbuf), "%s", property); buf = vbuf; while ((p = strtok(buf, ",")) != NULL) { buf = NULL; if (strcmp(p, "name") == 0) continue; kv = findConfKey(props, p); if (kv == NULL) fprintf(stderr, "get: no such property: %s\n", p); } } snprintf(vbuf, sizeof(vbuf), "%s", property); } buf = vbuf; /* print header after errors */ if (stats == orig) printf(" name prop source value\n"); while ((p = strtok(buf, ",")) != NULL) { buf = NULL; /* filter properties based on object type */ kv = findConfKey(props, "type"); if (kv != NULL && kv->val != NULL) { if (strcmp(kv->val, "mfunnel") == 0) { if (strcmp(p, "name") != 0 && strcmp(p, "type") != 0 && strcmp(p, "mfunnel") != 0 && strcmp(p, "shared") != 0) continue; } } else { /* no type == database (default) */ if (strcmp(p, "mfunnel") == 0) continue; } /* special virtual case */ if (strcmp(p, "name") == 0) { source = "-"; if (twidth > 0) { abbreviateString(value, stats->dbname, twidth); } else { value = stats->dbname; } } else { kv = findConfKey(props, p); if (kv == NULL) continue; if (kv->val == NULL) { char *y = NULL; kv = findConfKey(defprops, p); source = "default"; y = kv != NULL && kv->val != NULL ? kv->val : "<unknown>"; if (twidth > 0) { abbreviateString(value, y, twidth); } else { value = y; } } else { source = "local"; if (twidth > 0) { abbreviateString(value, kv->val, twidth); } else { value = kv->val; } } } printf("%-15s %-8s %-7s %s\n", stats->dbname, p, source, value); } freeConfFile(props); stats = stats->next; } if (twidth > 0) free(value); msab_freeStatus(&orig); free(props); free(defprops); }
static void command_set(int argc, char *argv[], meroset type) { char *p = NULL; char property[24] = ""; int i; int state = 0; char *res; char *out; sabdb *orig = NULL; sabdb *stats = NULL; char *e; if (argc >= 1 && argc <= 2) { /* print help message for this command */ command_help(2, &argv[-1]); exit(1); } else if (argc == 0) { exit(2); } /* time to collect some option flags */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { for (p = argv[i] + 1; *p != '\0'; p++) { switch (*p) { case '-': if (p[1] == '\0') { i = argc; break; } default: fprintf(stderr, "%s: unknown option: -%c\n", argv[0], *p); command_help(2, &argv[-1]); exit(1); break; } } /* make this option no longer available, for easy use * lateron */ argv[i] = NULL; } else if (property[0] == '\0') { /* first non-option is property, rest is database */ p = argv[i]; if (type == SET) { if ((p = strchr(argv[i], '=')) == NULL) { fprintf(stderr, "set: need property=value\n"); command_help(2, &argv[-1]); exit(1); } *p = '\0'; snprintf(property, sizeof(property), "%s", argv[i]); *p++ = '='; p = argv[i]; } else { snprintf(property, sizeof(property), "%s", argv[i]); } argv[i] = NULL; } } if (property[0] == '\0') { fprintf(stderr, "%s: need a property argument\n", argv[0]); command_help(2, &argv[-1]); exit(1); } if ((e = MEROgetStatus(&orig, NULL)) != NULL) { fprintf(stderr, "%s: %s\n", argv[0], e); free(e); exit(2); } stats = globMatchDBS(argc, argv, &orig, argv[0]); msab_freeStatus(&orig); orig = stats; if (orig == NULL) { /* error already printed by globMatchDBS */ exit(1); } /* handle rename separately due to single argument constraint */ if (strcmp(property, "name") == 0) { if (type == INHERIT) { fprintf(stderr, "inherit: cannot default to a database name\n"); exit(1); } if (orig->next != NULL) { fprintf(stderr, "%s: cannot rename multiple databases to " "the same name\n", argv[0]); exit(1); } out = control_send(&res, mero_host, mero_port, orig->dbname, p, 0, mero_pass); if (out != NULL || strcmp(res, "OK") != 0) { res = out == NULL ? res : out; fprintf(stderr, "%s: %s\n", argv[0], res); state |= 1; } free(res); msab_freeStatus(&orig); exit(state); } for (stats = orig; stats != NULL; stats = stats->next) { if (type == INHERIT) { strncat(property, "=", sizeof(property) - strlen(property) - 1); p = property; } out = control_send(&res, mero_host, mero_port, stats->dbname, p, 0, mero_pass); if (out != NULL || strcmp(res, "OK") != 0) { res = out == NULL ? res : out; fprintf(stderr, "%s: %s\n", argv[0], res); state |= 1; } free(res); } msab_freeStatus(&orig); exit(state); }
static int uwbrdev_ioctl(struct inode *inode, struct file *file, u32 cmd, unsigned long arg) { struct net_adapter *adapter; struct process_descriptor *procdsc; int ret = 0; struct eth_header *ctlhdr; u8 *tx_buffer; int length; adapter = (struct net_adapter *)(file->private_data); if ((adapter == NULL) || adapter->halted) { pr_debug("can't find adapter or Device Removed"); return -ENODEV; } if (cmd != CONTROL_IOCTL_WRITE_REQUEST) { pr_debug("uwbrdev_ioctl: unknown ioctl cmd: 0x%x", cmd); return -EINVAL; } if ((char *)arg == NULL) { pr_debug("arg == NULL: return -EFAULT"); return -EFAULT; } length = ((int *)arg)[0]; if (length >= WIMAX_MAX_TOTAL_SIZE) return -EFBIG; tx_buffer = kmalloc(length, GFP_KERNEL); if (!tx_buffer) { pr_err("%s: not enough memory to allocate tx_buffer\n", __func__); return -ENOMEM; } if (copy_from_user(tx_buffer, (void *)(arg + sizeof(int)), length)) { pr_err("%s: error copying buffer from user space\n", __func__); ret = -EFAULT; goto err_copy; } spin_lock(&adapter->ctl.apps.lock); procdsc = process_by_id(adapter, current->tgid); if (procdsc == NULL) { pr_debug("process %d not found", current->tgid); ret = -EFAULT; goto err_process; } ctlhdr = (struct eth_header *)tx_buffer; procdsc->type = ctlhdr->type; control_send(adapter, tx_buffer, length); err_process: spin_unlock(&adapter->ctl.apps.lock); err_copy: kfree(tx_buffer); return ret; }