Skip to content

Commit e33875a

Browse files
authored
merge exec.so and localefix.so into a single common library (#137)
1 parent 5d65ac5 commit e33875a

File tree

6 files changed

+68
-91
lines changed

6 files changed

+68
-91
lines changed

useful-tools/demo/gtk3-demo-appimage.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ set -eux
66

77
ARCH="$(uname -m)"
88
SHARUN="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/quick-sharun.sh"
9-
URUNTIME="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/uruntime2appimage.sh"
109
EXTRA_PACKAGES="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/get-debloated-pkgs.sh"
1110

1211
export ICON=DUMMY
12+
export ANYLINUX_LIB=1
1313
export DESKTOP=DUMMY
14+
export OUTPATH=./dist
1415
export OUTNAME=gtk3-demo-"$ARCH".AppImage
1516

1617
pacman -Syu --noconfirm \
@@ -43,10 +44,5 @@ wget --retry-connrefused --tries=30 "$SHARUN" -O ./quick-sharun
4344
chmod +x ./quick-sharun
4445
./quick-sharun /usr/bin/gtk3-demo
4546

46-
wget --retry-connrefused --tries=30 "$URUNTIME" -O ./uruntime2appimage
47-
chmod +x ./uruntime2appimage
48-
./uruntime2appimage
49-
mkdir -p ./dist
50-
mv -v ./*.AppImage ./dist
47+
./quick-sharun --make-appimage
5148

52-
echo "All Done!"

useful-tools/demo/gtk4-demo-appimage.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ set -eux
66

77
ARCH="$(uname -m)"
88
SHARUN="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/quick-sharun.sh"
9-
URUNTIME="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/uruntime2appimage.sh"
109
EXTRA_PACKAGES="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/get-debloated-pkgs.sh"
1110

1211
export DEPLOY_OPENGL=1
12+
export ANYLINUX_LIB=1
1313
export ICON=DUMMY
1414
export DESKTOP=DUMMY
15+
export OUTPATH=./dist
1516
export OUTNAME=gtk4-demo-"$ARCH".AppImage
1617

1718
pacman -Syu --noconfirm \
@@ -44,10 +45,5 @@ wget --retry-connrefused --tries=30 "$SHARUN" -O ./quick-sharun
4445
chmod +x ./quick-sharun
4546
./quick-sharun /usr/bin/gtk4-demo
4647

47-
wget --retry-connrefused --tries=30 "$URUNTIME" -O ./uruntime2appimage
48-
chmod +x ./uruntime2appimage
49-
./uruntime2appimage
50-
mkdir -p ./dist
51-
mv -v ./*.AppImage ./dist
48+
./quick-sharun --make-appimage
5249

53-
echo "All Done!"

useful-tools/demo/vkcube-glxgears-appimage.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ set -eux
66

77
ARCH="$(uname -m)"
88
SHARUN="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/quick-sharun.sh"
9-
URUNTIME="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/uruntime2appimage.sh"
109
EXTRA_PACKAGES="https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/get-debloated-pkgs.sh"
1110

1211
export DEPLOY_OPENGL=1
1312
export DEPLOY_VULKAN=1
13+
export ANYLINUX_LIB=1
1414
export ICON=DUMMY
1515
export DESKTOP=DUMMY
16+
export OUTPATH=./dist
1617
export OUTNAME=vkcube+glxgears-demo-"$ARCH".AppImage
1718

1819
pacman -Syu --noconfirm \
@@ -44,10 +45,5 @@ wget --retry-connrefused --tries=30 "$SHARUN" -O ./quick-sharun
4445
chmod +x ./quick-sharun
4546
./quick-sharun /usr/bin/vkcube /usr/bin/glxgears
4647

47-
wget --retry-connrefused --tries=30 "$URUNTIME" -O ./uruntime2appimage
48-
chmod +x ./uruntime2appimage
49-
./uruntime2appimage
50-
mkdir -p ./dist
51-
mv -v ./*.AppImage ./dist
48+
./quick-sharun --make-appimage
5249

53-
echo "All Done!"
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
* mode is used, where for example the HOME var from the portable .home dir would
99
* be inherited by other processes launched by the appimage in portable mode
1010
* causing them to start using the fake .home dir instead of the real home
11+
*
12+
* It also sets LC_ALL=C if it detects the application will fail to switch locale
13+
* While we normally bundle locales with quick-sharun and apps have working
14+
* language interface, we do not bundle the libc locale because glibc
15+
* has issues when LOCPATH is used This means some applications like dolphin-emu
16+
* crash when glibc cannot switch locale even though the application itself can
17+
* This library checks that and forces the C locale instead to prevent crashes
1118
*/
1219

1320
#ifndef _GNU_SOURCE
@@ -22,20 +29,36 @@
2229
#include <unistd.h>
2330
#include <fcntl.h>
2431
#include <errno.h>
32+
#include <locale.h>
2533

2634
typedef int (*execve_func_t)(const char *filename, char *const argv[], char *const envp[]);
2735

36+
#define LOG(fmt, ...) fprintf(stderr, " [anylinux.so] LOCALEFIX >> " fmt "\n", ##__VA_ARGS__)
37+
38+
__attribute__((constructor))
39+
static void locale_fix_init(void) {
40+
if (!setlocale(LC_ALL, "")) {
41+
LOG("Failed to set locale, falling back to C locale.");
42+
if (!setlocale(LC_ALL, "C")) {
43+
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
44+
}
45+
if (setenv("LC_ALL", "C", 1) != 0) {
46+
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
47+
}
48+
}
49+
}
50+
2851
#define VISIBLE __attribute__ ((visibility ("default")))
2952

3053
// print to stderr when APPIMAGE_EXEC_DEBUG=1
3154
static int appimage_exec_debug_enabled(void) {
32-
const char *v = getenv("APPIMAGE_EXEC_DEBUG");
55+
const char *v = getenv("ANYLINUX_LIB_DEBUG");
3356
return v && strcmp(v, "1") == 0;
3457
}
3558

3659
#define DEBUG_PRINT(...) do \
3760
if (appimage_exec_debug_enabled()) \
38-
fprintf(stderr, "APPIMAGE_EXEC>> " __VA_ARGS__); \
61+
fprintf(stderr, " [anylinux.so] >> " __VA_ARGS__); \
3962
while (0)
4063

4164
// problematic vars to check
@@ -185,8 +208,19 @@ static int exec_common(execve_func_t function, const char *filename, char* const
185208
DEBUG_PRINT("Error creating cleaned environment; using original env\n");
186209
env = envp;
187210
}
188-
} else
189-
DEBUG_PRINT("Internal process; leaving environment unchanged\n");
211+
} else {
212+
const char *basename = strrchr(filename, '/');
213+
basename = basename ? basename + 1 : filename;
214+
if (strcmp(basename, "xdg-open") == 0 || strcmp(basename, "gio-launch-desktop") == 0) {
215+
DEBUG_PRINT("Internal process detected (%s); cleaning environment anyway since this is needed\n", basename);
216+
env = create_cleaned_env(envp);
217+
if (!env) {
218+
DEBUG_PRINT("Error creating cleaned environment; using original env\n");
219+
env = envp;
220+
}
221+
} else
222+
DEBUG_PRINT("Internal process; leaving environment unchanged\n");
223+
}
190224

191225
DEBUG_PRINT("Calling exec for %s\n", filename);
192226
int ret = function(filename, argv, env);

useful-tools/lib/localefix.c

Lines changed: 0 additions & 33 deletions
This file was deleted.

useful-tools/quick-sharun.sh

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ SHARUN_LINK=${SHARUN_LINK:-https://github.com/VHSgunzo/sharun/releases/latest/do
2121
HOOKSRC=${HOOKSRC:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/hooks}
2222
LD_PRELOAD_OPEN=${LD_PRELOAD_OPEN:-https://github.com/VHSgunzo/pathmap.git}
2323

24-
EXEC_WRAPPER=${EXEC_WRAPPER:-0}
25-
EXEC_WRAPPER_SOURCE=${EXEC_WRAPPER_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/lib/exec.c}
26-
LOCALE_FIX=${LOCALE_FIX:-0}
27-
LOCALE_FIX_SOURCE=${LOCALE_FIX_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/lib/localefix.c}
24+
ANYLINUX_LIB=${ANYLINUX_LIB:-0}
25+
ANYLINUX_LIB_SOURCE=${ANYLINUX_LIB_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/lib/anylinux.c}
2826
NOTIFY_SOURCE=${NOTIFY_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/bin/notify}
2927
APPRUN_SOURCE=${APPRUN_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/bin/AppRun-generic}
3028
URUNTIME2APPIMAGE_SOURCE=${URUNTIME2APPIMAGE_SOURCE:-https://raw.githubusercontent.com/pkgforge-dev/Anylinux-AppImages/refs/heads/main/useful-tools/uruntime2appimage.sh}
@@ -49,6 +47,11 @@ DEPENDENCIES="
4947
tr
5048
"
5149

50+
# keep this for backwards compat until all existing scripts have been updated
51+
if [ "$EXEC_WRAPPER" = 1 ] || [ "$LOCALE_FIX" = 1 ]; then
52+
ANYLINUX_LIB=1
53+
fi
54+
5255
# check if the _tmp_* vars have not be declared already
5356
# likely to happen if this script run more than once
5457
PATH_MAPPING_SCRIPT="$APPDIR"/bin/path-mapping-hardcoded.src.hook
@@ -165,10 +168,8 @@ _help_msg() {
165168
LIB_DIR Set source library directory if autodetection fails.
166169
NO_STRIP Disable stripping binaries and libraries if set.
167170
APPDIR Destination AppDir (default: ./AppDir).
168-
EXEC_WRAPPER Preloads a library that unsets environment variables known to cause
169-
problems to child processes. Not needed if the app will just use
170-
xdg-open to spawn child proceeses since in that case sharun has
171-
a wrapper for xdg-open that handles that.
171+
ANYLINUX_LIB Preloads a library that unsets environment variables known to cause
172+
problems to child processes. Set to 0 to disable.
172173
173174
PATH_MAPPING Configures and preloads pathmap.
174175
Set this variable if the application is hardcoded to look
@@ -202,11 +203,8 @@ _sanity_check() {
202203
fi
203204
done
204205

205-
if [ "$EXEC_WRAPPER" = 1 ] && ! command -v cc 1>/dev/null; then
206-
_err_msg "ERROR: Using EXEC_WRAPPER requires cc"
207-
exit 1
208-
elif [ "$LOCALE_FIX" = 1 ] && ! command -v cc 1>/dev/null; then
209-
_err_msg "ERROR: Using LOCALE_FIX requires cc"
206+
if [ "$ANYLINUX_LIB" = 1 ] && ! command -v cc 1>/dev/null; then
207+
_err_msg "ERROR: Using ANYLINUX_LIB requires cc"
210208
exit 1
211209
elif [ "$DEPLOY_PYTHON" = 1 ] && [ "$DEPLOY_SYS_PYTHON" = 1 ]; then
212210
_err_msg "ERROR: DEPLOY_PYTHON and DEPLOY_SYS_PYTHON cannot be both enabled!"
@@ -796,17 +794,18 @@ _handle_bins_scripts() {
796794

797795
}
798796

799-
_add_exec_wrapper() {
800-
if [ "$EXEC_WRAPPER" != 1 ]; then
797+
_add_anylinux_lib() {
798+
if [ "$ANYLINUX_LIB" != 1 ]; then
801799
return 0
802800
fi
803801

804-
_echo "* Building exec.so..."
805-
_download "$TMPDIR"/exec.c "$EXEC_WRAPPER_SOURCE"
806-
cc -shared -fPIC "$TMPDIR"/exec.c -o "$APPDIR"/lib/exec.so
807-
echo "exec.so" >> "$APPDIR"/.preload
802+
_echo "* Building anylinux.so..."
803+
_download "$APPDIR"/.anylinux.c "$ANYLINUX_LIB_SOURCE"
804+
cc -shared -fPIC "$APPDIR"/.anylinux.c -o "$APPDIR"/lib/anylinux.so
805+
echo "anylinux.so" >> "$APPDIR"/.preload
808806

809-
# remove xdg-open wrapper not needed when exec.so is in use
807+
# remove xdg-open wrapper not needed when the lib is in use
808+
# we still need to have a wrapper for gio-launch-desktop though
810809
if [ -f "$APPDIR"/bin/gio-launch-desktop ]; then
811810
rm -f "$APPDIR"/bin/gio-launch-desktop
812811
cat <<-'EOF' > "$APPDIR"/bin/gio-launch-desktop
@@ -817,17 +816,7 @@ _add_exec_wrapper() {
817816
chmod +x "$APPDIR"/bin/gio-launch-desktop
818817
fi
819818
rm -f "$APPDIR"/bin/xdg-open
820-
_echo "* EXEC_WRAPPER successfully added!"
821-
}
822-
823-
_add_locale_fix() {
824-
if [ "$LOCALE_FIX" = 1 ] && [ ! -f "$APPDIR"/lib/localefix.so ]; then
825-
_echo "* Building localefix.so..."
826-
_download "$TMPDIR"/localefix.c "$LOCALE_FIX_SOURCE"
827-
cc -shared -fPIC "$TMPDIR"/localefix.c -o "$APPDIR"/lib/localefix.so
828-
echo "localefix.so" >> "$APPDIR"/.preload
829-
_echo "* LOCALE_FIX successfully added!"
830-
fi
819+
_echo "* anylinux.so successfully added!"
831820
}
832821

833822
_map_paths_ld_preload_open() {
@@ -1295,8 +1284,7 @@ echo ""
12951284
_deploy_icon_and_desktop
12961285
_map_paths_ld_preload_open
12971286
_map_paths_binary_patch
1298-
_add_exec_wrapper
1299-
_add_locale_fix
1287+
_add_anylinux_lib
13001288
_deploy_datadir
13011289
_deploy_locale
13021290
_check_window_class

0 commit comments

Comments
 (0)