| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import ctypes |
| 6 import ctypes.util |
| 7 import sys |
| 8 |
| 9 |
| 10 _CACHED_CMDLINE_LENGTH = None |
| 11 |
| 12 |
| 13 def set_command_line(cmdline): |
| 14 """Replaces the commandline of this process as seen by ps.""" |
| 15 |
| 16 # Get the current commandline. |
| 17 argc = ctypes.c_int() |
| 18 argv = ctypes.POINTER(ctypes.c_char_p)() |
| 19 ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv)) |
| 20 |
| 21 global _CACHED_CMDLINE_LENGTH |
| 22 if _CACHED_CMDLINE_LENGTH is None: |
| 23 # Each argument is terminated by a null-byte, so the length of the whole |
| 24 # thing in memory is the sum of all the argument byte-lengths, plus 1 null |
| 25 # byte for each. |
| 26 _CACHED_CMDLINE_LENGTH = sum( |
| 27 len(argv[i]) for i in xrange(0, argc.value)) + argc.value |
| 28 |
| 29 # Pad the cmdline string to the required length. If it's longer than the |
| 30 # current commandline, truncate it. |
| 31 if len(cmdline) >= _CACHED_CMDLINE_LENGTH: |
| 32 new_cmdline = ctypes.c_char_p(cmdline[:_CACHED_CMDLINE_LENGTH-1] + '\0') |
| 33 else: |
| 34 new_cmdline = ctypes.c_char_p(cmdline.ljust(_CACHED_CMDLINE_LENGTH, '\0')) |
| 35 |
| 36 # Replace the old commandline. |
| 37 libc = ctypes.CDLL(ctypes.util.find_library('c')) |
| 38 libc.memcpy(argv.contents, new_cmdline, _CACHED_CMDLINE_LENGTH) |
| OLD | NEW |