Rizin
unix-like reverse engineering framework and cli tools
proctitle.c File Reference
#include "uv.h"
#include "internal.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  uv__process_title
 

Functions

void uv__set_process_title (const char *title)
 
static void init_process_title_mutex_once (void)
 
char ** uv_setup_args (int argc, char **argv)
 
int uv_set_process_title (const char *title)
 
int uv_get_process_title (char *buffer, size_t size)
 
void uv__process_title_cleanup (void)
 

Variables

static uv_mutex_t process_title_mutex
 
static uv_once_t process_title_mutex_once = UV_ONCE_INIT
 
static struct uv__process_title process_title
 
static void * args_mem
 

Function Documentation

◆ init_process_title_mutex_once()

static void init_process_title_mutex_once ( void  )
static

Definition at line 41 of file proctitle.c.

41  {
43 }
static uv_mutex_t process_title_mutex
Definition: proctitle.c:35
UV_EXTERN int uv_mutex_init(uv_mutex_t *handle)
Definition: thread.c:282

References process_title_mutex, and uv_mutex_init().

Referenced by uv_get_process_title(), and uv_set_process_title().

◆ uv__process_title_cleanup()

void uv__process_title_cleanup ( void  )

Definition at line 156 of file proctitle.c.

156  {
157  uv__free(args_mem); /* Keep valgrind happy. */
158  args_mem = NULL;
159 }
#define NULL
Definition: cris-opc.c:27
static void * args_mem
Definition: proctitle.c:38
void uv__free(void *ptr)
Definition: uv-common.c:81

References args_mem, NULL, and uv__free().

◆ uv__set_process_title()

void uv__set_process_title ( const char *  title)

Definition at line 52 of file darwin-proctitle.c.

52  {
53 #if TARGET_OS_IPHONE
54  return uv__pthread_setname_np(title);
55 #else
56  CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
57  const char*,
59  CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
60  void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
61  void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
62  CFTypeRef (*pLSGetCurrentApplicationASN)(void);
63  OSStatus (*pLSSetApplicationInformationItem)(int,
64  CFTypeRef,
68  void* application_services_handle;
69  void* core_foundation_handle;
70  CFBundleRef launch_services_bundle;
71  CFStringRef* display_name_key;
72  CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
73  CFBundleRef (*pCFBundleGetMainBundle)(void);
74  CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
75  void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
76  void*);
77  CFTypeRef asn;
78  int err;
79 
80  err = UV_ENOENT;
81  application_services_handle = dlopen("/System/Library/Frameworks/"
82  "ApplicationServices.framework/"
83  "Versions/A/ApplicationServices",
84  RTLD_LAZY | RTLD_LOCAL);
85  core_foundation_handle = dlopen("/System/Library/Frameworks/"
86  "CoreFoundation.framework/"
87  "Versions/A/CoreFoundation",
88  RTLD_LAZY | RTLD_LOCAL);
89 
90  if (application_services_handle == NULL || core_foundation_handle == NULL)
91  goto out;
92 
93  *(void **)(&pCFStringCreateWithCString) =
94  dlsym(core_foundation_handle, "CFStringCreateWithCString");
95  *(void **)(&pCFBundleGetBundleWithIdentifier) =
96  dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
97  *(void **)(&pCFBundleGetDataPointerForName) =
98  dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
99  *(void **)(&pCFBundleGetFunctionPointerForName) =
100  dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
101 
102  if (pCFStringCreateWithCString == NULL ||
103  pCFBundleGetBundleWithIdentifier == NULL ||
104  pCFBundleGetDataPointerForName == NULL ||
105  pCFBundleGetFunctionPointerForName == NULL) {
106  goto out;
107  }
108 
109 #define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
110 
111  launch_services_bundle =
112  pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
113 
114  if (launch_services_bundle == NULL)
115  goto out;
116 
117  *(void **)(&pLSGetCurrentApplicationASN) =
118  pCFBundleGetFunctionPointerForName(launch_services_bundle,
119  S("_LSGetCurrentApplicationASN"));
120 
121  if (pLSGetCurrentApplicationASN == NULL)
122  goto out;
123 
124  *(void **)(&pLSSetApplicationInformationItem) =
125  pCFBundleGetFunctionPointerForName(launch_services_bundle,
126  S("_LSSetApplicationInformationItem"));
127 
128  if (pLSSetApplicationInformationItem == NULL)
129  goto out;
130 
131  display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
132  S("_kLSDisplayNameKey"));
133 
134  if (display_name_key == NULL || *display_name_key == NULL)
135  goto out;
136 
137  *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
138  "CFBundleGetInfoDictionary");
139  *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
140  "CFBundleGetMainBundle");
141  if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
142  goto out;
143 
144  *(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
145  launch_services_bundle,
146  S("_LSApplicationCheckIn"));
147 
148  if (pLSApplicationCheckIn == NULL)
149  goto out;
150 
151  *(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
152  pCFBundleGetFunctionPointerForName(
153  launch_services_bundle,
154  S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
155 
156  if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)
157  goto out;
158 
159  pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
160 
161  /* Check into process manager?! */
162  pLSApplicationCheckIn(-2,
163  pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
164 
165  asn = pLSGetCurrentApplicationASN();
166 
167  err = UV_EBUSY;
168  if (asn == NULL)
169  goto out;
170 
171  err = UV_EINVAL;
172  if (pLSSetApplicationInformationItem(-2, /* Magic value. */
173  asn,
174  *display_name_key,
175  S(title),
176  NULL) != noErr) {
177  goto out;
178  }
179 
180  uv__pthread_setname_np(title); /* Don't care if it fails. */
181  err = 0;
182 
183 out:
184  if (core_foundation_handle != NULL)
185  dlclose(core_foundation_handle);
186 
187  if (application_services_handle != NULL)
188  dlclose(application_services_handle);
189 
190  return err;
191 #endif /* !TARGET_OS_IPHONE */
192 }
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int uv__pthread_setname_np(const char *name)
#define S(s)
void * CFBundleRef
Definition: darwin-stub.h:45
static const OSStatus noErr
Definition: darwin-stub.h:89
void * CFDictionaryRef
Definition: darwin-stub.h:47
int OSStatus
Definition: darwin-stub.h:35
void * CFStringRef
Definition: darwin-stub.h:52
void * CFAllocatorRef
Definition: darwin-stub.h:43
void * CFTypeRef
Definition: darwin-stub.h:53
unsigned CFStringEncoding
Definition: darwin-stub.h:42
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28

References err, int, noErr, NULL, out, S, and uv__pthread_setname_np().

◆ uv_get_process_title()

int uv_get_process_title ( char *  buffer,
size_t  size 
)

Definition at line 129 of file proctitle.c.

129  {
130  if (buffer == NULL || size == 0)
131  return UV_EINVAL;
132 
133  /* If uv_setup_args wasn't called or failed, we can't continue. */
134  if (args_mem == NULL)
135  return UV_ENOBUFS;
136 
139 
140  if (size <= process_title.len) {
142  return UV_ENOBUFS;
143  }
144 
145  if (process_title.len != 0)
147 
148  buffer[process_title.len] = '\0';
149 
151 
152  return 0;
153 }
voidpf void uLong size
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void init_process_title_mutex_once(void)
Definition: proctitle.c:41
static uv_once_t process_title_mutex_once
Definition: proctitle.c:36
static struct uv__process_title process_title
Definition: proctitle.c:37
Definition: buffer.h:15
UV_EXTERN void uv_mutex_lock(uv_mutex_t *handle)
Definition: thread.c:330
UV_EXTERN void uv_once(uv_once_t *guard, void(*callback)(void))
Definition: thread.c:419
UV_EXTERN void uv_mutex_unlock(uv_mutex_t *handle)
Definition: thread.c:350

References args_mem, init_process_title_mutex_once(), uv__process_title::len, memcpy(), NULL, process_title, process_title_mutex, process_title_mutex_once, uv__process_title::str, uv_mutex_lock(), uv_mutex_unlock(), and uv_once().

◆ uv_set_process_title()

int uv_set_process_title ( const char *  title)

Definition at line 99 of file proctitle.c.

99  {
100  struct uv__process_title* pt;
101  size_t len;
102 
103  /* If uv_setup_args wasn't called or failed, we can't continue. */
104  if (args_mem == NULL)
105  return UV_ENOBUFS;
106 
107  pt = &process_title;
108  len = strlen(title);
109 
112 
113  if (len >= pt->cap) {
114  len = 0;
115  if (pt->cap > 0)
116  len = pt->cap - 1;
117  }
118 
119  memcpy(pt->str, title, len);
120  memset(pt->str + len, '\0', pt->cap - len);
121  pt->len = len;
122 
124 
125  return 0;
126 }
size_t len
Definition: 6502dis.c:15
return memset(p, 0, total)

References args_mem, uv__process_title::cap, init_process_title_mutex_once(), len, uv__process_title::len, memcpy(), memset(), NULL, process_title, process_title_mutex, process_title_mutex_once, uv__process_title::str, uv_mutex_lock(), uv_mutex_unlock(), and uv_once().

◆ uv_setup_args()

char** uv_setup_args ( int  argc,
char **  argv 
)

Definition at line 46 of file proctitle.c.

46  {
47  struct uv__process_title pt;
48  char** new_argv;
49  size_t size;
50  char* s;
51  int i;
52 
53  if (argc <= 0)
54  return argv;
55 
56  pt.str = argv[0];
57  pt.len = strlen(argv[0]);
58  pt.cap = pt.len + 1;
59 
60  /* Calculate how much memory we need for the argv strings. */
61  size = pt.cap;
62  for (i = 1; i < argc; i++)
63  size += strlen(argv[i]) + 1;
64 
65  /* Add space for the argv pointers. */
66  size += (argc + 1) * sizeof(char*);
67 
68  new_argv = uv__malloc(size);
69  if (new_argv == NULL)
70  return argv;
71 
72  /* Copy over the strings and set up the pointer table. */
73  i = 0;
74  s = (char*) &new_argv[argc + 1];
75  size = pt.cap;
76  goto loop;
77 
78  for (/* empty */; i < argc; i++) {
79  size = strlen(argv[i]) + 1;
80  loop:
81  memcpy(s, argv[i], size);
82  new_argv[i] = s;
83  s += size;
84  }
85  new_argv[i] = NULL;
86 
87  /* argv is not adjacent on z/os, we use just argv[0] on that platform. */
88 #ifndef __MVS__
89  pt.cap = argv[i - 1] + size - argv[0];
90 #endif
91 
92  args_mem = new_argv;
93  process_title = pt;
94 
95  return new_argv;
96 }
lzma_index ** i
Definition: index.h:629
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static RzSocket * s
Definition: rtr.c:28
uv_loop_t * loop
Definition: main.c:7
void * uv__malloc(size_t size)
Definition: uv-common.c:75

References args_mem, argv, uv__process_title::cap, i, uv__process_title::len, loop, memcpy(), NULL, process_title, s, uv__process_title::str, and uv__malloc().

Variable Documentation

◆ args_mem

void* args_mem
static

◆ process_title

struct uv__process_title process_title
static

Definition at line 36 of file proctitle.c.

Referenced by uv_get_process_title(), uv_set_process_title(), and uv_setup_args().

◆ process_title_mutex

uv_mutex_t process_title_mutex
static

◆ process_title_mutex_once

uv_once_t process_title_mutex_once = UV_ONCE_INIT
static

Definition at line 36 of file proctitle.c.

Referenced by uv_get_process_title(), and uv_set_process_title().