| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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] = NULL; |
| 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(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 = main_argv; | 114 g_main_argv = const_cast<char**>(main_argv); |
| 115 } | 115 } |
| OLD | NEW |