1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file implements BSD-style setproctitle() for Linux.
6// It is written such that it can easily be compiled outside Chromium.
7//
8// The Linux kernel sets up two locations in memory to pass arguments and
9// environment variables to processes. First, there are two char* arrays stored
10// one after another: argv and environ. A pointer to argv is passed to main(),
11// while glibc sets the global variable |environ| to point at the latter. Both
12// of these arrays are terminated by a NULL pointer; the environment array is
13// also followed by some empty space to allow additional variables to be added.
14//
15// These arrays contain pointers to a second location in memory, where the
16// strings themselves are stored one after another: first all the arguments,
17// then the environment variables. The kernel will allocate a single page of
18// memory for this purpose, so the end of the page containing argv[0] is the
19// end of the storage potentially available to store the process title.
20//
21// When the kernel reads the command line arguments for a process, it looks at
22// the range of memory within this page that it initially used for the argument
23// list. If the terminating '\0' character is still where it expects, nothing
24// further is done. If it has been overwritten, the kernel will scan up to the
25// size of a page looking for another. (Note, however, that in general not that
26// much space is actually mapped, since argv[0] is rarely page-aligned and only
27// one page is mapped.)
28//
29// Thus to change the process title, we must move any environment variables out
30// of the way to make room for a potentially longer title, and then overwrite
31// the memory pointed to by argv[0] with a single replacement string, making
32// sure its size does not exceed the available space.
33//
34// It is perhaps worth noting that patches to add a system call to Linux for
35// this, like in BSD, have never made it in: this is the "official" way to do
36// this on Linux. Presumably it is not in glibc due to some disagreement over
37// this position within the glibc project, leaving applications caught in the
38// middle. (Also, only a very few applications need or want this anyway.)
39
40#include "base/setproctitle_linux.h"
41
42#include <stdarg.h>
43#include <stdint.h>
44#include <stdio.h>
45#include <string.h>
46#include <unistd.h>
47
48extern char** environ;
49
50static char** g_main_argv = NULL;
51static char* g_orig_argv0 = NULL;
52
53void setproctitle(const char* fmt, ...) {
54  va_list ap;
55  size_t i, avail_size;
56  uintptr_t page_size, page, page_end;
57  // Sanity check before we try and set the process title.
58  // The BSD version allows fmt == NULL to restore the original title.
59  if (!g_main_argv || !environ || !fmt)
60    return;
61  if (!g_orig_argv0) {
62    // Save the original argv[0].
63    g_orig_argv0 = strdup(g_main_argv[0]);
64    if (!g_orig_argv0)
65      return;
66  }
67  page_size = sysconf(_SC_PAGESIZE);
68  // Get the page on which the argument list and environment live.
69  page = (uintptr_t) g_main_argv[0];
70  page -= page % page_size;
71  page_end = page + page_size;
72  // Move the environment out of the way. Note that we are moving the values,
73  // not the environment array itself (which may not be on the page we need
74  // to overwrite anyway).
75  for (i = 0; environ[i]; ++i) {
76    uintptr_t env_i = (uintptr_t) environ[i];
77    // Only move the value if it's actually in the way. This avoids
78    // leaking copies of the values if this function is called again.
79    if (page <= env_i && env_i < page_end) {
80      char* copy = strdup(environ[i]);
81      // Be paranoid. Check for allocation failure and bail out.
82      if (!copy)
83        return;
84      environ[i] = copy;
85    }
86  }
87  // Put the title in argv[0]. We have to zero out the space first since the
88  // kernel doesn't actually look for a null terminator unless we make the
89  // argument list longer than it started.
90  avail_size = page_end - (uintptr_t) g_main_argv[0];
91  memset(g_main_argv[0], 0, avail_size);
92  va_start(ap, fmt);
93  if (fmt[0] == '-') {
94    vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap);
95  } else {
96    size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0);
97    if (size < avail_size)
98      vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap);
99  }
100  va_end(ap);
101  g_main_argv[1] = NULL;
102}
103
104// A version of this built into glibc would not need this function, since
105// it could stash the argv pointer in __libc_start_main(). But we need it.
106void setproctitle_init(char** main_argv) {
107  if (g_main_argv)
108    return;
109
110  uintptr_t page_size = sysconf(_SC_PAGESIZE);
111  // Check that the argv array is in fact on the same page of memory
112  // as the environment array just as an added measure of protection.
113  if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size)
114    g_main_argv = main_argv;
115}
116