void handle_acl_log(const struct flow *headers, struct ofpbuf *userdata) { if (!VLOG_IS_INFO_ENABLED()) { return; } struct log_pin_header *lph = ofpbuf_try_pull(userdata, sizeof *lph); if (!lph) { VLOG_WARN("log data missing"); return; } size_t name_len = userdata->size; char *name = name_len ? xmemdup0(userdata->data, name_len) : NULL; struct ds ds = DS_EMPTY_INITIALIZER; ds_put_cstr(&ds, "name="); json_string_escape(name_len ? name : "<unnamed>", &ds); ds_put_format(&ds, ", verdict=%s, severity=%s: ", log_verdict_to_string(lph->verdict), log_severity_to_string(lph->severity)); flow_format(&ds, headers, NULL); VLOG_INFO("%s", ds_cstr(&ds)); ds_destroy(&ds); free(name); }
uint32_t ofputil_versions_from_string(const char *s) { size_t i = 0; uint32_t bitmap = 0; while (s[i]) { size_t j; int version; char *key; if (is_delimiter(s[i])) { i++; continue; } j = 0; while (s[i + j] && !is_delimiter(s[i + j])) { j++; } key = xmemdup0(s + i, j); version = ofputil_version_from_string(key); if (!version) { VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key); } free(key); bitmap |= 1u << version; i += j; } return bitmap; }
/* Ensure that NAME of length LEN exists in the table, creating an entry if needed. */ static m4_symbol * symtab_fetch (m4_symbol_table *symtab, const char *name, size_t len) { m4_symbol **psymbol; m4_symbol *symbol; m4_string key; assert (symtab); assert (name); /* Safe to cast away const, since m4_hash_lookup doesn't modify key. */ key.str = (char *) name; key.len = len; psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key); if (psymbol) { symbol = *psymbol; } else { /* Use xmemdup0 rather than memdup so that debugging the symbol table is easier. */ m4_string *new_key = (m4_string *) xmalloc (sizeof *new_key); new_key->str = xmemdup0 (name, len); new_key->len = len; symbol = (m4_symbol *) xzalloc (sizeof *symbol); m4_hash_insert (symtab->table, new_key, symbol); } return symbol; }
/* Returns the directory name portion of 'file_name' as a malloc()'d string, * similar to the POSIX dirname() function but thread-safe. */ char * dir_name(const char *file_name) { size_t len = strlen(file_name); while (len > 0 && file_name[len - 1] == '/') { len--; } while (len > 0 && file_name[len - 1] != '/') { len--; } while (len > 0 && file_name[len - 1] == '/') { len--; } return len ? xmemdup0(file_name, len) : all_slashes_name(file_name); }
int main (int argc, char **argv) { char buffer[10] = { 'a', 'b', 'c', 'd', '\0', 'f', 'g', 'h', 'i', 'j' }; set_program_name (argv[0]); /* Empty string. */ { char *result = xmemdup0 (NULL, 0); ASSERT (result); ASSERT (!*result); free (result); } { char *result = xmemdup0 ("", 0); ASSERT (result); ASSERT (!*result); free (result); } /* Various buffer lengths. */ { char *result = xmemdup0 (buffer, 4); ASSERT (result); ASSERT (strcmp (result, buffer) == 0); free (result); } { char *result = xmemdup0 (buffer, 5); ASSERT (result); ASSERT (strcmp (result, buffer) == 0); ASSERT (result[5] == '\0'); free (result); } { char *result = xmemdup0 (buffer, 9); ASSERT (result); ASSERT (memcmp (result, buffer, 9) == 0); ASSERT (result[9] == '\0'); free (result); } { char *result = xmemdup0 (buffer, 10); ASSERT (result); ASSERT (memcmp (result, buffer, 10) == 0); ASSERT (result[10] == '\0'); free (result); } return 0; }
/* the string 's' may not terminate by '\0' at 'length'. */ void lex_token_strcpy(struct lex_token *token, const char *s, size_t length) { if (token->s == NULL || token->s == token->buffer) { if (length + 1 <= sizeof(token->buffer)) { memcpy(token->buffer, s, length); token->buffer[length] = '\0'; token->s = token->buffer; return; } token->s = xmemdup0(s, length); } else { token->s = xrealloc(token->s, length + 1); memcpy(token->s, s, length); token->s[length] = '\0'; } }
int main (int argc, char *argv[]) { const char *file_name; size_t length; char *input; set_program_name (argv[0]); file_name = parse_options (argc, argv); /* Read from stdin into 'input'. Ensure that 'input' ends in a new-line followed by a null byte. */ input = (!strcmp (file_name, "-") ? fread_file (stdin, &length) : read_file (file_name, &length)); if (input == NULL) error (EXIT_FAILURE, errno, "reading %s failed", file_name); if (!check_truncations) { input = xrealloc (input, length + 3); if (length == 0 || input[length - 1] != '\n') input[length++] = '\n'; input[length++] = '\0'; check_segmentation (input, length, true); } else { size_t test_len; for (test_len = 0; test_len <= length; test_len++) { char *copy = xmemdup0 (input, test_len); check_segmentation (copy, test_len + 1, false); free (copy); } } free (input); return 0; }
/* Returns the file name portion of 'file_name' as a malloc()'d string, * similar to the POSIX basename() function but thread-safe. */ char * base_name(const char *file_name) { size_t end, start; end = strlen(file_name); while (end > 0 && file_name[end - 1] == '/') { end--; } if (!end) { return all_slashes_name(file_name); } start = end; while (start > 0 && file_name[start - 1] != '/') { start--; } return xmemdup0(file_name + start, end - start); }
static bool get_token(void) { char *start; while (isspace((unsigned char) *pos)) { pos++; } if (*pos == '\0') { free(token); token = NULL; return false; } start = pos; if (isalpha((unsigned char) *pos)) { while (isalpha((unsigned char) *++pos)) { continue; } } else if (isdigit((unsigned char) *pos)) { if (*pos == '0' && (pos[1] == 'x' || pos[1] == 'X')) { pos += 2; while (isxdigit((unsigned char) *pos)) { pos++; } } else { while (isdigit((unsigned char) *++pos)) { continue; } } } else { pos++; } free(token); token = xmemdup0(start, pos - start); return true; }
static char * read_host_uuid(void) { static const char filename[] = "/etc/xensource-inventory"; char line[128]; FILE *file; file = fopen(filename, "r"); if (!file) { if (errno == ENOENT) { VLOG_DBG("not running on a XenServer"); } else { VLOG_INFO("%s: open: %s", filename, ovs_strerror(errno)); } return NULL; } while (fgets(line, sizeof line, file)) { static const char leader[] = "INSTALLATION_UUID='"; const int leader_len = strlen(leader); const int uuid_len = 36; static const char trailer[] = "'\n"; const int trailer_len = strlen(trailer); if (strlen(line) == leader_len + uuid_len + trailer_len && !memcmp(line, leader, leader_len) && !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) { char *host_uuid = xmemdup0(line + leader_len, uuid_len); VLOG_INFO("running on XenServer, host-uuid %s", host_uuid); fclose(file); return host_uuid; } } fclose(file); VLOG_ERR("%s: INSTALLATION_UUID not found", filename); return NULL; }
char * xstrdup(const char *s) { return xmemdup0(s, strlen(s)); }
/* Parse and sanity check user_spec. * * If successful, set global variables 'uid' and 'gid' * with the parsed results. Global variable 'user' * will be pointing to a string that stores the name * of the user to be switched into. * * Also set 'switch_to_new_user' to true, The actual * user switching is done as soon as daemonize_start() * is called. I/O access before calling daemonize_start() * will still be with root's credential. */ void daemon_set_new_user(const char *user_spec) { char *pos = strchr(user_spec, ':'); size_t init_bufsize, bufsize; init_bufsize = get_sysconf_buffer_size(); uid = getuid(); gid = getgid(); if (geteuid() || uid) { VLOG_FATAL("%s: only root can use --user option", pidfile); } user_spec += strspn(user_spec, " \t\r\n"); size_t len = pos ? pos - user_spec : strlen(user_spec); char *buf; struct passwd pwd, *res; int e; bufsize = init_bufsize; buf = xmalloc(bufsize); if (len) { user = xmemdup0(user_spec, len); while ((e = getpwnam_r(user, &pwd, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e != 0) { VLOG_FATAL("%s: Failed to retrive user %s's uid (%s), aborting.", pidfile, user, ovs_strerror(e)); } } else { /* User name is not specified, use current user. */ while ((e = getpwuid_r(uid, &pwd, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e != 0) { VLOG_FATAL("%s: Failed to retrive current user's name " "(%s), aborting.", pidfile, ovs_strerror(e)); } user = xstrdup(pwd.pw_name); } uid = pwd.pw_uid; gid = pwd.pw_gid; free(buf); if (pos) { char *grpstr = pos + 1; grpstr += strspn(grpstr, " \t\r\n"); if (*grpstr) { struct group grp, *res; bufsize = init_bufsize; buf = xmalloc(bufsize); while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e) { VLOG_FATAL("%s: Failed to get group entry for %s, " "(%s), aborting.", pidfile, grpstr, ovs_strerror(e)); } if (gid != grp.gr_gid) { char **mem; for (mem = grp.gr_mem; *mem; ++mem) { if (!strcmp(*mem, user)) { break; } } if (!*mem) { VLOG_FATAL("%s: Invalid --user option %s (user %s is " "not in group %s), aborting.", pidfile, user_spec, user, grpstr); } gid = grp.gr_gid; } free(buf); } } switch_user = true; }
construct_dpdk_args(const struct smap *ovs_other_config, struct svec *args) { const char *extra_configuration = smap_get(ovs_other_config, "dpdk-extra"); if (extra_configuration) { svec_parse_words(args, extra_configuration); } construct_dpdk_options(ovs_other_config, args); construct_dpdk_mutex_options(ovs_other_config, args); } static ssize_t dpdk_log_write(void *c OVS_UNUSED, const char *buf, size_t size) { char *str = xmemdup0(buf, size); static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(600, 600); static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(600, 600); switch (rte_log_cur_msg_loglevel()) { case RTE_LOG_DEBUG: VLOG_DBG_RL(&dbg_rl, "%s", str); break; case RTE_LOG_INFO: case RTE_LOG_NOTICE: VLOG_INFO_RL(&rl, "%s", str); break; case RTE_LOG_WARNING: VLOG_WARN_RL(&rl, "%s", str); break; case RTE_LOG_ERR:
} } } } /* This callback is used exclusively by m4_symtab_delete(), to cleanup the memory used by the symbol table. As such, the trace bit is reset on every symbol so that m4_symbol_popdef() doesn't try to preserve the table entry. */ static void * symbol_destroy_CB (m4_symbol_table *symtab, const char *name, size_t len, m4_symbol *symbol, void *ignored M4_GNUC_UNUSED) { m4_string key; key.str = xmemdup0 (name, len); key.len = len; symbol->traced = false; while (m4_hash_lookup (symtab->table, &key)) m4_symbol_popdef (symtab, key.str, key.len); free (key.str); return NULL; } /* -- SYMBOL MANAGEMENT --