Skip to content

Commit 619e4f3

Browse files
Fix launcher issues + add new test
1 parent 82ef529 commit 619e4f3

5 files changed

Lines changed: 41 additions & 19 deletions

File tree

launcher/main.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <stdio.h>
44
#include <string.h>
55
#include <sys/stat.h>
6-
#include <sys/syslog.h>
76
#include <sys/wait.h>
87
#include <stdbool.h>
98
#include <stdlib.h>
@@ -12,19 +11,30 @@
1211

1312
#define SERVICE_NAME "com.notmarek.shell_integration.launcher"
1413

14+
void Log(const char* format, ...)
15+
{
16+
va_list args;
17+
va_start (args, format);
18+
vprintf (format, args);
19+
printf("\n");
20+
va_end (args);
21+
}
22+
1523
pid_t app_pid = -1;
1624
bool shouldExit = false;
1725

1826
LIPCcode stub(LIPC *lipc, const char *property, void *value, void*) {
19-
syslog(LOG_INFO, "Stub called for \"%s\" with value \"%s\"", property,
27+
Log("Stub called for \"%s\" with value \"%s\"", property,
2028
(char *)value);
2129
char *id = strtok((char *)value, ":");
22-
char *response = (char*) malloc(strlen(id) + 3 + 1);
23-
snprintf(response, strlen(id) + 3 + 1, "%s:0:", id);
24-
syslog(LOG_INFO, "Replying with %s", response);
25-
char *target = (char*) malloc(strlen(property) + 6 + 1);
26-
snprintf(target, strlen(property) + 6 + 1, "%sresult", property);
27-
syslog(LOG_INFO, "Replying with %s, %s", target, response);
30+
int bufSize = snprintf(NULL, 0, "%s:0:", id);
31+
char *response = (char*) malloc((unsigned long) bufSize);
32+
snprintf(response, (unsigned long) bufSize, "%s:0:", id);
33+
Log("Replying with %s", response);
34+
bufSize = snprintf(NULL, 0, "%sresult", property);
35+
char *target = (char*) malloc((unsigned long) bufSize);
36+
snprintf(target, (unsigned long) bufSize, "%sresult", property);
37+
Log("Replying with %s, %s", target, response);
2838
LipcSetStringProperty(lipc, "com.lab126.appmgrd", target, response);
2939
free(response);
3040
free(target);
@@ -37,13 +47,13 @@ LIPCcode pause_callback(LIPC* lipc, const char* property, void* value, void* dat
3747
}
3848

3949
LIPCcode unload_callback(LIPC* lipc, const char* property, void* value, void* data) {
40-
syslog(LOG_INFO, "Unloading shell integration launcher");
50+
Log("Unloading shell integration launcher");
4151

4252
// Kill the app if it's running
4353
if (app_pid > 0) {
4454
char command[48];
4555
sprintf(command, "/var/local/mkk/su -c \"kill -9 %i\"", app_pid);
46-
syslog(LOG_INFO, "Killing with: %s", command);
56+
Log("Killing with: %s", command);
4757
system(command);
4858
}
4959

@@ -99,7 +109,7 @@ LIPCcode go_callback(LIPC* lipc, const char* property, void* value, void* data)
99109
query[0] = 0;
100110
}
101111

102-
syslog(LOG_INFO, "Raw path: \"%s\"", rawFilePath);
112+
Log("Raw path: \"%s\"", rawFilePath);
103113

104114
// Parse the filePath as it is urlencoded
105115
char* filePath = urlDecode(rawFilePath);
@@ -110,7 +120,7 @@ LIPCcode go_callback(LIPC* lipc, const char* property, void* value, void* data)
110120
return stub(lipc, property, value, data);
111121
}
112122

113-
syslog(LOG_INFO, "Invoking app using \"%s\"", command);
123+
Log("Invoking app using \"%s\"", command);
114124
struct timespec time[2] = {{
115125
.tv_nsec = UTIME_NOW,
116126
.tv_sec = UTIME_NOW
@@ -125,7 +135,7 @@ LIPCcode go_callback(LIPC* lipc, const char* property, void* value, void* data)
125135
utimensat(0, filePath, time, 0);
126136
// Run the app on a background thread
127137
app_pid = fork();
128-
syslog(LOG_INFO, "Our app PID \"%d\"", app_pid);
138+
Log("Our app PID \"%d\"", app_pid);
129139
if (app_pid == 0) {
130140
// we are running as framework call gandalf for help
131141
execl("/var/local/mkk/su", "su", "-c", command, NULL); // su is first arg bc it expects to be run from a shell ($1 = name of command, duh)
@@ -138,8 +148,6 @@ LIPCcode go_callback(LIPC* lipc, const char* property, void* value, void* data)
138148

139149
#ifndef LAUNCHER_TESTING
140150
int main(void) {
141-
openlog(SERVICE_NAME, LOG_PID, LOG_DAEMON);
142-
143151
LIPCcode code;
144152
LIPC* lipc = LipcOpenEx(SERVICE_NAME, &code);
145153
if (code != LIPC_OK)
@@ -167,7 +175,7 @@ int main(void) {
167175
}
168176
}
169177

170-
syslog(LOG_INFO, "Running exit routine with PID: %d", app_pid);
178+
Log("Running exit routine with PID: %d", app_pid);
171179
LipcClose(lipc);
172180
return 0;
173181
}

tests/launcher.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "openlipc.h"
44
#include "unistd.h"
55
#include <sys/stat.h>
6-
#include <sys/syslog.h>
76
#include <sys/wait.h>
87
#include <stdbool.h>
98
#include "utils.h"

tests/launcher_test_stub.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "launcher.h"
2+
#include <assert.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
int main()
8+
{
9+
stub(NULL, "test", "testValue", NULL);
10+
return 0;
11+
}

tests/meson.build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ extractor_test_edgecases = executable('extractor_test_edgecases', ['extractor_te
7070
test('Test Extractor Edge Cases', extractor_test_edgecases, is_parallel : false)
7171

7272
launcher_test_generate_command = executable('launcher_test_generate_command', ['launcher_test_generate_command.c', '../launcher/main.c', '../utils/utils.c'], include_directories: ['../utils', '.'], dependencies: [ lipc_dep, scanner_stub_dep ], c_args: ['-DLAUNCHER_TESTING'])
73-
test('Test Launcher Generate Command', launcher_test_generate_command, is_parallel: false)
73+
test('Test Launcher Generate Command', launcher_test_generate_command, is_parallel: false)
74+
75+
76+
launcher_test_stub = executable('launcher_test_stub', ['launcher_test_stub.c', '../launcher/main.c', '../utils/utils.c'], include_directories: ['../utils', '.'], dependencies: [ lipc_dep, scanner_stub_dep ], c_args: ['-DLAUNCHER_TESTING'])
77+
test('Test Launcher Stub', launcher_test_stub, is_parallel: false)

utils/utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ char* urlDecode(char* raw)
2020
return result;
2121
}
2222

23-
char* buildCommand(const char* command, const char* sub)
23+
inline char* buildCommand(const char* command, const char* sub)
2424
{
2525
char* builtCommand = (char*) malloc(strlen(command) + strlen(sub) + 1);
2626
sprintf(builtCommand, command, sub);

0 commit comments

Comments
 (0)