[systemd-devel] [PATCH V2] Do not serialize environment, when switching root

Lennart Poettering lennart at poettering.net
Mon Apr 8 05:42:31 PDT 2013


On Mon, 08.04.13 14:33, harald at redhat.com (harald at redhat.com) wrote:

> From: Harald Hoyer <harald at redhat.com>
> 
> When switching root, i.e. LANG can be set to the locale of the initramfs
> or "C", if it was unset. When systemd deserializes LANG in the real root
> this would overwrite the setting previously gathered by locale_set().
> 
> To reproduce, boot with an initramfs without locale.conf or change
> /etc/locale.conf to a different language than the initramfs and check a
> daemon started by systemd:
> 
> $ tr "$\000" '\n' </proc/$(pidof sshd)/environ | grep LANG
> LANG=C
> 
> To prevent that, serialization of environment variables is skipped, when
> serializing for switching root.
> 
> https://bugzilla.redhat.com/show_bug.cgi?id=949525

Looks good! Please apply!

> ---
>  src/core/main.c    |  8 ++++----
>  src/core/manager.c | 18 ++++++++++--------
>  src/core/manager.h |  2 +-
>  3 files changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/src/core/main.c b/src/core/main.c
> index 921476a..7899761 100644
> --- a/src/core/main.c
> +++ b/src/core/main.c
> @@ -1088,7 +1088,7 @@ static int version(void) {
>          return 0;
>  }
>  
> -static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool serialize_jobs) {
> +static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
>          FILE *f = NULL;
>          FDSet *fds = NULL;
>          int r;
> @@ -1113,7 +1113,7 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool serialize
>                  goto fail;
>          }
>  
> -        r = manager_serialize(m, f, fds, serialize_jobs);
> +        r = manager_serialize(m, f, fds, switching_root);
>          if (r < 0) {
>                  log_error("Failed to serialize state: %s", strerror(-r));
>                  goto fail;
> @@ -1780,7 +1780,7 @@ int main(int argc, char *argv[]) {
>  
>                  case MANAGER_REEXECUTE:
>  
> -                        if (prepare_reexecute(m, &serialization, &fds, true) < 0)
> +                        if (prepare_reexecute(m, &serialization, &fds, false) < 0)
>                                  goto finish;
>  
>                          reexecute = true;
> @@ -1794,7 +1794,7 @@ int main(int argc, char *argv[]) {
>                          m->switch_root = m->switch_root_init = NULL;
>  
>                          if (!switch_root_init)
> -                                if (prepare_reexecute(m, &serialization, &fds, false) < 0)
> +                                if (prepare_reexecute(m, &serialization, &fds, true) < 0)
>                                          goto finish;
>  
>                          reexecute = true;
> diff --git a/src/core/manager.c b/src/core/manager.c
> index 549153e..f8d097e 100644
> --- a/src/core/manager.c
> +++ b/src/core/manager.c
> @@ -2004,7 +2004,7 @@ int manager_open_serialization(Manager *m, FILE **_f) {
>          return 0;
>  }
>  
> -int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs) {
> +int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root) {
>          Iterator i;
>          Unit *u;
>          const char *t;
> @@ -2032,12 +2032,14 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs) {
>                  dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
>          }
>  
> -        STRV_FOREACH(e, m->environment) {
> -                _cleanup_free_ char *ce;
> +        if (!switching_root) {
> +                STRV_FOREACH(e, m->environment) {
> +                        _cleanup_free_ char *ce;
>  
> -                ce = cescape(*e);
> -                if (ce)
> -                        fprintf(f, "env=%s\n", *e);
> +                        ce = cescape(*e);
> +                        if (ce)
> +                                fprintf(f, "env=%s\n", *e);
> +                }
>          }
>  
>          fputc('\n', f);
> @@ -2053,7 +2055,7 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs) {
>                  fputs(u->id, f);
>                  fputc('\n', f);
>  
> -                if ((r = unit_serialize(u, f, fds, serialize_jobs)) < 0) {
> +                if ((r = unit_serialize(u, f, fds, !switching_root)) < 0) {
>                          m->n_reloading --;
>                          return r;
>                  }
> @@ -2241,7 +2243,7 @@ int manager_reload(Manager *m) {
>                  goto finish;
>          }
>  
> -        r = manager_serialize(m, f, fds, true);
> +        r = manager_serialize(m, f, fds, false);
>          if (r < 0) {
>                  m->n_reloading --;
>                  goto finish;
> diff --git a/src/core/manager.h b/src/core/manager.h
> index 9d8d943..649a8aa 100644
> --- a/src/core/manager.h
> +++ b/src/core/manager.h
> @@ -278,7 +278,7 @@ void manager_dispatch_bus_query_pid_done(Manager *m, const char *name, pid_t pid
>  
>  int manager_open_serialization(Manager *m, FILE **_f);
>  
> -int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs);
> +int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root);
>  int manager_deserialize(Manager *m, FILE *f, FDSet *fds);
>  int manager_distribute_fds(Manager *m, FDSet *fds);
>  


Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list