| 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 NULL 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 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 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 "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 namespace content { |
| 49 |
| 48 extern char** environ; | 50 extern char** environ; |
| 49 | 51 |
| 50 static char** g_main_argv = NULL; | 52 static char** g_main_argv = NULL; |
| 51 static char* g_orig_argv0 = NULL; | 53 static char* g_orig_argv0 = NULL; |
| 52 | 54 |
| 53 void setproctitle(const char* fmt, ...) { | 55 void SetProcTitle(const char* fmt, ...) { |
| 54 va_list ap; | 56 va_list ap; |
| 55 size_t i, avail_size; | 57 size_t i, avail_size; |
| 56 uintptr_t page_size, page, page_end; | 58 uintptr_t page_size, page, page_end; |
| 57 // Sanity check before we try and set the process title. | 59 // Sanity check before we try and set the process title. |
| 58 // The BSD version allows fmt == NULL to restore the original title. | 60 // The BSD version allows fmt == NULL to restore the original title. |
| 59 if (!g_main_argv || !environ || !fmt) | 61 if (!g_main_argv || !environ || !fmt) |
| 60 return; | 62 return; |
| 61 if (!g_orig_argv0) { | 63 if (!g_orig_argv0) { |
| 62 // Save the original argv[0]. | 64 // Save the original argv[0]. |
| 63 g_orig_argv0 = strdup(g_main_argv[0]); | 65 g_orig_argv0 = strdup(g_main_argv[0]); |
| (...skipping 32 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); | 98 size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0); |
| 97 if (size < avail_size) | 99 if (size < avail_size) |
| 98 vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap); | 100 vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap); |
| 99 } | 101 } |
| 100 va_end(ap); | 102 va_end(ap); |
| 101 g_main_argv[1] = NULL; | 103 g_main_argv[1] = NULL; |
| 102 } | 104 } |
| 103 | 105 |
| 104 // A version of this built into glibc would not need this function, since | 106 // 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. | 107 // it could stash the argv pointer in __libc_start_main(). But we need it. |
| 106 void setproctitle_init(const char** main_argv) { | 108 void SetProcTitleInit(const char** main_argv) { |
| 107 if (g_main_argv) | 109 if (g_main_argv) |
| 108 return; | 110 return; |
| 109 | 111 |
| 110 uintptr_t page_size = sysconf(_SC_PAGESIZE); | 112 uintptr_t page_size = sysconf(_SC_PAGESIZE); |
| 111 // Check that the argv array is in fact on the same page of memory | 113 // 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. | 114 // as the environment array just as an added measure of protection. |
| 113 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) | 115 if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size) |
| 114 g_main_argv = const_cast<char**>(main_argv); | 116 g_main_argv = const_cast<char**>(main_argv); |
| 115 } | 117 } |
| 118 |
| 119 } // namespace content |
| OLD | NEW |