| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 environ[i] = copy; | 84 environ[i] = copy; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 // Put the title in argv[0]. We have to zero out the space first since the | 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 | 88 // kernel doesn't actually look for a null terminator unless we make the |
| 89 // argument list longer than it started. | 89 // argument list longer than it started. |
| 90 avail_size = page_end - (uintptr_t) g_main_argv[0]; | 90 avail_size = page_end - (uintptr_t) g_main_argv[0]; |
| 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, 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] = 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(char** main_argv) { |
| 107 uintptr_t page_size = sysconf(_SC_PAGESIZE); | 107 uintptr_t page_size = sysconf(_SC_PAGESIZE); |
| 108 // Check that the argv array is in fact on the same page of memory | 108 // Check that the argv array is in fact on the same page of memory |
| 109 // as the environment array just as an added measure of protection. | 109 // as the environment array just as an added measure of protection. |
| 110 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) | 110 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) |
| 111 g_main_argv = main_argv; | 111 g_main_argv = main_argv; |
| 112 } | 112 } |
| OLD | NEW |