tools: lxc-autostart: non-functional changes

Closes #2073. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 9267beba
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
...@@ -144,14 +146,13 @@ int lists_contain_common_entry(struct lxc_list *p1, struct lxc_list *p2) { ...@@ -144,14 +146,13 @@ int lists_contain_common_entry(struct lxc_list *p1, struct lxc_list *p2) {
return 0; return 0;
} }
/* /* This is a variation of get_list below it. This version allows two additional
* This is a variation of get_list below it. * features. If a list is passed to it, it adds to it. It allows for empty
* This version allows two additional features. * entries (i.e. "group1,,group2") generating and empty list entry.
* If a list is passed to it, it adds to it.
* It allows for empty entries (i.e. "group1,,group2") generating
* and empty list entry.
*/ */
static struct lxc_list *accumulate_list(char *input, char *delimiter, struct lxc_list *str_list) { static struct lxc_list *accumulate_list(char *input, char *delimiter,
struct lxc_list *str_list)
{
char *workstr = NULL; char *workstr = NULL;
char *workptr = NULL; char *workptr = NULL;
char *next_ptr = NULL; char *next_ptr = NULL;
...@@ -159,45 +160,33 @@ static struct lxc_list *accumulate_list(char *input, char *delimiter, struct lxc ...@@ -159,45 +160,33 @@ static struct lxc_list *accumulate_list(char *input, char *delimiter, struct lxc
struct lxc_list *workstr_list; struct lxc_list *workstr_list;
workstr = strdup(input); workstr = strdup(input);
if (!workstr) { if (!workstr)
return NULL; return NULL;
}
workstr_list = str_list; workstr_list = str_list;
if ( ! workstr_list ) { if (!workstr_list) {
workstr_list = malloc(sizeof(*workstr_list)); workstr_list = malloc(sizeof(*workstr_list));
lxc_list_init(workstr_list); lxc_list_init(workstr_list);
} }
for (workptr = workstr; workptr; workptr = next_ptr) { for (workptr = workstr; workptr; workptr = next_ptr) {
/* /* We can't use strtok_r here because it collapses multiple
* We can't use strtok_r here because it collapses * delimiters into 1 making empty fields impossible...
* multiple delimiters into 1 making empty fields
* impossible...
*/ */
/* token = strtok_r(workptr, delimiter, &sptr); */ next_ptr = strchr(workptr, *delimiter);
next_ptr = strchr( workptr, *delimiter ); if (next_ptr)
if( next_ptr ) {
*next_ptr++ = '\0'; *next_ptr++ = '\0';
}
/* /* At this point, we'd like to check to see if this group is
* At this point, we'd like to check to see if this * already contained in the list and ignore it if it is... This
* group is already contained in the list and ignore * also helps us with any corner cases where a string begins or
* it if it is... This also helps us with any * ends with a delimiter.
* corner cases where a string begins or ends with a
* delimiter.
*/ */
if (list_contains_entry(workptr, workstr_list)) {
if ( list_contains_entry( workptr, workstr_list ) ) { if (*workptr)
if ( *workptr ) { fprintf(stderr, "Duplicate group \"%s\" in list - ignoring\n", workptr);
fprintf(stderr, "Duplicate group \"%s\" in list - ignoring\n", workptr ); else
fflush(stderr); fprintf(stderr, "Duplicate NULL group in list - ignoring\n");
} else {
fprintf(stderr, "Duplicate NULL group in list - ignoring\n" );
fflush(stderr);
}
} else { } else {
worklist = malloc(sizeof(*worklist)); worklist = malloc(sizeof(*worklist));
if (!worklist) if (!worklist)
...@@ -218,7 +207,8 @@ static struct lxc_list *accumulate_list(char *input, char *delimiter, struct lxc ...@@ -218,7 +207,8 @@ static struct lxc_list *accumulate_list(char *input, char *delimiter, struct lxc
return workstr_list; return workstr_list;
} }
static struct lxc_list *get_list(char *input, char *delimiter) { static struct lxc_list *get_list(char *input, char *delimiter)
{
char *workstr = NULL; char *workstr = NULL;
char *workptr = NULL; char *workptr = NULL;
char *sptr = NULL; char *sptr = NULL;
...@@ -235,11 +225,10 @@ static struct lxc_list *get_list(char *input, char *delimiter) { ...@@ -235,11 +225,10 @@ static struct lxc_list *get_list(char *input, char *delimiter) {
return NULL; return NULL;
} }
for (workptr = workstr;;workptr = NULL) { for (workptr = workstr;; workptr = NULL) {
token = strtok_r(workptr, delimiter, &sptr); token = strtok_r(workptr, delimiter, &sptr);
if (!token) { if (!token)
break; break;
}
worklist = malloc(sizeof(*worklist)); worklist = malloc(sizeof(*worklist));
if (!worklist) if (!worklist)
...@@ -259,16 +248,17 @@ static struct lxc_list *get_list(char *input, char *delimiter) { ...@@ -259,16 +248,17 @@ static struct lxc_list *get_list(char *input, char *delimiter) {
return workstr_list; return workstr_list;
} }
static struct lxc_list *get_config_list(struct lxc_container *c, char *key) { static struct lxc_list *get_config_list(struct lxc_container *c, char *key)
{
int len = 0; int len = 0;
char* value = NULL; char *value = NULL;
struct lxc_list *config_list = NULL; struct lxc_list *config_list = NULL;
len = c->get_config_item(c, key, NULL, 0); len = c->get_config_item(c, key, NULL, 0);
if (len < 0) if (len < 0)
return NULL; return NULL;
value = (char*) malloc(sizeof(char)*len + 1); value = (char *)malloc(sizeof(char) * len + 1);
if (value == NULL) if (value == NULL)
return NULL; return NULL;
...@@ -288,16 +278,16 @@ static struct lxc_list *get_config_list(struct lxc_container *c, char *key) { ...@@ -288,16 +278,16 @@ static struct lxc_list *get_config_list(struct lxc_container *c, char *key) {
return config_list; return config_list;
} }
static int get_config_integer(struct lxc_container *c, char *key) { static int get_config_integer(struct lxc_container *c, char *key)
int len = 0; {
int ret = 0; int len = 0, ret = 0;
char* value = NULL; char *value = NULL;
len = c->get_config_item(c, key, NULL, 0); len = c->get_config_item(c, key, NULL, 0);
if (len < 0) if (len < 0)
return 0; return 0;
value = (char*) malloc(sizeof(char)*len + 1); value = (char *)malloc(sizeof(char) * len + 1);
if (value == NULL) if (value == NULL)
return 0; return 0;
...@@ -314,7 +304,8 @@ static int get_config_integer(struct lxc_container *c, char *key) { ...@@ -314,7 +304,8 @@ static int get_config_integer(struct lxc_container *c, char *key) {
return ret; return ret;
} }
static int cmporder(const void *p1, const void *p2) { static int cmporder(const void *p1, const void *p2)
{
struct lxc_container *c1 = *(struct lxc_container **)p1; struct lxc_container *c1 = *(struct lxc_container **)p1;
struct lxc_container *c2 = *(struct lxc_container **)p2; struct lxc_container *c2 = *(struct lxc_container **)p2;
...@@ -323,33 +314,33 @@ static int cmporder(const void *p1, const void *p2) { ...@@ -323,33 +314,33 @@ static int cmporder(const void *p1, const void *p2) {
if (c1_order == c2_order) if (c1_order == c2_order)
return strcmp(c1->name, c2->name); return strcmp(c1->name, c2->name);
else
return (c1_order - c2_order); return (c1_order - c2_order);
} }
static int toss_list( struct lxc_list *c_groups_list ) { static int toss_list(struct lxc_list *c_groups_list)
{
struct lxc_list *it, *next; struct lxc_list *it, *next;
if (c_groups_list) { if (!c_groups_list)
return 1;
lxc_list_for_each_safe(it, c_groups_list, next) { lxc_list_for_each_safe(it, c_groups_list, next) {
lxc_list_del(it); lxc_list_del(it);
free(it->elem); free(it->elem);
free(it); free(it);
} }
free(c_groups_list); free(c_groups_list);
}
return 1; return 1;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int count = 0; int count = 0, i = 0, ret = 0;
int i = 0; struct lxc_list *cmd_group;
int ret = 0;
struct lxc_container **containers = NULL; struct lxc_container **containers = NULL;
struct lxc_list **c_groups_lists = NULL; struct lxc_list **c_groups_lists = NULL;
struct lxc_list *cmd_group;
struct lxc_log log; struct lxc_log log;
if (lxc_arguments_parse(&my_args, argc, argv)) if (lxc_arguments_parse(&my_args, argc, argv))
...@@ -374,36 +365,29 @@ int main(int argc, char *argv[]) ...@@ -374,36 +365,29 @@ int main(int argc, char *argv[])
if (count < 0) if (count < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (!my_args.all) {
/* Allocate an array for our container group lists */ /* Allocate an array for our container group lists */
if (!my_args.all)
c_groups_lists = calloc( count, sizeof( struct lxc_list * ) ); c_groups_lists = calloc( count, sizeof( struct lxc_list * ) );
}
qsort(&containers[0], count, sizeof(struct lxc_container *), cmporder); qsort(&containers[0], count, sizeof(struct lxc_container *), cmporder);
if (cmd_groups_list && my_args.all) { if (cmd_groups_list && my_args.all)
fprintf(stderr, "Specifying -a (all) with -g (groups) doesn't make sense. All option overrides.\n"); fprintf(stderr, "Specifying -a (all) with -g (groups) doesn't make sense. All option overrides.\n");
fflush(stderr);
}
if (!cmd_groups_list) { /* We need a default cmd_groups_list even for the -a
/*
* We need a default cmd_groups_list even for the -a
* case in order to force a pass through the loop for * case in order to force a pass through the loop for
* the NULL group. This, someday, could be taken from * the NULL group. This, someday, could be taken from
* a config file somewhere... * a config file somewhere...
*/ */
if (!cmd_groups_list)
cmd_groups_list = accumulate_list( "" , ",", NULL ); cmd_groups_list = accumulate_list( "" , ",", NULL );
}
lxc_list_for_each(cmd_group, cmd_groups_list) { lxc_list_for_each(cmd_group, cmd_groups_list) {
/* Because we may take several passes through the container list
/* * We'll switch on if the container pointer is NULL and if we
* Prograpmmers Note: * process a container (run it or decide to ignore it) and call
* Because we may take several passes through the container list * lxc_container_put then we'll NULL it out and not check it
* We'll switch on if the container pointer is NULL and if we process a * again.
* container (run it or decide to ignore it) and call lxc_container_put
* then we'll NULL it out and not check it again.
*/ */
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
struct lxc_container *c = containers[i]; struct lxc_container *c = containers[i];
...@@ -412,10 +396,9 @@ int main(int argc, char *argv[]) ...@@ -412,10 +396,9 @@ int main(int argc, char *argv[])
/* Skip - must have been already processed */ /* Skip - must have been already processed */
continue; continue;
/* /* We haven't loaded the container groups yet so these
* We haven't loaded the container groups yet so * next two checks don't need to free them if they fail.
* these next two checks don't need to free them * They'll fail on the first pass.
* if they fail. They'll fail on the first pass.
*/ */
if (!c->may_control(c)) { if (!c->may_control(c)) {
/* We're done with this container */ /* We're done with this container */
...@@ -442,8 +425,10 @@ int main(int argc, char *argv[]) ...@@ -442,8 +425,10 @@ int main(int argc, char *argv[])
ret = list_contains_entry(cmd_group->elem, c_groups_lists[i]); ret = list_contains_entry(cmd_group->elem, c_groups_lists[i]);
if ( ret == 0 ) { if ( ret == 0 ) {
/* Not in the target group this pass */ /* Not in the target group this pass so
/* Leave in the list for subsequent passes */ * leave in the list for subsequent
* passes.
*/
continue; continue;
} }
} }
...@@ -467,8 +452,7 @@ int main(int argc, char *argv[]) ...@@ -467,8 +452,7 @@ int main(int argc, char *argv[])
} }
} }
} }
} } else if (my_args.hardstop) {
else if (my_args.hardstop) {
/* Kill the container */ /* Kill the container */
if (c->is_running(c)) { if (c->is_running(c)) {
if (my_args.list) { if (my_args.list) {
...@@ -482,8 +466,7 @@ int main(int argc, char *argv[]) ...@@ -482,8 +466,7 @@ int main(int argc, char *argv[])
} }
} }
} }
} } else if (my_args.reboot) {
else if (my_args.reboot) {
/* Reboot the container */ /* Reboot the container */
if (c->is_running(c)) { if (c->is_running(c)) {
if (my_args.list) { if (my_args.list) {
...@@ -500,8 +483,7 @@ int main(int argc, char *argv[]) ...@@ -500,8 +483,7 @@ int main(int argc, char *argv[])
sleep(get_config_integer(c, "lxc.start.delay")); sleep(get_config_integer(c, "lxc.start.delay"));
} }
} }
} } else {
else {
/* Start the container */ /* Start the container */
if (!c->is_running(c)) { if (!c->is_running(c)) {
if (my_args.list) { if (my_args.list) {
...@@ -525,9 +507,8 @@ int main(int argc, char *argv[]) ...@@ -525,9 +507,8 @@ int main(int argc, char *argv[])
* then we're done with this container... We can dump any * then we're done with this container... We can dump any
* c_groups_list and the container itself. * c_groups_list and the container itself.
*/ */
if ( lxc_container_put(c) > 0 ) { if ( lxc_container_put(c) > 0 )
containers[i] = NULL; containers[i] = NULL;
}
if ( c_groups_lists ) { if ( c_groups_lists ) {
toss_list(c_groups_lists[i]); toss_list(c_groups_lists[i]);
c_groups_lists[i] = NULL; c_groups_lists[i] = NULL;
...@@ -538,13 +519,12 @@ int main(int argc, char *argv[]) ...@@ -538,13 +519,12 @@ int main(int argc, char *argv[])
/* clean up any lingering detritus */ /* clean up any lingering detritus */
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if ( containers[i] ) { if (containers[i])
lxc_container_put(containers[i]); lxc_container_put(containers[i]);
}
if ( c_groups_lists && c_groups_lists[i] ) { if (c_groups_lists && c_groups_lists[i])
toss_list(c_groups_lists[i]); toss_list(c_groups_lists[i]);
} }
}
free(c_groups_lists); free(c_groups_lists);
toss_list( cmd_groups_list ); toss_list( cmd_groups_list );
......
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