FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

added read and write support for process.title Darwin · sgithens/node@758f12f · GitHub

Repository navigation

Commit 758f12f

Browse files
authored andcommitted
added read and write support for process.title Darwin
This will only manipulate the OS X-level process name, not the title shown in e.g. ps.
1 parent 083ee0f commit 758f12f

5 files changed

Lines changed: 201 additions & 4 deletions

File tree

‎LICENSE‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ are:
3333
and has copyrights from Jérémy Lal, Ryan Tomayko, Dominic Baggott, Ash
3434
Berlin, and Joey Mazzarelli.
3535

36+
- src/platform_darwin_proctitle.cc, has code taken from the Chromium
37+
project copyright Google Inc. and released with the BSD license.
38+
3639

3740
Node's license follows:
3841

‎src/platform_darwin.cc‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77
#include <limits.h> /* PATH_MAX */
88

99
namespace node {
10-
10+
static char *process_title;
1111

1212
char** OS::SetupArgs(int argc, char *argv[]) {
13+
process_title = argc ? strdup(argv[0]) : NULL;
1314
return argv;
1415
}
1516

1617

17-
void OS::SetProcessTitle(char *title) {
18-
;
19-
}
18+
// OS::SetProcessTitle implemented in platform_darwin_proctitle.cc
19+
} // namespace node
20+
#include "platform_darwin_proctitle.cc"
21+
namespace node {
2022

2123

2224
const char* OS::GetProcessTitle(int *len) {
25+
if (process_title) {
26+
*len = strlen(process_title);
27+
return process_title;
28+
}
2329
*len = 0;
2430
return NULL;
2531
}

‎src/platform_darwin_proctitle.cc‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2010, Google Inc.
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are
6+
// met:
7+
//
8+
// * Redistributions of source code must retain the above copyright
9+
// notice, this list of conditions and the following disclaimer.
10+
// * Redistributions in binary form must reproduce the above
11+
// copyright notice, this list of conditions and the following disclaimer
12+
// in the documentation and/or other materials provided with the
13+
// distribution.
14+
// * Neither the name of Google Inc. nor the names of its
15+
// contributors may be used to endorse or promote products derived from
16+
// this software without specific prior written permission.
17+
//
18+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
//
30+
// This code emanates from the Chromium project:
31+
// http://src.chromium.org/viewvc/chrome/trunk/src/base/mac_util.mm
32+
33+
#include <CoreFoundation/CoreFoundation.h>
34+
#include <Carbon/Carbon.h> // Oh noes! See discussion blow at GetCurrentProcess
35+
#ifndef NDEBUG
36+
#include <err.h>
37+
#endif
38+
39+
namespace node {
40+
41+
void OS::SetProcessTitle(char *title) {
42+
static int symbol_lookup_status = 0; // 1=ok, 2=unavailable
43+
if (symbol_lookup_status == 2) {
44+
// feature is unavailable
45+
return;
46+
}
47+
48+
if (process_title) free(process_title);
49+
process_title = strdup(title);
50+
51+
// Warning: here be dragons! This is SPI reverse-engineered from WebKit's
52+
// plugin host, and could break at any time (although realistically it's only
53+
// likely to break in a new major release).
54+
// When 10.7 is available, check that this still works, and update this
55+
// comment for 10.8.
56+
57+
// Private CFType used in these LaunchServices calls.
58+
typedef CFTypeRef PrivateLSASN;
59+
typedef PrivateLSASN (*LSGetCurrentApplicationASNType)();
60+
typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN,
61+
CFStringRef,
62+
CFStringRef,
63+
CFDictionaryRef*);
64+
65+
static LSGetCurrentApplicationASNType ls_get_current_application_asn_func =
66+
NULL;
67+
static LSSetApplicationInformationItemType
68+
ls_set_application_information_item_func = NULL;
69+
static CFStringRef ls_display_name_key = NULL;
70+
if (!symbol_lookup_status) {
71+
CFBundleRef launch_services_bundle =
72+
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
73+
if (!launch_services_bundle) {
74+
#ifndef NDEBUG
75+
warnx("failed to look up LaunchServices bundle");
76+
#endif
77+
symbol_lookup_status = 2;
78+
return;
79+
}
80+
81+
ls_get_current_application_asn_func =
82+
reinterpret_cast<LSGetCurrentApplicationASNType>(
83+
CFBundleGetFunctionPointerForName(
84+
launch_services_bundle, CFSTR("_LSGetCurrentApplicationASN")));
85+
if (!ls_get_current_application_asn_func) {
86+
#ifndef NDEBUG
87+
warnx("could not find _LSGetCurrentApplicationASN");
88+
#endif
89+
symbol_lookup_status = 2;
90+
return;
91+
}
92+
93+
ls_set_application_information_item_func =
94+
reinterpret_cast<LSSetApplicationInformationItemType>(
95+
CFBundleGetFunctionPointerForName(
96+
launch_services_bundle,
97+
CFSTR("_LSSetApplicationInformationItem")));
98+
if (!ls_set_application_information_item_func) {
99+
#ifndef NDEBUG
100+
warnx("Could not find _LSSetApplicationInformationItem");
101+
#endif
102+
symbol_lookup_status = 2;
103+
return;
104+
}
105+
106+
const CFStringRef* key_pointer = reinterpret_cast<const CFStringRef*>(
107+
CFBundleGetDataPointerForName(launch_services_bundle,
108+
CFSTR("_kLSDisplayNameKey")));
109+
ls_display_name_key = key_pointer ? *key_pointer : NULL;
110+
if (!ls_display_name_key) {
111+
#ifndef NDEBUG
112+
warnx("Could not find _kLSDisplayNameKey");
113+
#endif
114+
symbol_lookup_status = 2;
115+
return;
116+
}
117+
118+
// Internally, this call relies on the Mach ports that are started up by the
119+
// Carbon Process Manager. In debug builds this usually happens due to how
120+
// the logging layers are started up; but in release, it isn't started in as
121+
// much of a defined order. So if the symbols had to be loaded, go ahead
122+
// and force a call to make sure the manager has been initialized and hence
123+
// the ports are opened.
124+
ProcessSerialNumber psn;
125+
GetCurrentProcess(&psn);
126+
symbol_lookup_status = 1; // 1=ok
127+
}
128+
129+
PrivateLSASN asn = ls_get_current_application_asn_func();
130+
// Constant used by WebKit; what exactly it means is unknown.
131+
const int magic_session_constant = -2;
132+
CFStringRef process_name =
133+
CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
134+
OSErr err =
135+
ls_set_application_information_item_func(magic_session_constant, asn,
136+
ls_display_name_key,
137+
process_name,
138+
NULL /* optional out param */);
139+
#ifndef NDEBUG
140+
if (err) {
141+
warnx("Call LSSetApplicationInformationItem failed");
142+
}
143+
#endif
144+
}
145+
146+
} // namespace node
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var common = require("../common");
2+
var assert = common.assert;
3+
var spawn = require('child_process').spawn;
4+
5+
if (process.title === '') {
6+
console.log('skipping test -- not implemented for the host platform');
7+
return;
8+
}
9+
10+
// disabled because of two things
11+
// - not tested on linux (can ps show process title on Linux?)
12+
// - unable to verify effect on Darwin/OS X (only avail through GUI tools AFAIK)
13+
14+
function verifyProcessName(str, callback) {
15+
process.title = str;
16+
var buf = '';
17+
ps = spawn('ps');
18+
ps.stdout.setEncoding('utf8');
19+
ps.stdout.addListener("data", function (s) { buf += s; });
20+
ps.addListener("exit", function (c) {
21+
try {
22+
assert.equal(0, c);
23+
assert.ok(new RegExp(process.pid+' ', 'm').test(buf));
24+
assert.ok(new RegExp(str, 'm').test(buf));
25+
callback();
26+
} catch (err) {
27+
callback(err);
28+
}
29+
});
30+
}
31+
32+
verifyProcessName("3kd023mslkfp--unique-string--sksdf", function(err){
33+
if (err) throw err;
34+
console.log('title is now %j', process.title);
35+
verifyProcessName("3kd023mslxxx--unique-string--xxx", function(err){
36+
if (err) throw err;
37+
console.log('title is now %j', process.title);
38+
});
39+
});

‎wscript‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ def configure(conf):
252252
conf.env.append_value ('CCFLAGS', threadflags)
253253
conf.env.append_value ('CXXFLAGS', threadflags)
254254
conf.env.append_value ('LINKFLAGS', threadflags)
255+
if sys.platform.startswith("darwin"):
256+
# used by platform_darwin_*.cc
257+
conf.env.append_value('LINKFLAGS', ['-framework','Carbon'])
255258

256259
conf.env.append_value("CCFLAGS", "-DX_STACKSIZE=%d" % (1024*64))
257260

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL