[lxc-users] library start() API from process name having spaces

Christian Brauner christian at brauner.io
Thu Jul 5 13:42:55 UTC 2018


On Wed, Jul 04, 2018 at 03:23:19PM -0400, Chaetoo3 wrote:
> Hi!
> 
> I try to use the liblxc start() API from my process and it worked, but
> I notice it would not set the process name for the container process
> as it would do from the lxc-start command line.  Instead the container
> process stayed with the name of my caller.

You mean it wouldn't change from "my binary --with --args" to "[lxc monitor]?

> 
> I sniff around and make a local fix.  I'm sorry I do not know how to
> contribute this to lxc, but if anyone want to do that, here is the
> code.  Reason is: /proc/self/stat contains the process name in
> parentheses.  If the name contains space, when liblxc parse the file
> it would get off by one field, and get confused and fail in
> setproctitle().
> 
> I don't know if my fix is robust enough for real, but at least should
> point someone to the right place?  This looks for trailing ") "
> sequence (parent and space).  That is still subject to errors because
> the process name might include such, but maybe there is not a way to
> avoid a false positive sometimes.  You cannot paren match either,
> because a process may not have matching.  Well anyway, this at least
> account for spaces in process name, which is a little bit common
> situation I think.
> 
> I hope it would help someone.
> 
> 
> @@ -296,10 +296,23 @@ int setproctitle(char *title)
>                 return -1;
>         }
>  
> -       /* Skip the first 25 fields, column 26-28 are start_code, end_code,
> +        /* Find the end of the process name, which is bounded by parentheses.
> +         * Some processes may include parens in their name, which in the worst
> +         * case can trigger a false positive no matter what we do here.  This
> +         * tries to avoid that by looking for a ') ' sequence.
> +         */

Interesting. Ccing Mr. Andersen in case he has any opinions or wants to
contribute a patch.

Christian

> +       tmp = buf;
> +
> +       while (tmp[0] && tmp[1] && tmp[0] != ')' && tmp[1] != ' ') {
> +               tmp = strchr(tmp + 1, ')');
> +               if (!tmp)
> +                       return -1;
> +       }
> +
> +       /* Skip the next 23 fields, column 26-28 are start_code, end_code,
>          * and start_stack */
> -       tmp = strchr(buf, ' ');
> -       for (i = 0; i < 24; i++) {
> +       tmp = strchr(tmp+1, ' ');
> +       for (i = 0; i < 23; i++) {
>                 if (!tmp)
>                         return -1;
>                 tmp = strchr(tmp+1, ' ');
> 
> 
> _______________________________________________
> lxc-users mailing list
> lxc-users at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-users


More information about the lxc-users mailing list