static int cli_zmq_method_call(struct teamdctl *tdc, const char *method_name, char **p_reply, void *priv, const char *fmt, va_list ap) { struct cli_zmq_priv *cli_zmq = priv; char *str; char *msg = NULL; char *recv_message = NULL; /* gcc needs this initialized */ char *replystr; int err; dbg(tdc, "zmq: Calling method \"%s\"", method_name); err = myasprintf(&msg, "%s\n%s\n", TEAMD_ZMQ_REQUEST_PREFIX, method_name); if (err) return err; while (*fmt) { switch (*fmt++) { case 's': /* string */ str = va_arg(ap, char *); err = myasprintf(&msg, "%s%s\n", msg, str); if (err) goto free_msg; break; default: err(tdc, "zmq: Unknown argument type requested."); err = -EINVAL; goto free_msg; } } err = cli_zmq_send(tdc, cli_zmq->sock, msg); if (err) goto send_err; err = cli_zmq_recv(tdc, cli_zmq->sock, &recv_message); if (err) goto send_err; err = cli_zmq_process_msg(tdc, recv_message, &replystr); if (err) goto free_recv_message; if (p_reply) { replystr = strdup(replystr); if (!replystr) { err = -ENOMEM; goto free_recv_message; } *p_reply = replystr; } free_recv_message: free(recv_message); goto send_err; free_msg: free(msg); send_err: return err; }
static int do_env_group(char **fields, int nfields, char **err) { char *name; char *list; struct env_group *eg; uint64_t mask; char *item; if (nfields != 2) { myasprintf(err, "number of fields (%d) != 2", nfields); return (-1); } name = fields[0]; list = fields[1]; mask = 0; if (expand_env(list, &mask, &item) < 0) { myasprintf(err, "reference to undefined env %s", item); return (-1); } eg = myzalloc(sizeof (*eg)); eg->eg_name = mystrdup(name); eg->eg_mask = mask; eg->eg_next = env_groups; env_groups = eg; return (0); }
static int do_env(char **fields, int nfields, char **err) { char *name; char *lang; char *defs; if (nfields != 3) { myasprintf(err, "number of fields (%d) != 3", nfields); return (-1); } if (next_env >= MAXENV) { myasprintf(err, "too many environments"); return (-1); } name = fields[0]; lang = fields[1]; defs = fields[2]; compile_env[next_env].ce_name = mystrdup(name); compile_env[next_env].ce_lang = mystrdup(lang); compile_env[next_env].ce_defs = mystrdup(defs); compile_env[next_env].ce_index = next_env; next_env++; return (0); }
static int do_func(char **fields, int nfields, char **err) { char *name; char *rtype; char *atype; char *hdrs; char *envs; struct sym_test *st; if (nfields != 5) { myasprintf(err, "number of fields (%d) != 5", nfields); return (-1); } name = fields[0]; rtype = fields[1]; atype = fields[2]; hdrs = fields[3]; envs = fields[4]; st = myzalloc(sizeof (*st)); st->st_type = SYM_FUNC; st->st_name = mystrdup(name); st->st_rtype = mystrdup(rtype); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0) || (add_arg_types(st, atype, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_value(char **fields, int nfields, char **err) { char *name; char *type; char *hdrs; char *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; type = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_VALUE; st->st_name = mystrdup(name); st->st_rtype = mystrdup(type); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_type(char **fields, int nfields, char **err) { char *decl; char *hdrs; char *envs; struct sym_test *st; if (nfields != 3) { myasprintf(err, "number of fields (%d) != 3", nfields); return (-1); } decl = fields[0]; hdrs = fields[1]; envs = fields[2]; st = myzalloc(sizeof (*st)); st->st_type = SYM_TYPE; st->st_name = mystrdup(decl); st->st_rtype = mystrdup(decl); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int mkworkdir(void) { char b[32]; char *d; cleanup(); (void) strlcpy(b, "/tmp/symbols_testXXXXXX", sizeof (b)); if ((d = mkdtemp(b)) == NULL) { perror("mkdtemp"); return (-1); } dname = mystrdup(d); myasprintf(&cfile, "%s/compile_test.c", d); myasprintf(&ofile, "%s/compile_test.o", d); myasprintf(&lfile, "%s/compile_test.log", d); myasprintf(&efile, "%s/compile_test.exe", d); return (0); }
static int add_envs(struct sym_test *st, char *envs, char **err) { char *item; if (expand_env_list(envs, &st->st_test_mask, &st->st_need_mask, &item) < 0) { myasprintf(err, "bad env action %s", item); return (-1); } return (0); }
static int add_arg_types(struct sym_test *st, char *atype, char **err) { int i = 0; char *a; for (a = strsep(&atype, ";"); a != NULL; a = strsep(&atype, ";")) { if (i >= MAXARG) { myasprintf(err, "too many arguments"); return (-1); } test_trim(&a); st->st_atypes[i++] = mystrdup(a); } return (0); }
static int add_headers(struct sym_test *st, char *hdrs, char **err) { int i = 0; for (char *h = strsep(&hdrs, ";"); h != NULL; h = strsep(&hdrs, ";")) { if (i >= MAXHDR) { myasprintf(err, "too many headers"); return (-1); } test_trim(&h); st->st_hdrs[i++] = mystrdup(h); } return (0); }
static int do_define(char **fields, int nfields, char **err) { char *name, *value, *hdrs, *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; value = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_DEFINE; st->st_name = mystrdup(name); /* * A value to compare against is optional. trim will leave it as a null * pointer if there's nothing there. */ test_trim(&value); if (*value != '\0') st->st_defval = mystrdup(value); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
int do_compile(test_t t, struct sym_test *st, struct compile_env *cenv, int need) { char *cmd; FILE *logf; FILE *dotc; const char *prog, *cflags, *lang; full_count++; if ((dotc = fopen(cfile, "w+")) == NULL) { test_failed(t, "fopen(%s): %s", cfile, strerror(errno)); return (-1); } prog = sym_test_prog(st); if (fwrite(prog, 1, strlen(prog), dotc) < strlen(prog)) { test_failed(t, "fwrite: %s", strerror(errno)); (void) fclose(dotc); return (-1); } if (fclose(dotc) < 0) { test_failed(t, "fclose: %s", strerror(errno)); return (-1); } (void) unlink(ofile); if (strcmp(env_lang(cenv), "c99") == 0) { lang = "c99"; cflags = c99flags; } else if (strcmp(env_lang(cenv), "c11") == 0) { lang = "c11"; cflags = c11flags; } else { lang = "c89"; cflags = c89flags; } if (cflags == NULL) { test_failed(t, "compiler %s does not support %s", compiler, lang); return (-1); } myasprintf(&cmd, "%s %s %s -c %s -o %s >>%s 2>&1", compiler, cflags, env_defs(cenv), cfile, ofile, lfile); if (extra_debug) { test_debugf(t, "command: %s", cmd); } if ((logf = fopen(lfile, "w+")) == NULL) { test_failed(t, "fopen: %s", strerror(errno)); return (-1); } (void) fprintf(logf, "===================\n"); (void) fprintf(logf, "PROGRAM:\n%s\n", sym_test_prog(st)); (void) fprintf(logf, "COMMAND: %s\n", cmd); (void) fprintf(logf, "EXPECT: %s\n", need ? "OK" : "FAIL"); (void) fclose(logf); switch (system(cmd)) { case -1: test_failed(t, "error compiling in %s: %s", env_name(cenv), strerror(errno)); return (-1); case 0: if (!need) { fail_count++; show_file(t, lfile); test_failed(t, "symbol visible in %s", env_name(cenv)); return (-1); } break; default: if (need) { fail_count++; show_file(t, lfile); test_failed(t, "error compiling in %s", env_name(cenv)); return (-1); } break; } good_count++; return (0); }
void find_compiler(void) { test_t t; int i; FILE *cf; t = test_start("finding compiler"); if ((cf = fopen(cfile, "w+")) == NULL) { test_failed(t, "Unable to open %s for write: %s", cfile, strerror(errno)); return; } (void) fprintf(cf, "#include <stdio.h>\n"); (void) fprintf(cf, "int main(int argc, char **argv) {\n"); (void) fprintf(cf, "#if defined(__SUNPRO_C)\n"); (void) fprintf(cf, "exit(51);\n"); (void) fprintf(cf, "#elif defined(__GNUC__)\n"); (void) fprintf(cf, "exit(52);\n"); (void) fprintf(cf, "#else\n"); (void) fprintf(cf, "exit(99)\n"); (void) fprintf(cf, "#endif\n}\n"); (void) fclose(cf); for (i = 0; compilers[i] != NULL; i++) { char cmd[256]; int rv; (void) snprintf(cmd, sizeof (cmd), "%s %s %s -o %s >/dev/null 2>&1", compilers[i], MFLAG, cfile, efile); test_debugf(t, "trying %s", cmd); rv = system(cmd); test_debugf(t, "result: %d", rv); if ((rv < 0) || !WIFEXITED(rv) || WEXITSTATUS(rv) != 0) continue; rv = system(efile); if (rv >= 0 && WIFEXITED(rv)) { rv = WEXITSTATUS(rv); } else { rv = -1; } switch (rv) { case 51: /* STUDIO */ test_debugf(t, "Found Studio C"); c89flags = "-Xc -errwarn=%all -v -xc99=%none " MFLAG; c99flags = "-Xc -errwarn=%all -v -xc99=%all " MFLAG; c11flags = NULL; if (extra_debug) { test_debugf(t, "c89flags: %s", c89flags); test_debugf(t, "c99flags: %s", c99flags); } test_passed(t); break; case 52: /* GCC */ test_debugf(t, "Found GNU C"); c89flags = "-Wall -Werror -std=c89 -nostdinc " "-isystem /usr/include " MFLAG; c99flags = "-Wall -Werror -std=c99 -nostdinc " "-isystem /usr/include " MFLAG; c11flags = "-Wall -Werror -std=c11 -nostdinc " "-isystem /usr/include " MFLAG; if (extra_debug) { test_debugf(t, "c89flags: %s", c89flags); test_debugf(t, "c99flags: %s", c99flags); } test_passed(t); break; case 99: test_debugf(t, "Found unknown (unsupported) compiler"); continue; default: continue; } myasprintf(&compiler, "%s", compilers[i]); test_debugf(t, "compiler: %s", compiler); return; } test_failed(t, "No compiler found."); }
static int gpsd_start( int unit, peerT * peer) { clockprocT * const pp = peer->procptr; gpsd_unitT * const up = emalloc_zero(sizeof(*up)); struct stat sb; /* initialize the unit structure */ up->fdt = -1; up->addr = s_gpsd_addr; up->tickpres = TICKOVER_LOW; /* setup refclock processing */ up->unit = unit; pp->unitptr = (caddr_t)up; pp->io.fd = -1; pp->io.clock_recv = gpsd_receive; pp->io.srcclock = peer; pp->io.datalen = 0; pp->a_lastcode[0] = '\0'; pp->lencode = 0; pp->clockdesc = DESCRIPTION; memcpy(&pp->refid, REFID, 4); /* Initialize miscellaneous variables */ peer->precision = PRECISION; /* Create the device name and check for a Character Device. It's * assumed that GPSD was started with the same link, so the * names match. (If this is not practicable, we will have to * read the symlink, if any, so we can get the true device * file.) */ if (-1 == myasprintf(&up->device, "%s%u", s_dev_stem, unit)) { msyslog(LOG_ERR, "%s clock device name too long", refnumtoa(&peer->srcadr)); goto dev_fail; } if (-1 == stat(up->device, &sb) || !S_ISCHR(sb.st_mode)) { msyslog(LOG_ERR, "%s: '%s' is not a character device", refnumtoa(&peer->srcadr), up->device); goto dev_fail; } LOGIF(CLOCKINFO, (LOG_NOTICE, "%s: startup, device is '%s'", refnumtoa(&peer->srcadr), up->device)); return TRUE; dev_fail: /* On failure, remove all UNIT ressources and declare defeat. */ INSIST (up); free(up->device); free(up); pp->unitptr = (caddr_t)NULL; return FALSE; }