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,14 @@ 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
+ ) ;
86
95
87
96
this . connect ( 'startup' , this . startup . bind ( this ) ) ;
88
97
this . connect ( 'handle-local-options' , this . handle_local_options . bind ( this ) ) ;
@@ -230,8 +239,7 @@ const Application = GObject.registerClass(
230
239
handle_local_options ( _ , options ) {
231
240
if ( options . lookup ( 'launch-through-extension' ) ) {
232
241
try {
233
- const iface = extensiondbus . get ( ) ;
234
- iface . ServiceSync ( ) ;
242
+ this . launch_through_extension ( options ) ;
235
243
return 0 ;
236
244
} catch ( e ) {
237
245
logError ( e ) ;
@@ -247,6 +255,56 @@ const Application = GObject.registerClass(
247
255
return - 1 ;
248
256
}
249
257
258
+ launch_through_extension ( options ) {
259
+ const iface = extensiondbus . get ( ) ;
260
+ const [ pid ] = iface . ServiceSync ( ) ;
261
+
262
+ const attach_unit = options . lookup ( 'attach-unit' , 's' ) ;
263
+ if ( attach_unit ) {
264
+ Gio . DBus . session . call_sync (
265
+ 'org.freedesktop.systemd1' ,
266
+ '/org/freedesktop/systemd1' ,
267
+ 'org.freedesktop.systemd1.Manager' ,
268
+ 'AttachProcessesToUnit' ,
269
+ GLib . Variant . new_tuple ( [
270
+ GLib . Variant . new_string ( attach_unit ) ,
271
+ GLib . Variant . new_string ( '' ) ,
272
+ GLib . Variant . new_array (
273
+ new GLib . VariantType ( 'u' ) ,
274
+ [ GLib . Variant . new_uint32 ( parseInt ( pid , 10 ) ) ]
275
+ ) ,
276
+ ] ) ,
277
+ null ,
278
+ Gio . DBusCallFlags . NONE ,
279
+ - 1 ,
280
+ null
281
+ ) ;
282
+ }
283
+
284
+ const notify_socket_path = GLib . getenv ( 'NOTIFY_SOCKET' ) ;
285
+ if ( notify_socket_path ) {
286
+ const abstract = notify_socket_path . startsWith ( '@' ) ;
287
+
288
+ const address = new Gio . UnixSocketAddress ( {
289
+ path : abstract ? notify_socket_path . substr ( 1 ) : notify_socket_path ,
290
+ abstract,
291
+ } ) ;
292
+
293
+ const socket = Gio . Socket . new (
294
+ Gio . SocketFamily . UNIX ,
295
+ Gio . SocketType . DATAGRAM ,
296
+ Gio . SocketProtocol . DEFAULT
297
+ ) ;
298
+
299
+ try {
300
+ socket . connect ( address , null ) ;
301
+ socket . send ( ByteArray . fromString ( `MAINPID=${ pid } ` ) , null ) ;
302
+ } finally {
303
+ socket . close ( ) ;
304
+ }
305
+ }
306
+ }
307
+
250
308
preferences ( ) {
251
309
if ( this . prefs_dialog === null ) {
252
310
this . prefs_dialog = new PrefsDialog ( {
0 commit comments