@@ -318,56 +318,55 @@ session_bus_closed_cb (G_GNUC_UNUSED GDBusConnection *bus,
318318}
319319
320320static gboolean opt_sandbox_flags = 0 ;
321+ static gboolean opt_optional_sandbox_flags = 0 ;
322+
323+ static guint32
324+ sandbox_flag_from_string (const gchar * value )
325+ {
326+ if (strcmp (value , "share-display" ) == 0 )
327+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_DISPLAY ;
328+ else if (strcmp (value , "share-sound" ) == 0 )
329+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_SOUND ;
330+ else if (strcmp (value , "share-gpu" ) == 0 )
331+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_GPU ;
332+ else if (strcmp (value , "share-input" ) == 0 )
333+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_INPUT ;
334+ else if (strcmp (value , "share-usb" ) == 0 )
335+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_USB ;
336+ else if (strcmp (value , "share-kvm" ) == 0 )
337+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_KVM ;
338+ else if (strcmp (value , "share-shm" ) == 0 )
339+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_SHM ;
340+ else if (strcmp (value , "share-devices" ) == 0 )
341+ return FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_DEVICES ;
342+ else if (strcmp (value , "allow-dbus" ) == 0 )
343+ return FLATPAK_SPAWN_SANDBOX_FLAGS_ALLOW_DBUS ;
344+ else if (strcmp (value , "allow-a11y" ) == 0 )
345+ return FLATPAK_SPAWN_SANDBOX_FLAGS_ALLOW_A11Y ;
346+ else
347+ return FLATPAK_SPAWN_SANDBOX_FLAGS_NONE ;
348+ }
321349
322350static gboolean
323351sandbox_flag_callback (G_GNUC_UNUSED const gchar * option_name ,
324352 const gchar * value ,
325353 G_GNUC_UNUSED gpointer data ,
326354 GError * * error )
327355{
328- long val ;
329- char * end ;
330-
331- if (strcmp (value , "share-display" ) == 0 )
356+ guint32 flag = sandbox_flag_from_string (value );
357+ if (flag == FLATPAK_SPAWN_SANDBOX_FLAGS_NONE )
332358 {
333- opt_sandbox_flags |= FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_DISPLAY ;
334- return TRUE;
335- }
336-
337- if (strcmp (value , "share-sound" ) == 0 )
338- {
339- opt_sandbox_flags |= FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_SOUND ;
340- return TRUE;
359+ g_set_error (error , G_OPTION_ERROR , G_OPTION_ERROR_FAILED ,
360+ "Unknown sandbox flag %s" , value );
361+ return FALSE;
341362 }
342363
343- if (strcmp (value , "share-gpu" ) == 0 )
344- {
345- opt_sandbox_flags |= FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_GPU ;
346- return TRUE;
347- }
364+ opt_sandbox_flags |= flag ;
348365
349- if (strcmp (value , "allow-dbus" ) == 0 )
350- {
351- opt_sandbox_flags |= FLATPAK_SPAWN_SANDBOX_FLAGS_ALLOW_DBUS ;
352- return TRUE;
353- }
366+ if (strcmp (option_name , "--sandbox-flag-try" ) == 0 )
367+ opt_optional_sandbox_flags |= flag ;
354368
355- if (strcmp (value , "allow-a11y" ) == 0 )
356- {
357- opt_sandbox_flags |= FLATPAK_SPAWN_SANDBOX_FLAGS_ALLOW_A11Y ;
358- return TRUE;
359- }
360-
361- val = strtol (value , & end , 10 );
362- if (val > 0 && * end == 0 )
363- {
364- opt_sandbox_flags |= val ;
365- return TRUE;
366- }
367-
368- g_set_error (error , G_OPTION_ERROR , G_OPTION_ERROR_FAILED ,
369- "Unknown sandbox flag %s" , value );
370- return FALSE;
369+ return TRUE;
371370}
372371
373372static GPtrArray * sandbox_a11y_own_names = NULL ;
@@ -436,6 +435,24 @@ check_portal_version (const char *option, guint32 version_needed)
436435 }
437436}
438437
438+ static void
439+ check_sandbox_flag_support (const char * option , guint32 version_needed , guint32 flag )
440+ {
441+ if (!(opt_sandbox_flags && flag ))
442+ return ;
443+
444+ guint32 portal_version = get_portal_version ();
445+ if (portal_version >= version_needed )
446+ return ;
447+
448+ g_printerr ("--%s not supported by host portal version (need version %d, has %d)\n" , option , version_needed , portal_version );
449+
450+ if (opt_optional_sandbox_flags && flag )
451+ opt_sandbox_flags = opt_sandbox_flags & ~flag ;
452+ else
453+ exit (1 );
454+ }
455+
439456static guint32
440457get_portal_supports (void )
441458{
@@ -831,6 +848,7 @@ main (int argc,
831848 { "sandbox-expose-path-try" , 0 , 0 , G_OPTION_ARG_FILENAME_ARRAY , & opt_sandbox_expose_path_try , "Expose access to path if it exists" , "PATH" },
832849 { "sandbox-expose-path-ro-try" , 0 , 0 , G_OPTION_ARG_FILENAME_ARRAY , & opt_sandbox_expose_path_ro_try , "Expose readonly access to path if it exists" , "PATH" },
833850 { "sandbox-flag" , 0 , 0 , G_OPTION_ARG_CALLBACK , sandbox_flag_callback , "Enable sandbox flag" , "FLAG" },
851+ { "sandbox-flag-try" , 0 , 0 , G_OPTION_ARG_CALLBACK , sandbox_flag_callback , "Enable sandbox flag if aviable" , "FLAG" },
834852 { "sandbox-a11y-own-name" , 0 , 0 , G_OPTION_ARG_CALLBACK , sandbox_a11y_own_name_callback , "Allow owning the name on the a11y bus" , "DBUS_NAME" },
835853 { "host" , 0 , 0 , G_OPTION_ARG_NONE , & opt_host , "Start the command on the host" , NULL },
836854 { "directory" , 0 , 0 , G_OPTION_ARG_FILENAME , & opt_directory , "Working directory in which to run the command" , "DIR" },
@@ -1146,6 +1164,8 @@ main (int argc,
11461164
11471165 check_portal_version ("sandbox-flags" , 3 );
11481166
1167+ check_sandbox_flag_support ("device-flags" , 8 , FLATPAK_SPAWN_SANDBOX_FLAGS_SHARE_DEVICES );
1168+
11491169 g_variant_builder_add (& options_builder , "{s@v}" , "sandbox-flags" ,
11501170 g_variant_new_variant (g_variant_new_uint32 (opt_sandbox_flags )));
11511171 }
0 commit comments