-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.js
206 lines (172 loc) · 5.2 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
/*
* part of downgr8 by throwaway96
* licensed under AGPL 3.0 or later
* https://github.com/throwaway96/downgr8
*/
var pkgInfo = require("./package.json");
var serviceId = pkgInfo.name;
var Service = require("webos-service");
var service = new Service(serviceId, undefined, { idleTimer: 15 });
var child_process = require("child_process");
function isRoot() {
var uid = process.getuid();
console.log("uid: " + uid);
return uid === 0;
}
var keepAlive;
/* stop service from being killed when idle */
service.activityManager.create("keepAlive", function (activity) {
keepAlive = activity;
});
var subscriptions = {};
var getStatus = service.register("fakeusb/getStatus");
getStatus.on("request", function (message) {
try {
var sender = message.sender;
console.log("got a fakeusb/getStatus request from " + sender);
} catch (err) {
var message = "message" in err ? err.message : "<no message>";
console.warn(
"something went wrong in fakeusb/getStatus request handler: " + message
);
}
message.respond({
returnValue: true,
isAttached: true,
isAuthenticated: true,
});
if (message.isSubscription) {
subscriptions[message.uniqueToken] = message;
}
});
getStatus.on("cancel", function (message) {
try {
var sender = message.sender;
console.log("subscription to fakeusb/getStatus canceled by " + sender);
} catch (err) {
var message = "message" in err ? err.message : "<no message>";
console.warn(
"something went wrong in fakeusb/getStatus cancel handler: " + message
);
}
delete subscriptions[message.uniqueToken];
});
/* TODO: avoid hardcoding this path */
var elevatePath =
"/media/developer/apps/usr/palm/services/org.webosbrew.hbchannel.service/elevate-service";
service.register("elevate", function (message) {
if (isRoot()) {
message.respond({ returnValue: true, errorText: "already root" });
return;
}
var elevateCommand = elevatePath + " " + serviceId;
service.call(
"luna://org.webosbrew.hbchannel.service/exec",
{ command: elevateCommand },
function (innerMessage) {
var payload = innerMessage.payload;
var result = "returnValue" in payload ? payload["returnValue"] : false;
message.respond({ returnValue: result });
if (result) {
service.activityManager.complete(keepAlive);
process.exit(0);
}
}
);
});
function doPatchAsync(pid) {
var pidArg = typeof pid === "number" ? "'" + pid + "'" : '"$(pidof update)"';
var patchPath = __dirname + "/patch";
var command = "'" + patchPath + "' " + pidArg;
return new Promise(function (resolve, reject) {
child_process.exec(command, function (error, stdout, stderr) {
var exitStatus = error !== null ? error.code : 0;
resolve({
returnValue: exitStatus == 0,
exitStatus: exitStatus,
stdout: stdout,
stderr: stderr,
});
});
});
}
function doRestartAsync() {
var command = "restart securitymanager";
return new Promise(function (resolve, reject) {
child_process.exec(command, function (error, stdout, stderr) {
var exitStatus = error !== null ? error.code : 0;
resolve({
returnValue: exitStatus == 0,
exitStatus: exitStatus,
stdout: stdout,
stderr: stderr,
});
});
});
}
service.register("patch", function (message) {
if (isRoot()) {
doPatchAsync()
.then(function (value) {
message.respond(value);
})
.catch(function (err) {
console.error(err);
var errorText = "exception: " + err.message;
message.respond({ returnValue: false, errorText: errorText });
});
} else {
message.respond({ returnValue: false, errorText: "not root" });
}
});
service.register("restart", function (message) {
if (isRoot()) {
doRestartAsync()
.then(function (value) {
message.respond(value);
})
.catch(function (err) {
console.error(err);
var errorText = "exception: " + err.message;
message.respond({ returnValue: false, errorText: errorText });
});
} else {
message.respond({ returnValue: false, errorText: "not root" });
}
});
service.register("launchUpdate", function (message) {
/* TODO: clean this up */
service.call(
"luna://com.webos.service.update/setExpertMode",
{ mode: true },
function (response) {
var payload = response.payload;
var result = "returnValue" in payload ? payload["returnValue"] : false;
if (!result) {
message.respond({
returnValue: false,
errorText: "setExpertMode failed",
});
return;
}
service.call(
"luna://com.webos.applicationManager/launch",
{
id: "com.webos.app.softwareupdate",
params: { mode: "expert", flagUpdate: true },
},
function (innerResponse) {
var payload = innerResponse.payload;
var result =
"returnValue" in payload ? payload["returnValue"] : false;
if (result) {
message.respond({ returnValue: true });
} else {
message.respond({ returnValue: false, errorText: "launch failed" });
}
}
);
}
);
});