Commit 12a50cc6 by Dwight Engen Committed by Stéphane Graber

Make config api items const

This makes it easier to write a binding, and presents a cleaner API. Use strdupa in a few places to get mutable strings for tokenizing / parsing. Also change the argv type in lxcapi_start and lxcapi_create to match that of execv(3). Signed-off-by: 's avatarDwight Engen <dwight.engen@oracle.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 12ad0f90
......@@ -2478,7 +2478,7 @@ static void lxc_remove_nic(struct lxc_list *it)
}
/* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
int lxc_clear_nic(struct lxc_conf *c, char *key)
int lxc_clear_nic(struct lxc_conf *c, const char *key)
{
char *p1;
int ret, idx, i;
......@@ -2586,11 +2586,11 @@ int lxc_clear_config_caps(struct lxc_conf *c)
return 0;
}
int lxc_clear_cgroups(struct lxc_conf *c, char *key)
int lxc_clear_cgroups(struct lxc_conf *c, const char *key)
{
struct lxc_list *it,*next;
bool all = false;
char *k = key + 11;
const char *k = key + 11;
if (strcmp(key, "lxc.cgroup") == 0)
all = true;
......@@ -2620,11 +2620,11 @@ int lxc_clear_mount_entries(struct lxc_conf *c)
return 0;
}
int lxc_clear_hooks(struct lxc_conf *c, char *key)
int lxc_clear_hooks(struct lxc_conf *c, const char *key)
{
struct lxc_list *it,*next;
bool all = false, done = false;
char *k = key + 9;
const char *k = key + 9;
int i;
if (strcmp(key, "lxc.hook") == 0)
......
......@@ -259,11 +259,11 @@ extern int lxc_create_tty(const char *name, struct lxc_conf *conf);
extern void lxc_delete_tty(struct lxc_tty_info *tty_info);
extern int lxc_clear_config_network(struct lxc_conf *c);
extern int lxc_clear_nic(struct lxc_conf *c, char *key);
extern int lxc_clear_nic(struct lxc_conf *c, const char *key);
extern int lxc_clear_config_caps(struct lxc_conf *c);
extern int lxc_clear_cgroups(struct lxc_conf *c, char *key);
extern int lxc_clear_cgroups(struct lxc_conf *c, const char *key);
extern int lxc_clear_mount_entries(struct lxc_conf *c);
extern int lxc_clear_hooks(struct lxc_conf *c, char *key);
extern int lxc_clear_hooks(struct lxc_conf *c, const char *key);
/*
* Configure the container from inside
......
......@@ -27,14 +27,14 @@
struct lxc_conf;
struct lxc_list;
typedef int (*config_cb)(const char *, char *, struct lxc_conf *);
typedef int (*config_cb)(const char *, const char *, struct lxc_conf *);
struct lxc_config_t {
char *name;
config_cb cb;
};
extern struct lxc_config_t *lxc_getconfig(const char *key);
extern int lxc_list_nicconfigs(struct lxc_conf *c, char *key, char *retv, int inlen);
extern int lxc_list_nicconfigs(struct lxc_conf *c, const char *key, char *retv, int inlen);
extern int lxc_listconfigs(char *retv, int inlen);
extern int lxc_config_read(const char *file, struct lxc_conf *conf);
extern int lxc_config_readline(char *buffer, struct lxc_conf *conf);
......@@ -46,7 +46,7 @@ extern int lxc_config_define_load(struct lxc_list *defines,
/* needed for lxc-attach */
extern signed long lxc_config_parse_arch(const char *arch);
extern int lxc_get_config_item(struct lxc_conf *c, char *key, char *retv, int inlen);
extern int lxc_clear_config_item(struct lxc_conf *c, char *key);
extern int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv, int inlen);
extern int lxc_clear_config_item(struct lxc_conf *c, const char *key);
extern void write_config(FILE *fout, struct lxc_conf *c);
#endif
......@@ -182,7 +182,7 @@ extern const char const *lxc_version(void);
/*
* Create and return a new lxccontainer struct.
*/
extern struct lxc_container *lxc_container_new(char *name);
extern struct lxc_container *lxc_container_new(const char *name);
/*
* Returns 1 on success, 0 on failure.
......
......@@ -218,7 +218,7 @@ static pid_t lxcapi_init_pid(struct lxc_container *c)
return ret;
}
static bool load_config_locked(struct lxc_container *c, char *fname)
static bool load_config_locked(struct lxc_container *c, const char *fname)
{
if (!c->lxc_conf)
c->lxc_conf = lxc_conf_init();
......@@ -227,10 +227,10 @@ static bool load_config_locked(struct lxc_container *c, char *fname)
return false;
}
static bool lxcapi_load_config(struct lxc_container *c, char *alt_file)
static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
{
bool ret = false;
char *fname;
const char *fname;
if (!c)
return false;
......@@ -253,7 +253,7 @@ static void lxcapi_want_daemonize(struct lxc_container *c)
c->daemonize = 1;
}
static bool lxcapi_wait(struct lxc_container *c, char *state, int timeout)
static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
{
int ret;
......@@ -284,7 +284,7 @@ static bool wait_on_daemonized_start(struct lxc_container *c)
* I can't decide if it'd be more convenient for callers if we accept '...',
* or a null-terminated array (i.e. execl vs execv)
*/
static bool lxcapi_start(struct lxc_container *c, int useinit, char ** argv)
static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
{
int ret;
struct lxc_conf *conf;
......@@ -499,7 +499,7 @@ static bool create_container_dir(struct lxc_container *c)
* for ->create, argv contains the arguments to pass to the template,
* terminated by NULL. If no arguments, you can just pass NULL.
*/
static bool lxcapi_create(struct lxc_container *c, char *t, char **argv)
static bool lxcapi_create(struct lxc_container *c, char *t, char *const argv[])
{
bool bret = false;
pid_t pid;
......@@ -703,7 +703,7 @@ out:
return bret;
}
static bool lxcapi_clear_config_item(struct lxc_container *c, char *key)
static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
{
int ret;
......@@ -717,7 +717,7 @@ static bool lxcapi_clear_config_item(struct lxc_container *c, char *key)
return ret == 0;
}
static int lxcapi_get_config_item(struct lxc_container *c, char *key, char *retv, int inlen)
static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
{
int ret;
......@@ -731,7 +731,7 @@ static int lxcapi_get_config_item(struct lxc_container *c, char *key, char *retv
return ret;
}
static int lxcapi_get_keys(struct lxc_container *c, char *key, char *retv, int inlen)
static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
{
if (!key)
return lxc_listconfigs(retv, inlen);
......@@ -754,7 +754,7 @@ static int lxcapi_get_keys(struct lxc_container *c, char *key, char *retv, int i
/* default config file - should probably come through autoconf */
#define LXC_DEFAULT_CONFIG "/etc/lxc/lxc.conf"
static bool lxcapi_save_config(struct lxc_container *c, char *alt_file)
static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
{
if (!alt_file)
alt_file = c->configfile;
......@@ -815,7 +815,7 @@ again:
return WEXITSTATUS(status) == 0;
}
static bool lxcapi_set_config_item(struct lxc_container *c, char *key, char *v)
static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
{
int ret;
bool b = false;
......@@ -850,7 +850,7 @@ static char *lxcapi_config_file_name(struct lxc_container *c)
return strdup(c->configfile);
}
struct lxc_container *lxc_container_new(char *name)
struct lxc_container *lxc_container_new(const char *name)
{
struct lxc_container *c;
int ret, len;
......
......@@ -24,9 +24,9 @@ struct lxc_container {
bool (*freeze)(struct lxc_container *c);
bool (*unfreeze)(struct lxc_container *c);
pid_t (*init_pid)(struct lxc_container *c);
bool (*load_config)(struct lxc_container *c, char *alt_file);
bool (*load_config)(struct lxc_container *c, const char *alt_file);
/* The '...' is the command line. If provided, it must be ended with a NULL */
bool (*start)(struct lxc_container *c, int useinit, char ** argv);
bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
bool (*startl)(struct lxc_container *c, int useinit, ...);
bool (*stop)(struct lxc_container *c);
void (*want_daemonize)(struct lxc_container *c);
......@@ -34,20 +34,20 @@ struct lxc_container {
char *(*config_file_name)(struct lxc_container *c);
// for wait, timeout == -1 means wait forever, timeout == 0 means don't wait.
// otherwise timeout is seconds to wait.
bool (*wait)(struct lxc_container *c, char *state, int timeout);
bool (*set_config_item)(struct lxc_container *c, char *key, char *value);
bool (*wait)(struct lxc_container *c, const char *state, int timeout);
bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
bool (*destroy)(struct lxc_container *c);
bool (*save_config)(struct lxc_container *c, char *alt_file);
bool (*create)(struct lxc_container *c, char *t, char **argv);
bool (*save_config)(struct lxc_container *c, const char *alt_file);
bool (*create)(struct lxc_container *c, char *t, char *const argv[]);
bool (*createl)(struct lxc_container *c, char *t, ...);
/* send SIGPWR. if timeout is not 0 or -1, do a hard stop after timeout seconds */
bool (*shutdown)(struct lxc_container *c, int timeout);
/* clear all network or capability items in the in-memory configuration */
bool (*clear_config_item)(struct lxc_container *c, char *key);
bool (*clear_config_item)(struct lxc_container *c, const char *key);
/* print a config item to a in-memory string allocated by the caller. Return
* the length which was our would be printed. */
int (*get_config_item)(struct lxc_container *c, char *key, char *retv, int inlen);
int (*get_keys)(struct lxc_container *c, char *key, char *retv, int inlen);
int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
#if 0
bool (*commit_cgroups)(struct lxc_container *c);
......@@ -59,7 +59,7 @@ struct lxc_container {
#endif
};
struct lxc_container *lxc_container_new(char *name);
struct lxc_container *lxc_container_new(const char *name);
int lxc_container_get(struct lxc_container *c);
int lxc_container_put(struct lxc_container *c);
int lxc_get_wait_states(const char **states);
......
......@@ -27,7 +27,7 @@
#define LXCLOCK_PREFIX "/lxcapi."
static char *lxclock_name(char *container)
static char *lxclock_name(const char *container)
{
int ret;
int len = strlen(container) + strlen(LXCLOCK_PREFIX) + 1;
......@@ -62,7 +62,7 @@ static sem_t *lxc_new_unnamed_sem(void)
return s;
}
sem_t *lxc_newlock(char *name)
sem_t *lxc_newlock(const char *name)
{
char *lname;
sem_t *lock;
......
......@@ -44,7 +44,7 @@
* return NULL on failure, else a sem_t * which can be passed to
* lxclock() and lxcunlock().
*/
extern sem_t *lxc_newlock(char *name);
extern sem_t *lxc_newlock(const char *name);
/*
* lxclock: take an existing lock. If timeout is 0, wait
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
......@@ -164,26 +165,33 @@ out:
return ret;
}
static int fillwaitedstates(char *strstates, int *states)
static int fillwaitedstates(const char *strstates, int *states)
{
char *token, *saveptr = NULL;
char *strstates_dup = strdup(strstates);
int state;
token = strtok_r(strstates, "|", &saveptr);
if (!strstates_dup)
return -1;
token = strtok_r(strstates_dup, "|", &saveptr);
while (token) {
state = lxc_str2state(token);
if (state < 0)
if (state < 0) {
free(strstates_dup);
return -1;
}
states[state] = 1;
token = strtok_r(NULL, "|", &saveptr);
}
free(strstates_dup);
return 0;
}
extern int lxc_wait(char *lxcname, char *states, int timeout)
extern int lxc_wait(const char *lxcname, const char *states, int timeout)
{
struct lxc_msg msg;
int state, ret;
......
......@@ -33,6 +33,6 @@ extern lxc_state_t lxc_getstate(const char *name);
extern lxc_state_t lxc_str2state(const char *state);
extern const char *lxc_state2str(lxc_state_t state);
extern int lxc_wait(char *lxcname, char *states, int timeout);
extern int lxc_wait(const char *lxcname, const char *states, int timeout);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment