OLD | NEW |
| (Empty) |
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 "content/common/set_process_title_linux.h" | |
41 | |
42 #include <stdarg.h> | |
43 #include <stddef.h> | |
44 #include <stdint.h> | |
45 #include <stdio.h> | |
46 #include <string.h> | |
47 #include <unistd.h> | |
48 | |
49 extern char** environ; | |
50 | |
51 static char** g_main_argv = NULL; | |
52 static char* g_orig_argv0 = NULL; | |
53 | |
54 void setproctitle(const char* fmt, ...) { | |
55 va_list ap; | |
56 size_t i, avail_size; | |
57 uintptr_t page_size, page, page_end; | |
58 // Sanity check before we try and set the process title. | |
59 // The BSD version allows fmt == NULL to restore the original title. | |
60 if (!g_main_argv || !environ || !fmt) | |
61 return; | |
62 if (!g_orig_argv0) { | |
63 // Save the original argv[0]. | |
64 g_orig_argv0 = strdup(g_main_argv[0]); | |
65 if (!g_orig_argv0) | |
66 return; | |
67 } | |
68 page_size = sysconf(_SC_PAGESIZE); | |
69 // Get the page on which the argument list and environment live. | |
70 page = (uintptr_t) g_main_argv[0]; | |
71 page -= page % page_size; | |
72 page_end = page + page_size; | |
73 // Move the environment out of the way. Note that we are moving the values, | |
74 // not the environment array itself (which may not be on the page we need | |
75 // to overwrite anyway). | |
76 for (i = 0; environ[i]; ++i) { | |
77 uintptr_t env_i = (uintptr_t) environ[i]; | |
78 // Only move the value if it's actually in the way. This avoids | |
79 // leaking copies of the values if this function is called again. | |
80 if (page <= env_i && env_i < page_end) { | |
81 char* copy = strdup(environ[i]); | |
82 // Be paranoid. Check for allocation failure and bail out. | |
83 if (!copy) | |
84 return; | |
85 environ[i] = copy; | |
86 } | |
87 } | |
88 // Put the title in argv[0]. We have to zero out the space first since the | |
89 // kernel doesn't actually look for a null terminator unless we make the | |
90 // argument list longer than it started. | |
91 avail_size = page_end - (uintptr_t) g_main_argv[0]; | |
92 memset(g_main_argv[0], 0, avail_size); | |
93 va_start(ap, fmt); | |
94 if (fmt[0] == '-') { | |
95 vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap); | |
96 } else { | |
97 size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0); | |
98 if (size < avail_size) | |
99 vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap); | |
100 } | |
101 va_end(ap); | |
102 g_main_argv[1] = NULL; | |
103 } | |
104 | |
105 // A version of this built into glibc would not need this function, since | |
106 // it could stash the argv pointer in __libc_start_main(). But we need it. | |
107 void setproctitle_init(const char** main_argv) { | |
108 if (g_main_argv) | |
109 return; | |
110 | |
111 uintptr_t page_size = sysconf(_SC_PAGESIZE); | |
112 // Check that the argv array is in fact on the same page of memory | |
113 // as the environment array just as an added measure of protection. | |
114 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) | |
115 g_main_argv = const_cast<char**>(main_argv); | |
116 } | |
OLD | NEW |