15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2009 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file implements BSD-style setproctitle() for Linux.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It is written such that it can easily be compiled outside Chromium.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Linux kernel sets up two locations in memory to pass arguments and
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// environment variables to processes. First, there are two char* arrays stored
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// one after another: argv and environ. A pointer to argv is passed to main(),
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// while glibc sets the global variable |environ| to point at the latter. Both
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of these arrays are terminated by a NULL pointer; the environment array is
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// also followed by some empty space to allow additional variables to be added.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These arrays contain pointers to a second location in memory, where the
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// strings themselves are stored one after another: first all the arguments,
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// then the environment variables. The kernel will allocate a single page of
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// memory for this purpose, so the end of the page containing argv[0] is the
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// end of the storage potentially available to store the process title.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When the kernel reads the command line arguments for a process, it looks at
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the range of memory within this page that it initially used for the argument
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// list. If the terminating '\0' character is still where it expects, nothing
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// further is done. If it has been overwritten, the kernel will scan up to the
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size of a page looking for another. (Note, however, that in general not that
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// much space is actually mapped, since argv[0] is rarely page-aligned and only
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// one page is mapped.)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Thus to change the process title, we must move any environment variables out
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the way to make room for a potentially longer title, and then overwrite
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the memory pointed to by argv[0] with a single replacement string, making
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sure its size does not exceed the available space.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It is perhaps worth noting that patches to add a system call to Linux for
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this, like in BSD, have never made it in: this is the "official" way to do
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this on Linux. Presumably it is not in glibc due to some disagreement over
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this position within the glibc project, leaving applications caught in the
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// middle. (Also, only a very few applications need or want this anyway.)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/set_process_title_linux.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdarg.h>
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdint.h>
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern char** environ;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char** g_main_argv = NULL;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static char* g_orig_argv0 = NULL;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void setproctitle(const char* fmt, ...) {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  va_list ap;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t i, avail_size;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uintptr_t page_size, page, page_end;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sanity check before we try and set the process title.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The BSD version allows fmt == NULL to restore the original title.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!g_main_argv || !environ || !fmt)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!g_orig_argv0) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Save the original argv[0].
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_orig_argv0 = strdup(g_main_argv[0]);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!g_orig_argv0)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  page_size = sysconf(_SC_PAGESIZE);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the page on which the argument list and environment live.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  page = (uintptr_t) g_main_argv[0];
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  page -= page % page_size;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  page_end = page + page_size;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Move the environment out of the way. Note that we are moving the values,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not the environment array itself (which may not be on the page we need
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to overwrite anyway).
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = 0; environ[i]; ++i) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    uintptr_t env_i = (uintptr_t) environ[i];
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Only move the value if it's actually in the way. This avoids
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // leaking copies of the values if this function is called again.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (page <= env_i && env_i < page_end) {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      char* copy = strdup(environ[i]);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Be paranoid. Check for allocation failure and bail out.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!copy)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      environ[i] = copy;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Put the title in argv[0]. We have to zero out the space first since the
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kernel doesn't actually look for a null terminator unless we make the
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // argument list longer than it started.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  avail_size = page_end - (uintptr_t) g_main_argv[0];
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  memset(g_main_argv[0], 0, avail_size);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  va_start(ap, fmt);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (fmt[0] == '-') {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (size < avail_size)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  va_end(ap);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_main_argv[1] = NULL;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A version of this built into glibc would not need this function, since
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it could stash the argv pointer in __libc_start_main(). But we need it.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void setproctitle_init(const char** main_argv) {
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (g_main_argv)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uintptr_t page_size = sysconf(_SC_PAGESIZE);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that the argv array is in fact on the same page of memory
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as the environment array just as an added measure of protection.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_main_argv = const_cast<char**>(main_argv);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
116