19
19
20
20
'use strict' ;
21
21
22
+ const ByteArray = imports . byteArray ;
22
23
const System = imports . system ;
23
24
24
25
const Me = imports . misc . extensionUtils . getCurrentExtension ( ) ;
@@ -83,6 +84,22 @@ const Application = GObject.registerClass(
83
84
'Ask the extension to launch the app' ,
84
85
null
85
86
) ;
87
+ this . add_main_option (
88
+ 'attach-unit' ,
89
+ 0 ,
90
+ GLib . OptionFlags . NONE ,
91
+ GLib . OptionArg . STRING ,
92
+ 'Attach launched application process to the specified systemd unit' ,
93
+ 'UNIT_NAME'
94
+ ) ;
95
+ this . add_main_option (
96
+ 'pid-file' ,
97
+ 0 ,
98
+ GLib . OptionFlags . NONE ,
99
+ GLib . OptionArg . FILENAME ,
100
+ 'Write launched application process id to the specified file' ,
101
+ 'FILE_NAME'
102
+ ) ;
86
103
87
104
this . connect ( 'startup' , this . startup . bind ( this ) ) ;
88
105
this . connect ( 'handle-local-options' , this . handle_local_options . bind ( this ) ) ;
@@ -230,8 +247,7 @@ const Application = GObject.registerClass(
230
247
handle_local_options ( _ , options ) {
231
248
if ( options . lookup ( 'launch-through-extension' ) ) {
232
249
try {
233
- const iface = extensiondbus . get ( ) ;
234
- iface . ServiceSync ( ) ;
250
+ this . launch_through_extension ( options ) ;
235
251
return 0 ;
236
252
} catch ( e ) {
237
253
logError ( e ) ;
@@ -247,6 +263,50 @@ const Application = GObject.registerClass(
247
263
return - 1 ;
248
264
}
249
265
266
+ launch_through_extension ( options ) {
267
+ const iface = extensiondbus . get ( ) ;
268
+ const [ pid ] = iface . ServiceSync ( ) ;
269
+
270
+ const attach_unit = options . lookup ( 'attach-unit' , 's' ) ;
271
+ if ( attach_unit ) {
272
+ Gio . DBus . session . call_sync (
273
+ 'org.freedesktop.systemd1' ,
274
+ '/org/freedesktop/systemd1' ,
275
+ 'org.freedesktop.systemd1.Manager' ,
276
+ 'AttachProcessesToUnit' ,
277
+ GLib . Variant . new_tuple ( [
278
+ GLib . Variant . new_string ( attach_unit ) ,
279
+ GLib . Variant . new_string ( '' ) ,
280
+ GLib . Variant . new_array (
281
+ new GLib . VariantType ( 'u' ) ,
282
+ [ GLib . Variant . new_uint32 ( parseInt ( pid , 10 ) ) ]
283
+ ) ,
284
+ ] ) ,
285
+ null ,
286
+ Gio . DBusCallFlags . NONE ,
287
+ - 1 ,
288
+ null
289
+ ) ;
290
+ }
291
+
292
+ const pid_file_path = options . lookup ( 'pid-file' , 'ay' ) ;
293
+ if ( pid_file_path ) {
294
+ const pid_file = Gio . File . new_for_commandline_arg (
295
+ ByteArray . toString ( pid_file_path )
296
+ ) ;
297
+
298
+ GLib . mkdir_with_parents ( pid_file . get_parent ( ) . get_path ( ) , 0o700 ) ;
299
+
300
+ pid_file . replace_contents (
301
+ ByteArray . fromString ( `${ pid } ` ) ,
302
+ null ,
303
+ false ,
304
+ Gio . FileCreateFlags . PRIVATE | Gio . FileCreateFlags . REPLACE_DESTINATION ,
305
+ null
306
+ ) ;
307
+ }
308
+ }
309
+
250
310
preferences ( ) {
251
311
if ( this . prefs_dialog === null ) {
252
312
this . prefs_dialog = new PrefsDialog ( {
0 commit comments