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>
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+
1523pid_t app_pid = -1 ;
1624bool shouldExit = false;
1725
1826LIPCcode 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
3949LIPCcode 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
140150int 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}
0 commit comments