5656#endif
5757#include "squashfuse_dlopen.h"
5858
59+ /* Exit status to use when launching an AppImage fails.
60+ * For applications that assign meanings to exit status codes (e.g. rsync),
61+ * we avoid "cluttering" pre-defined exit status codes by using 127 which
62+ * is known to alias an application exit status and also known as launcher
63+ * error, see SYSTEM(3POSIX).
64+ */
65+ #define EXIT_EXECERROR 127 /* Execution error exit status. */
66+
5967//#include "notify.c"
6068extern int notify (char * title , char * body , int timeout );
6169struct stat st ;
@@ -64,7 +72,7 @@ static ssize_t fs_offset; // The offset at which a filesystem image is expected
6472
6573static void die (const char * msg ) {
6674 fprintf (stderr , "%s\n" , msg );
67- exit (1 );
75+ exit (EXIT_EXECERROR );
6876}
6977
7078/* Check whether directory is writable */
@@ -544,7 +552,7 @@ int main(int argc, char *argv[]) {
544552 // error check
545553 if (fs_offset < 0 ) {
546554 printf ("Failed to get fs offset for %s\n" , appimage_path );
547- exit (EXIT_FAILURE );
555+ exit (EXIT_EXECERROR );
548556 }
549557
550558 arg = getArg (argc ,argv ,'-' );
@@ -556,7 +564,7 @@ int main(int argc, char *argv[]) {
556564 ssize_t length = readlink (appimage_path , fullpath , sizeof (fullpath ));
557565 if (length < 0 ) {
558566 printf ("Error getting realpath for %s\n" , appimage_path );
559- exit (EXIT_FAILURE );
567+ exit (EXIT_EXECERROR );
560568 }
561569 fullpath [length ] = '\0' ;
562570
@@ -603,7 +611,7 @@ int main(int argc, char *argv[]) {
603611 FILE * f = fopen (appimage_path , "rb" );
604612 if (f == NULL ) {
605613 perror ("Failed to open AppImage file" );
606- exit (1 );
614+ exit (EXIT_EXECERROR );
607615 }
608616
609617 Md5Context ctx ;
@@ -628,14 +636,14 @@ int main(int argc, char *argv[]) {
628636
629637 if (!extract_appimage (appimage_path , prefix , NULL , false)) {
630638 fprintf (stderr , "Failed to extract AppImage\n" );
631- exit (1 );
639+ exit (EXIT_EXECERROR );
632640 }
633641
634642 int pid ;
635643 if ((pid = fork ()) == -1 ) {
636644 int error = errno ;
637645 fprintf (stderr , "fork() failed: %s\n" , strerror (error ));
638- exit (1 );
646+ exit (EXIT_EXECERROR );
639647 } else if (pid == 0 ) {
640648 const char apprun_fname [] = "AppRun" ;
641649 char * apprun_path = malloc (strlen (prefix ) + 1 + strlen (apprun_fname ) + 1 );
@@ -660,21 +668,25 @@ int main(int argc, char *argv[]) {
660668 fprintf (stderr , "Failed to run %s: %s\n" , apprun_path , strerror (error ));
661669
662670 free (apprun_path );
671+ exit (EXIT_EXECERROR );
663672 }
664673
665- int rv = waitpid (pid , NULL , 0 );
674+ int status = 0 ;
675+ int rv = waitpid (pid , & status , 0 );
676+ status = rv > 0 && WIFEXITED (status ) ? WEXITSTATUS (status ) : EXIT_EXECERROR ;
666677
667678 if (getenv ("NO_CLEANUP" ) == NULL ) {
668679 if (!rm_recursive (prefix )) {
669680 fprintf (stderr , "Failed to clean up cache directory\n" );
670- rv = false;
681+ if (status == 0 ) /* avoid messing existing failure exit status */
682+ status = EXIT_EXECERROR ;
671683 }
672684 }
673685
674686 // template == prefix, must be freed only once
675687 free (prefix );
676688
677- exit (rv ? 0 : 1 );
689+ exit (status );
678690 }
679691
680692 if (arg && strcmp (arg ,"appimage-version" )== 0 ) {
@@ -742,18 +754,18 @@ int main(int argc, char *argv[]) {
742754
743755 if (mkdtemp (mount_dir ) == NULL ) {
744756 perror ("create mount dir error" );
745- exit (1 );
757+ exit (EXIT_EXECERROR );
746758 }
747759
748760 if (pipe (keepalive_pipe ) == -1 ) {
749761 perror ("pipe error" );
750- exit (1 );
762+ exit (EXIT_EXECERROR );
751763 }
752764
753765 pid = fork ();
754766 if (pid == -1 ) {
755767 perror ("fork error" );
756- exit (1 );
768+ exit (EXIT_EXECERROR );
757769 }
758770
759771 if (pid == 0 ) {
@@ -801,13 +813,13 @@ int main(int argc, char *argv[]) {
801813 dir_fd = open (mount_dir , O_RDONLY );
802814 if (dir_fd == -1 ) {
803815 perror ("open dir error" );
804- exit (1 );
816+ exit (EXIT_EXECERROR );
805817 }
806818
807819 res = dup2 (dir_fd , 1023 );
808820 if (res == -1 ) {
809821 perror ("dup2 error" );
810- exit (1 );
822+ exit (EXIT_EXECERROR );
811823 }
812824 close (dir_fd );
813825
@@ -876,7 +888,7 @@ int main(int argc, char *argv[]) {
876888 execv (filename , real_argv );
877889 /* Error if we continue here */
878890 perror ("execv error" );
879- exit (1 );
891+ exit (EXIT_EXECERROR );
880892 }
881893
882894 return 0 ;
0 commit comments