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