diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-01-08 15:31:17 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-01-08 16:17:35 +0200 |
commit | 489432d354d88049afe4af54c29965d382d67f7a (patch) | |
tree | 84725496a92564824f75f0a2edd5a595411ffab2 /src/progman.c | |
parent | 3325fed2895f079486b88c65409c73153fec306f (diff) | |
download | pies-489432d354d88049afe4af54c29965d382d67f7a.tar.gz pies-489432d354d88049afe4af54c29965d382d67f7a.tar.bz2 |
Uniformly use grecs memory management functions.
* gnulib.modules: Remove unneded modules.
* ident/ident.h: Remove xalloc.h, include errno.h
* ident/ident.c: Use standard allocation functions instead of x*
* ident/pam.c: Remove.
* ident/provider.c: Remove.
* ident/system.c: Remove.
* src/meta.c: Remove.
* src/Makefile.am: Remove meta.c
* src/progman.c: Use grecs_* allocation functions instead of x*.
(notify): Use wordsplit to expand variables within message.
Rename variables: program-name to program_name;
canonical-program-name to canonical_program_name.
* doc/pies.texi: Update.
* src/depmap.c: Use grecs_* allocation functions instead of x*.
(depmap_end): New function.
* src/diag.c (logmsg_vprintf): Use grecs_txtacc instead of obstack.
* src/pies.h (depmap_end): New proto.
Remove unused includes.
* src/acl.c: Use grecs_* allocation functions instead of x*.
* src/ctl.c: Likewise.
* src/inetd.c: Likewise.
* src/limits.c: Likewise.
* src/meta1gram.y: Likewise.
* src/meta1lex.l: Likewise.
* src/pies.c: Likewise.
* src/socket.c: Likewise.
* src/sysvinit.c: Likewise.
* src/userprivs.c: Likewise.
Diffstat (limited to 'src/progman.c')
-rw-r--r-- | src/progman.c | 218 |
1 files changed, 146 insertions, 72 deletions
diff --git a/src/progman.c b/src/progman.c index 216fb72..eb7b70f 100644 --- a/src/progman.c +++ b/src/progman.c @@ -201,11 +201,12 @@ static char * redir_tag (struct prog *master, int type) { static char *redirstr[2] = { "stdout", "stderr" }; - char *str; + char *str = NULL; + size_t len = 0; if (type < ARRAY_SIZE(redirstr)) - str = xasprintf ("%s/%s", master->tag, redirstr[type]); + grecs_asprintf (&str, &len, "%s/%s", master->tag, redirstr[type]); else - str = xasprintf ("%s/%d", master->tag, type); + grecs_asprintf (&str, &len, "%s/%d", master->tag, type); return str; } @@ -214,8 +215,8 @@ register_redir (int type, struct prog *master) { char *tag = redir_tag (master, type); char *pstr; - struct prog *pp = xzalloc (sizeof (*pp) + strlen (tag) + 1 + - 2 * sizeof (char**)); + struct prog *pp = grecs_zalloc (sizeof (*pp) + strlen (tag) + 1 + + 2 * sizeof (char**)); pp->type = TYPE_REDIRECTOR; pstr = (char *) (pp + 1); @@ -251,7 +252,7 @@ register_prog0 (struct component *comp, unsigned index) { struct prog *newp; - newp = xzalloc (sizeof (*newp)); + newp = grecs_zalloc (sizeof (*newp)); newp->type = TYPE_COMPONENT; newp->tag = comp->tag; newp->pid = 0; @@ -280,8 +281,7 @@ register_prog (struct component *comp) void register_command (char *tag, char *command, pid_t pid) { - struct prog *newp = xzalloc (sizeof (*newp)); - newp = xzalloc (sizeof (*newp)); + struct prog *newp = grecs_zalloc (sizeof (*newp)); newp->type = TYPE_COMMAND; newp->tag = tag; newp->pid = pid; @@ -321,7 +321,7 @@ prog_rebuild_prerequisites (struct prog *prog) if (depc == 0) return; - prog->prereq = xcalloc (depc + 1, sizeof (prog->prereq[0])); + prog->prereq = grecs_calloc (depc + 1, sizeof (prog->prereq[0])); depc = 0; if (comp->prereq) @@ -549,7 +549,7 @@ conn_class_lookup (const char *tag, int install = 1; struct conn_class *probe, *ret; - probe = xmalloc (sizeof (probe[0])); + probe = grecs_malloc (sizeof (probe[0])); probe->tag = tag; probe->sa_storage = *sa_storage_ptr; probe->sa_len = sa_len; @@ -578,7 +578,7 @@ conn_class_lookup (const char *tag, NULL, conn_class_free); if (!conn_tab) - xalloc_die (); + grecs_alloc_die (); } ret = grecs_symtab_lookup_or_install (conn_tab, probe, &install); @@ -671,7 +671,7 @@ add_env (const char *name, const char *value) char **new_env; envsize = i + 4; - new_env = xcalloc (envsize, sizeof new_env[0]); + new_env = grecs_calloc (envsize, sizeof new_env[0]); for (i = 0; (new_env[i] = environ[i]); i++) ; environ = new_env; @@ -679,15 +679,15 @@ add_env (const char *name, const char *value) else if (i == envsize) { envsize += 4; - environ = xrealloc (environ, - envsize * sizeof environ[0]); + environ = grecs_realloc (environ, + envsize * sizeof environ[0]); } environ[i+1] = NULL; } if (!value) value = ""; - p = xmalloc (namelen + 1 + strlen (value) + 1); + p = grecs_malloc (namelen + 1 + strlen (value) + 1); strcpy (p, name); p[namelen] = '='; strcpy (p + namelen + 1, value); @@ -743,7 +743,7 @@ env_concat (const char *name, size_t namelen, const char *a, const char *b) if (a && b) { - res = xmalloc (namelen + 1 + strlen (a) + strlen (b) + 1); + res = grecs_malloc (namelen + 1 + strlen (a) + strlen (b) + 1); strcpy (res + namelen + 1, a); strcat (res, b); } @@ -752,7 +752,7 @@ env_concat (const char *name, size_t namelen, const char *a, const char *b) len = strlen (a); if (c_ispunct (a[len-1])) len--; - res = xmalloc (namelen + 1 + len + 1); + res = grecs_malloc (namelen + 1 + len + 1); memcpy (res + namelen + 1, a, len); res[namelen + 1 + len] = 0; } @@ -761,7 +761,7 @@ env_concat (const char *name, size_t namelen, const char *a, const char *b) if (c_ispunct (b[0])) b++; len = strlen (b); - res = xmalloc (namelen + 1 + len + 1); + res = grecs_malloc (namelen + 1 + len + 1); strcpy (res + namelen + 1, b); } memcpy (res, name, namelen); @@ -795,7 +795,7 @@ environ_setup (char **hint) count++; /* Allocate new environment. */ - new_env = xcalloc (count + 1, sizeof new_env[0]); + new_env = grecs_calloc (count + 1, sizeof new_env[0]); /* Populate the environment. */ n = 0; @@ -1494,7 +1494,7 @@ component_fixup_depend (struct component *comp) tgt->prereq = grecs_list_create(); } /* FIXME: memory allocation */ - grecs_list_append (tgt->prereq, xstrdup (comp->tag)); + grecs_list_append (tgt->prereq, grecs_strdup (comp->tag)); } grecs_list_free (comp->depend); comp->depend = NULL; @@ -1533,6 +1533,7 @@ print_dep (struct prog *prog) struct prog *dp = prog_lookup_by_idx (n); logmsg_printf (LOG_NOTICE, "%s -> ", dp->tag); } + depmap_end (pos); logmsg_printf (LOG_NOTICE, "%s\n", prog->tag); } @@ -1791,7 +1792,7 @@ prog_start_prerequisites (struct prog *prog) { int i; int ret; - + if (prog->v.p.status == status_disabled) return 1; if (!prog->prereq) @@ -1870,7 +1871,7 @@ prog_stop_dependents (struct prog *prog) } prog_stop (dp, SIGTERM); } - free (pos); + depmap_end (pos); } void @@ -2009,7 +2010,7 @@ wordsplit_string (struct wordsplit const *ws) { char *ret, *p; size_t count = ws->ws_wordc + ws->ws_offs; - char **argv = xcalloc (count, sizeof (argv[0])); + char **argv = grecs_calloc (count, sizeof (argv[0])); size_t i; size_t len = 0; @@ -2019,7 +2020,7 @@ wordsplit_string (struct wordsplit const *ws) len += strlen (argv[i]); } len += count; - ret = xmalloc (len); + ret = grecs_malloc (len); for (i = 0, p = ret; i < count; i++) { @@ -2168,62 +2169,135 @@ send_msg (char *rcpts, const char *msg_text) static const char default_termination_message[] = "From: <>\n" -"X-Agent: ${canonical-program-name} (${package} ${version})\n" +"X-Agent: ${canonical_program_name} (${package} ${version})\n" "Subject: Component ${component} ${termination} ${retcode}.\n" "\n"; + +struct notify_closure +{ + const char *tag; + int status; + struct action *act; +}; -static void -notify (const char *tag, int status, struct action *act) +static int +notify_get_tag (char **ret, void *data) { - struct metadef mdef[] = - { - { "canonical-program-name", "pies" }, - { "package", PACKAGE }, - { "version", PACKAGE_VERSION }, -#define COMPONENT_IDX 3 - { "component", NULL }, -#define TERMINATION_IDX 4 - { "termination", NULL }, -#define RETCODE_IDX 5 - { "retcode", NULL }, -#define PROGRAM_NAME_IDX 6 - { "program-name", NULL }, -#define INSTANCE_IDX 7 - { "instance", NULL }, - { NULL } - }; - - char *msg_text = NULL; - char buf[INT_BUFSIZE_BOUND (uintmax_t)]; + struct notify_closure const *clos = data; + char *s = strdup (clos->tag); + if (!s) + return WRDSE_NOSPACE; + *ret = s; + return WRDSE_OK; +} - mdef[COMPONENT_IDX].value = (char*) tag; - mdef[INSTANCE_IDX].value = (char*) tag; - if (WIFEXITED (status)) - { - /* TRANSLATORS: The subject of this statement is 'component' */ - mdef[TERMINATION_IDX].value = (char*) _("exited with code"); - mdef[RETCODE_IDX].value = umaxtostr (WEXITSTATUS (status), buf); - } - else if (WIFSIGNALED (status)) - { - /* TRANSLATORS: The subject of this statement is 'component' */ - mdef[TERMINATION_IDX].value = (char*) _("terminated on signal"); - mdef[RETCODE_IDX].value = umaxtostr (WTERMSIG (status), buf); - } +static int +notify_get_termination (char **ret, void *data) +{ + struct notify_closure const *clos = data; + char const *msg; + char *s; + + if (WIFEXITED (clos->status)) + /* TRANSLATORS: The subject of this statement is 'component' */ + msg = _("exited with code"); + else if (WIFSIGNALED (clos->status)) + /* TRANSLATORS: The subject of this statement is 'component' */ + msg = _("terminated on signal"); else - { - mdef[TERMINATION_IDX].value = "UNKNOWN"; - mdef[RETCODE_IDX].value = "UNKNOWN"; - } - mdef[PROGRAM_NAME_IDX].value = (char*) program_name; - msg_text = meta_expand_string (act->message ? - act->message : default_termination_message, - mdef, NULL); + msg = "UNKNOWN"; - send_msg (act->addr, msg_text); - free (msg_text); + s = strdup (msg); + if (!s) + return WRDSE_NOSPACE; + *ret = s; + return WRDSE_OK; } + +static int +notify_get_retcode (char **ret, void *data) +{ + struct notify_closure const *clos = data; + char *s = NULL; + size_t l = 0; + + if (WIFEXITED (clos->status)) + grecs_asprintf (&s, &l, "%d", WEXITSTATUS (clos->status)); + else if (WIFSIGNALED (clos->status)) + grecs_asprintf (&s, &l, "%d", WTERMSIG (clos->status)); + else + s = strdup ("UNKNOWN"); + if (!s) + return WRDSE_NOSPACE; + *ret = s; + return WRDSE_OK; +} + +struct notify_variable +{ + char *name; + size_t name_length; + int (*get) (char **ret, void *data); +}; + +static struct notify_variable notify_vartab[] = { +#define S(s) #s, sizeof(#s)-1 + { S(component), notify_get_tag }, + { S(termination), notify_get_termination }, + { S(retcode), notify_get_retcode }, +#undef S + { NULL } +}; + +static int +notify_getvar (char **ret, const char *vptr, size_t vlen, void *data) +{ + struct notify_variable *var; + for (var = notify_vartab; var->name; var++) + if (var->name_length == vlen && memcmp (var->name, vptr, vlen) == 0) + return var->get (ret, data); + return WRDSE_UNDEF; +} + +static void +notify (const char *tag, int status, struct action *act) +{ + const char *env[] = { +#define PROGRAM_NAME_IDX 1 + "program_name", NULL, +#define INSTANCE_IDX 3 + "instance", NULL, + "canonical_program_name", "pies", + "package", PACKAGE, + "version", PACKAGE_VERSION, + NULL + }; + struct wordsplit ws; + struct notify_closure closure; + closure.tag = tag; + closure.status = status; + closure.act = act; + + env[PROGRAM_NAME_IDX] = program_name; + env[INSTANCE_IDX] = instance; + + ws.ws_env = env; + ws.ws_getvar = notify_getvar; + ws.ws_closure = &closure; + if (wordsplit (act->message ? act->message : default_termination_message, + &ws, + WRDSF_NOSPLIT | WRDSF_NOCMD | WRDSF_QUOTE | + WRDSF_ENV | WRDSF_ENV_KV | + WRDSF_GETVAR | WRDSF_CLOSURE)) + { + logmsg (LOG_ERR, "wordsplit: %s", wordsplit_strerror (&ws)); + return; + } + send_msg (act->addr, ws.ws_wordv[0]); + wordsplit_free (&ws); +} + static int status_matches_p (struct action *act, unsigned status) { @@ -2281,7 +2355,7 @@ run_command (struct action *act, struct prog *prog, unsigned retcode, /* Master */ debug (1, (_("started command: %s, pid=%lu"), act->command, (unsigned long) pid)); - register_command ((char*) _("[action]"), xstrdup (act->command), pid); + register_command ((char*) _("[action]"), grecs_strdup (act->command), pid); } static void |