OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file implements BSD-style setproctitle() for Linux. | 5 // This file implements BSD-style setproctitle() for Linux. |
6 // It is written such that it can easily be compiled outside Chromium. | 6 // It is written such that it can easily be compiled outside Chromium. |
7 // | 7 // |
8 // The Linux kernel sets up two locations in memory to pass arguments and | 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 | 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(), | 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 | 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 | 12 // of these arrays are terminated by a nullptr pointer; the environment array is |
13 // also followed by some empty space to allow additional variables to be added. | 13 // also followed by some empty space to allow additional variables to be added. |
14 // | 14 // |
15 // These arrays contain pointers to a second location in memory, where the | 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, | 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 | 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 | 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. | 19 // end of the storage potentially available to store the process title. |
20 // | 20 // |
21 // When the kernel reads the command line arguments for a process, it looks at | 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 | 22 // the range of memory within this page that it initially used for the argument |
(...skipping 17 matching lines...) Expand all Loading... |
40 #include "content/common/set_process_title_linux.h" | 40 #include "content/common/set_process_title_linux.h" |
41 | 41 |
42 #include <stdarg.h> | 42 #include <stdarg.h> |
43 #include <stdint.h> | 43 #include <stdint.h> |
44 #include <stdio.h> | 44 #include <stdio.h> |
45 #include <string.h> | 45 #include <string.h> |
46 #include <unistd.h> | 46 #include <unistd.h> |
47 | 47 |
48 extern char** environ; | 48 extern char** environ; |
49 | 49 |
50 static char** g_main_argv = NULL; | 50 static char** g_main_argv = nullptr; |
51 static char* g_orig_argv0 = NULL; | 51 static char* g_orig_argv0 = nullptr; |
52 | 52 |
53 void setproctitle(const char* fmt, ...) { | 53 void setproctitle(const char* fmt, ...) { |
54 va_list ap; | 54 va_list ap; |
55 size_t i, avail_size; | 55 size_t i, avail_size; |
56 uintptr_t page_size, page, page_end; | 56 uintptr_t page_size, page, page_end; |
57 // Sanity check before we try and set the process title. | 57 // Sanity check before we try and set the process title. |
58 // The BSD version allows fmt == NULL to restore the original title. | 58 // The BSD version allows fmt == nullptr to restore the original title. |
59 if (!g_main_argv || !environ || !fmt) | 59 if (!g_main_argv || !environ || !fmt) |
60 return; | 60 return; |
61 if (!g_orig_argv0) { | 61 if (!g_orig_argv0) { |
62 // Save the original argv[0]. | 62 // Save the original argv[0]. |
63 g_orig_argv0 = strdup(g_main_argv[0]); | 63 g_orig_argv0 = strdup(g_main_argv[0]); |
64 if (!g_orig_argv0) | 64 if (!g_orig_argv0) |
65 return; | 65 return; |
66 } | 66 } |
67 page_size = sysconf(_SC_PAGESIZE); | 67 page_size = sysconf(_SC_PAGESIZE); |
68 // Get the page on which the argument list and environment live. | 68 // Get the page on which the argument list and environment live. |
(...skipping 22 matching lines...) Expand all Loading... |
91 memset(g_main_argv[0], 0, avail_size); | 91 memset(g_main_argv[0], 0, avail_size); |
92 va_start(ap, fmt); | 92 va_start(ap, fmt); |
93 if (fmt[0] == '-') { | 93 if (fmt[0] == '-') { |
94 vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap); | 94 vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap); |
95 } else { | 95 } else { |
96 size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0); | 96 size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0); |
97 if (size < avail_size) | 97 if (size < avail_size) |
98 vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap); | 98 vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap); |
99 } | 99 } |
100 va_end(ap); | 100 va_end(ap); |
101 g_main_argv[1] = NULL; | 101 g_main_argv[1] = nullptr; |
102 } | 102 } |
103 | 103 |
104 // A version of this built into glibc would not need this function, since | 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. | 105 // it could stash the argv pointer in __libc_start_main(). But we need it. |
106 void setproctitle_init(const char** main_argv) { | 106 void setproctitle_init(const char** main_argv) { |
107 if (g_main_argv) | 107 if (g_main_argv) |
108 return; | 108 return; |
109 | 109 |
110 uintptr_t page_size = sysconf(_SC_PAGESIZE); | 110 uintptr_t page_size = sysconf(_SC_PAGESIZE); |
111 // Check that the argv array is in fact on the same page of memory | 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. | 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) | 113 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) |
114 g_main_argv = const_cast<char**>(main_argv); | 114 g_main_argv = const_cast<char**>(main_argv); |
115 } | 115 } |
OLD | NEW |