]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/ventoy_gui.c
10 #include <sys/utsname.h>
11 #include <sys/types.h>
12 #include <linux/limits.h>
14 #include <sys/fcntl.h>
16 #include "ventoy_json.h"
18 #define LIB_FLAG_GTK2 (1 << 0)
19 #define LIB_FLAG_GTK3 (1 << 1)
20 #define LIB_FLAG_GTK4 (1 << 2)
21 #define LIB_FLAG_QT4 (1 << 3)
22 #define LIB_FLAG_QT5 (1 << 4)
23 #define LIB_FLAG_QT6 (1 << 5)
24 #define LIB_FLAG_GLADE2 (1 << 30)
26 #define LIB_FLAG_GTK (LIB_FLAG_GTK2 | LIB_FLAG_GTK3 | LIB_FLAG_GTK4)
27 #define LIB_FLAG_QT (LIB_FLAG_QT4 | LIB_FLAG_QT5 | LIB_FLAG_QT6)
30 #define MAX_LOG_BUF (1024 * 1024)
31 #define VTOY_GUI_PATH "_vtoy_gui_path_="
32 #define VTOY_ENV_STR "_vtoy_env_str_="
33 #define LD_CACHE_FILE "/etc/ld.so.cache"
34 #define INT2STR_YN(a) ((a) == 0 ? "NO" : "YES")
36 static int g_xdg_log
= 0;
37 static int g_xdg_ini
= 0;
38 static char g_log_file
[PATH_MAX
];
39 static char g_ini_file
[PATH_MAX
];
40 static char *g_log_buf
= NULL
;
41 extern char ** environ
;
43 #define CACHEMAGIC "ld.so-1.7.0"
47 int flags
; /* This is 1 for an ELF library. */
48 unsigned int key
, value
; /* String table indices. */
53 char magic
[sizeof CACHEMAGIC
- 1];
55 struct file_entry libs
[0];
58 #define CACHEMAGIC_NEW "glibc-ld.so.cache"
59 #define CACHE_VERSION "1.1"
60 #define CACHEMAGIC_VERSION_NEW CACHEMAGIC_NEW CACHE_VERSION
64 int32_t flags
; /* This is 1 for an ELF library. */
65 uint32_t key
, value
; /* String table indices. */
66 uint32_t osversion
; /* Required OS version. */
67 uint64_t hwcap
; /* Hwcap entry. */
72 char magic
[sizeof CACHEMAGIC_NEW
- 1];
73 char version
[sizeof CACHE_VERSION
- 1];
74 uint32_t nlibs
; /* Number of entries. */
75 uint32_t len_strings
; /* Size of string table. */
76 uint32_t unused
[5]; /* Leave space for future extensions
77 and align to 8 byte boundary. */
78 struct file_entry_new libs
[0]; /* Entries describing libraries. */
79 /* After this the string table of size len_strings is found. */
82 /* Used to align cache_file_new. */
83 #define ALIGN_CACHE(addr) \
84 (((addr) + __alignof__ (struct cache_file_new) -1) \
85 & (~(__alignof__ (struct cache_file_new) - 1)))
87 #define vlog(fmt, args...) ventoy_syslog(0, fmt, ##args)
89 void ventoy_syslog(int level
, const char *Fmt
, ...)
102 localtime_r(&stamp
, &ttm
);
107 buflen
= MAX_LOG_BUF
;
112 buflen
= sizeof(log
);
116 vsnprintf(buf
, buflen
, Fmt
, arg
);
119 fp
= fopen(g_log_file
, "a+");
122 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
123 ttm
.tm_year
+ 1900, ttm
.tm_mon
, ttm
.tm_mday
,
124 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
130 printf("[%04u/%02u/%02u %02u:%02u:%02u] %s",
131 ttm
.tm_year
+ 1900, ttm
.tm_mon
, ttm
.tm_mday
,
132 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
137 static int is_gtk_env(void)
139 const char *env
= NULL
;
141 env
= getenv("GNOME_SETUP_DISPLAY");
142 if (env
&& env
[0] == ':')
144 vlog("GNOME_SETUP_DISPLAY=%s\n", env
);
148 env
= getenv("DESKTOP_SESSION");
149 if (env
&& strcasecmp(env
, "xfce") == 0)
151 vlog("DESKTOP_SESSION=%s\n", env
);
158 static int is_qt_env(void)
163 static int detect_gtk_version(int libflag
)
170 gtk2
= libflag
& LIB_FLAG_GTK2
;
171 gtk3
= libflag
& LIB_FLAG_GTK3
;
172 gtk4
= libflag
& LIB_FLAG_GTK4
;
173 glade2
= libflag
& LIB_FLAG_GLADE2
;
175 if (gtk2
> 0 && glade2
> 0 && (gtk3
== 0 && gtk4
== 0))
180 if (gtk3
> 0 && (gtk2
== 0 && gtk4
== 0))
185 if (gtk4
> 0 && (gtk2
== 0 && gtk3
== 0))
200 if (gtk2
> 0 && glade2
> 0)
208 static int detect_qt_version(int libflag
)
214 qt4
= libflag
& LIB_FLAG_QT4
;
215 qt5
= libflag
& LIB_FLAG_QT5
;
216 qt6
= libflag
& LIB_FLAG_QT6
;
218 if (qt4
> 0 && (qt5
== 0 && qt6
== 0))
223 if (qt5
> 0 && (qt4
== 0 && qt6
== 0))
228 if (qt6
> 0 && (qt4
== 0 && qt5
== 0))
251 int bit_from_machine(const char *machine
)
253 if (strstr(machine
, "64"))
263 int get_os_bit(int *bit
)
266 struct utsname unameData
;
268 memset(&unameData
, 0, sizeof(unameData
));
269 ret
= uname(&unameData
);
272 vlog("uname error, code: %d\n", errno
);
276 *bit
= strstr(unameData
.machine
, "64") ? 64 : 32;
277 vlog("uname -m <%s> %dbit\n", unameData
.machine
, *bit
);
282 int read_file_1st_line(const char *file
, char *buffer
, int buflen
)
286 fp
= fopen(file
, "r");
289 vlog("Failed to open file %s code:%d", file
, errno
);
293 fgets(buffer
, buflen
, fp
);
298 static int read_pid_cmdline(long pid
, char *Buffer
, int BufLen
)
302 snprintf(path
, sizeof(path
), "/proc/%ld/cmdline", pid
);
303 return read_file_1st_line(path
, Buffer
, BufLen
);
306 static int is_dir_exist(const char *fmt
, ...)
313 vsnprintf(path
, sizeof(path
), fmt
, arg
);
316 memset(&st
, 0, sizeof(st
));
317 if (stat(path
, &st
) < 0)
322 if (st
.st_mode
& S_IFDIR
)
330 static void touch_new_file(char *filename
)
335 if (access(filename
, F_OK
) == -1)
337 for (pos
= filename
; *pos
; pos
++)
342 if (!is_dir_exist("%s", filename
))
344 mkdir(filename
, 0755);
350 fp
= fopen(filename
, "w+");
358 static int find_exe_path(const char *exe
, char *pathbuf
, int buflen
)
363 char *saveptr
= NULL
;
365 const char *env
= getenv("PATH");
372 newenv
= strdup(env
);
379 while (NULL
!= (tmpptr
= strtok_r(tmpptr
, ":", &saveptr
)))
381 snprintf(path
, sizeof(path
), "%s/%s", tmpptr
, exe
);
382 if (access(path
, F_OK
) != -1)
384 snprintf(pathbuf
, buflen
, "%s", path
);
395 void dump_args(const char *prefix
, char **argv
)
399 vlog("=========%s ARGS BEGIN===========\n", prefix
);
402 vlog("argv[%d]=<%s>\n", i
, argv
[i
]);
405 vlog("=========%s ARGS END===========\n", prefix
);
413 const char *env
= NULL
;
415 env
= getenv("DISPLAY");
416 if (NULL
== env
|| env
[0] != ':')
418 vlog("DISPLAY not exist(%p). Not in X environment.\n", env
);
422 ret
= get_os_bit(&bit
);
425 vlog("Failed to get os bit.\n");
429 buildbit
= strstr(VTOY_GUI_ARCH
, "64") ? 64 : 32;
430 vlog("Build bit is %d (%s)\n", buildbit
, VTOY_GUI_ARCH
);
434 vlog("Current system is %d bit (%s). Please run the correct VentoyGUI.\n", bit
, VTOY_GUI_ARCH
);
441 static char * find_argv(int argc
, char **argv
, char *key
)
446 len
= (int)strlen(key
);
447 for (i
= 0; i
< argc
; i
++)
449 if (strncmp(argv
[i
], key
, len
) == 0)
458 static int adjust_cur_dir(char *argv0
)
470 for (pos
= argv0
; pos
&& *pos
; pos
++)
491 static char **recover_environ_param(char *env
)
497 char **newenvs
= NULL
;
499 for (i
= 0; env
[i
]; i
++)
507 newenvs
= malloc(sizeof(char *) * (cnt
+ 1));
510 vlog("malloc new envs fail %d\n", cnt
+ 1);
513 memset(newenvs
, 0, sizeof(char *) * (cnt
+ 1));
515 for (j
= i
= 0; env
[i
]; i
++)
520 newenvs
[k
++] = env
+ j
;
525 vlog("recover environ %d %d\n", cnt
, k
);
529 static int restart_main(int argc
, char **argv
, char *guiexe
)
535 char *newargv
[MAX_PARAS
+ 1] = { NULL
};
537 para
= find_argv(argc
, argv
, VTOY_ENV_STR
);
540 vlog("failed to find %s\n", VTOY_ENV_STR
);
544 newargv
[j
++] = guiexe
;
545 for (i
= 1; i
< argc
&& j
< MAX_PARAS
; i
++)
547 if (strncmp(argv
[i
], "_vtoy_", 6) != 0)
549 newargv
[j
++] = argv
[i
];
553 envs
= recover_environ_param(para
+ strlen(VTOY_ENV_STR
));
556 vlog("recover success, argc=%d evecve <%s>\n", j
, guiexe
);
557 dump_args("EXECVE", newargv
);
558 execve(guiexe
, newargv
, envs
);
562 vlog("recover failed, argc=%d evecv <%s>\n", j
, guiexe
);
563 execv(guiexe
, newargv
);
569 static char *create_environ_param(const char *prefix
, char **envs
)
578 prelen
= strlen(prefix
);
579 for (i
= 0; envs
[i
]; i
++)
582 envlen
+= strlen(envs
[i
]) + 1;
585 para
= malloc(prelen
+ envlen
);
588 vlog("failed to malloc env str %d\n", prelen
+ envlen
);
593 memcpy(cur
, prefix
, prelen
);
596 for (i
= 0; envs
[i
]; i
++)
598 envlen
= strlen(envs
[i
]);
599 memcpy(cur
, envs
[i
], envlen
);
605 vlog("create environment param %d\n", cnt
);
609 static int restart_by_pkexec(int argc
, char **argv
, const char *curpath
, const char *exe
)
615 char pkexec
[PATH_MAX
];
616 char exepara
[PATH_MAX
];
617 char *newargv
[MAX_PARAS
+ 1] = { NULL
};
619 vlog("try restart self by pkexec ...\n");
621 if (find_exe_path("pkexec", pkexec
, sizeof(pkexec
)))
623 vlog("Find pkexec at <%s>\n", pkexec
);
627 vlog("pkexec not found\n");
631 if (argv
[0][0] != '/')
633 snprintf(path
, sizeof(path
), "%s/%s", curpath
, argv
[0]);
637 snprintf(path
, sizeof(path
), "%s", argv
[0]);
639 snprintf(exepara
, sizeof(exepara
), "%s%s", VTOY_GUI_PATH
, exe
);
641 newargv
[j
++] = pkexec
;
643 for (i
= 1; i
< argc
&& j
< MAX_PARAS
; i
++)
645 if (strcmp(argv
[i
], "--xdg") == 0)
649 newargv
[j
++] = argv
[i
];
654 newargv
[j
++] = create_environ_param(VTOY_ENV_STR
, environ
);
659 newargv
[j
++] = exepara
;
662 if (g_xdg_log
&& j
+ 1 < MAX_PARAS
)
665 newargv
[j
++] = g_log_file
;
668 if (g_xdg_ini
&& j
+ 1 < MAX_PARAS
)
671 newargv
[j
++] = g_ini_file
;
674 dump_args("PKEXEC", newargv
);
675 execv(pkexec
, newargv
);
680 static int ld_cache_lib_check(const char *lib
, int *flag
)
682 if (((*flag
) & LIB_FLAG_GTK3
) == 0)
684 if (strncmp(lib
, "libgtk-3.so", 11) == 0)
686 vlog("LIB:<%s>\n", lib
);
687 *flag
|= LIB_FLAG_GTK3
;
692 if (((*flag
) & LIB_FLAG_GTK2
) == 0)
694 if (strncmp(lib
, "libgtk-x11-2.0.so", 17) == 0)
696 vlog("LIB:<%s>\n", lib
);
697 *flag
|= LIB_FLAG_GTK2
;
702 if (((*flag
) & LIB_FLAG_GTK4
) == 0)
704 if (strncmp(lib
, "libgtk-4.so", 11) == 0)
706 vlog("LIB:<%s>\n", lib
);
707 *flag
|= LIB_FLAG_GTK4
;
712 if (((*flag
) & LIB_FLAG_QT4
) == 0)
714 if (strncmp(lib
, "libQt4", 6) == 0)
716 vlog("LIB:<%s>\n", lib
);
717 *flag
|= LIB_FLAG_QT4
;
722 if (((*flag
) & LIB_FLAG_QT5
) == 0)
724 if (strncmp(lib
, "libQt5", 6) == 0)
726 vlog("LIB:<%s>\n", lib
);
727 *flag
|= LIB_FLAG_QT5
;
732 if (((*flag
) & LIB_FLAG_QT6
) == 0)
734 if (strncmp(lib
, "libQt6", 6) == 0)
736 vlog("LIB:<%s>\n", lib
);
737 *flag
|= LIB_FLAG_QT6
;
742 if (((*flag
) & LIB_FLAG_GLADE2
) == 0)
744 if (strncmp(lib
, "libglade-2", 10) == 0)
746 vlog("LIB:<%s>\n", lib
);
747 *flag
|= LIB_FLAG_GLADE2
;
755 static int parse_ld_cache(int *flag
)
762 size_t cache_size
= 0;
763 const char *cache_data
= NULL
;
764 struct cache_file
*cache
= NULL
;
765 struct cache_file_new
*cache_new
= NULL
;
769 fd
= open(LD_CACHE_FILE
, O_RDONLY
);
772 vlog("failed to open %s err:%d\n", LD_CACHE_FILE
, errno
);
776 if (fstat(fd
, &st
) < 0 || st
.st_size
== 0)
782 cache
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
783 if (cache
== MAP_FAILED
)
789 cache_size
= st
.st_size
;
790 if (cache_size
< sizeof (struct cache_file
))
792 vlog("File is not a cache file.\n");
793 munmap (cache
, cache_size
);
798 if (memcmp(cache
->magic
, CACHEMAGIC
, sizeof CACHEMAGIC
- 1))
800 /* This can only be the new format without the old one. */
801 cache_new
= (struct cache_file_new
*) cache
;
803 if (memcmp(cache_new
->magic
, CACHEMAGIC_NEW
, sizeof CACHEMAGIC_NEW
- 1) ||
804 memcmp (cache_new
->version
, CACHE_VERSION
, sizeof CACHE_VERSION
- 1))
806 munmap (cache
, cache_size
);
812 /* This is where the strings start. */
813 cache_data
= (const char *) cache_new
;
817 /* Check for corruption, avoiding overflow. */
818 if ((cache_size
- sizeof (struct cache_file
)) / sizeof (struct file_entry
) < cache
->nlibs
)
820 vlog("File is not a cache file.\n");
821 munmap (cache
, cache_size
);
826 offset
= ALIGN_CACHE(sizeof (struct cache_file
) + (cache
->nlibs
* sizeof (struct file_entry
)));
828 /* This is where the strings start. */
829 cache_data
= (const char *) &cache
->libs
[cache
->nlibs
];
831 /* Check for a new cache embedded in the old format. */
832 if (cache_size
> (offset
+ sizeof (struct cache_file_new
)))
834 cache_new
= (struct cache_file_new
*) ((void *)cache
+ offset
);
836 if (memcmp(cache_new
->magic
, CACHEMAGIC_NEW
, sizeof CACHEMAGIC_NEW
- 1) == 0 &&
837 memcmp(cache_new
->version
, CACHE_VERSION
, sizeof CACHE_VERSION
- 1) == 0)
839 cache_data
= (const char *) cache_new
;
847 vlog("%d libs found in cache format 0\n", cache
->nlibs
);
848 for (i
= 0; i
< cache
->nlibs
; i
++)
850 ld_cache_lib_check(cache_data
+ cache
->libs
[i
].key
, flag
);
853 else if (format
== 1)
855 vlog("%d libs found in cache format 1\n", cache_new
->nlibs
);
857 for (i
= 0; i
< cache_new
->nlibs
; i
++)
859 ld_cache_lib_check(cache_data
+ cache_new
->libs
[i
].key
, flag
);
863 vlog("ldconfig lib flags 0x%x\n", *flag
);
864 vlog("lib flags GLADE2:[%s] GTK2:[%s] GTK3:[%s] GTK4:[%s] QT4:[%s] QT5:[%s] QT6:[%s]\n",
865 INT2STR_YN((*flag
) & LIB_FLAG_GLADE2
), INT2STR_YN((*flag
) & LIB_FLAG_GTK2
),
866 INT2STR_YN((*flag
) & LIB_FLAG_GTK3
), INT2STR_YN((*flag
) & LIB_FLAG_GTK4
),
867 INT2STR_YN((*flag
) & LIB_FLAG_QT4
), INT2STR_YN((*flag
) & LIB_FLAG_QT5
),
868 INT2STR_YN((*flag
) & LIB_FLAG_QT6
));
870 munmap (cache
, cache_size
);
875 static int gui_type_check(VTOY_JSON
*pstNode
)
878 const char *env
= NULL
;
879 const char *arch
= NULL
;
880 const char *srctype
= NULL
;
881 const char *srcname
= NULL
;
882 const char *condition
= NULL
;
883 const char *expression
= NULL
;
886 arch
= vtoy_json_get_string_ex(pstNode
, "arch");
887 srctype
= vtoy_json_get_string_ex(pstNode
, "type");
888 srcname
= vtoy_json_get_string_ex(pstNode
, "name");
889 condition
= vtoy_json_get_string_ex(pstNode
, "condition");
890 expression
= vtoy_json_get_string_ex(pstNode
, "expression");
892 if (srctype
== NULL
|| srcname
== NULL
|| condition
== NULL
)
897 if (arch
&& NULL
== strstr(arch
, VTOY_GUI_ARCH
))
902 vlog("check <%s> <%s> <%s>\n", srctype
, srcname
, condition
);
904 if (strcmp(srctype
, "file") == 0)
906 if (access(srcname
, F_OK
) == -1)
911 if (strcmp(condition
, "exist") == 0)
913 vlog("File %s exist\n", srcname
);
916 else if (strcmp(condition
, "contains") == 0)
918 fp
= fopen(srcname
, "r");
924 while (fgets(line
, sizeof(line
), fp
))
926 if (strstr(line
, expression
))
928 vlog("File %s contains %s\n", srcname
, expression
);
938 else if (strcmp(srctype
, "env") == 0)
940 env
= getenv(srcname
);
946 if (strcmp(condition
, "exist") == 0)
948 vlog("env %s exist\n", srcname
);
951 else if (strcmp(condition
, "equal") == 0)
953 if (strcmp(expression
, env
) == 0)
955 vlog("env %s is %s\n", srcname
, env
);
960 else if (strcmp(condition
, "contains") == 0)
962 if (strstr(env
, expression
))
964 vlog("env %s is %s contains %s\n", srcname
, env
, expression
);
974 static int read_file_to_buf(const char *FileName
, int ExtLen
, void **Bufer
, int *BufLen
)
980 fp
= fopen(FileName
, "rb");
983 vlog("Failed to open file %s", FileName
);
987 fseek(fp
, 0, SEEK_END
);
988 FileSize
= (int)ftell(fp
);
990 Data
= malloc(FileSize
+ ExtLen
);
997 fseek(fp
, 0, SEEK_SET
);
998 fread(Data
, 1, FileSize
, fp
);
1008 static int distro_check_gui_env(char *type
, int len
, int *pver
)
1013 VTOY_JSON
*pstNode
= NULL
;
1014 VTOY_JSON
*pstJson
= NULL
;
1016 vlog("distro_check_gui_env ...\n");
1018 if (access("./tool/distro_gui_type.json", F_OK
) == -1)
1020 vlog("distro_gui_type.json file not exist\n");
1024 read_file_to_buf("./tool/distro_gui_type.json", 1, (void **)&pBuf
, &size
);
1027 pstJson
= vtoy_json_create();
1028 vtoy_json_parse(pstJson
, pBuf
);
1030 for (pstNode
= pstJson
->pstChild
; pstNode
; pstNode
= pstNode
->pstNext
)
1032 if (gui_type_check(pstNode
->pstChild
))
1034 length
= (int)snprintf(type
, len
, "%s", vtoy_json_get_string_ex(pstNode
->pstChild
, "gui"));
1035 *pver
= type
[length
- 1] - '0';
1036 type
[length
- 1] = 0;
1041 vtoy_json_destroy(pstJson
);
1042 return pstNode
? 1 : 0;
1045 static int detect_gui_exe_path(int argc
, char **argv
, const char *curpath
, char *pathbuf
, int buflen
)
1051 const char *guitype
= NULL
;
1054 struct stat filestat
;
1056 for (i
= 1; i
< argc
; i
++)
1058 if (argv
[i
] && strcmp(argv
[i
], "--gtk2") == 0)
1063 else if (argv
[i
] && strcmp(argv
[i
], "--gtk3") == 0)
1068 else if (argv
[i
] && strcmp(argv
[i
], "--gtk4") == 0)
1073 else if (argv
[i
] && strcmp(argv
[i
], "--qt4") == 0)
1078 else if (argv
[i
] && strcmp(argv
[i
], "--qt5") == 0)
1083 else if (argv
[i
] && strcmp(argv
[i
], "--qt6") == 0)
1092 vlog("Get GUI type from param <%s%d>.\n", guitype
, ver
);
1094 else if (access("./ventoy_gui_type", F_OK
) != -1)
1096 vlog("Get GUI type from ventoy_gui_type file.\n");
1099 read_file_1st_line("./ventoy_gui_type", line
, sizeof(line
));
1100 if (strncmp(line
, "gtk2", 4) == 0)
1105 parse_ld_cache(&libflag
);
1106 if ((libflag
& LIB_FLAG_GLADE2
) == 0)
1108 vlog("libglade2 is necessary for GTK2, but not found.\n");
1112 else if (strncmp(line
, "gtk3", 4) == 0)
1117 else if (strncmp(line
, "gtk4", 4) == 0)
1122 else if (strncmp(line
, "qt4", 3) == 0)
1127 else if (strncmp(line
, "qt5", 3) == 0)
1132 else if (strncmp(line
, "qt6", 3) == 0)
1139 vlog("Current X environment is NOT supported.\n");
1145 vlog("Now detect the GUI type ...\n");
1147 parse_ld_cache(&libflag
);
1149 if ((LIB_FLAG_GTK
& libflag
) > 0 && (LIB_FLAG_QT
& libflag
) == 0)
1152 ver
= detect_gtk_version(libflag
);
1154 else if ((LIB_FLAG_GTK
& libflag
) == 0 && (LIB_FLAG_QT
& libflag
) > 0)
1157 ver
= detect_qt_version(libflag
);
1159 else if ((LIB_FLAG_GTK
& libflag
) > 0 && (LIB_FLAG_QT
& libflag
) > 0)
1161 if (distro_check_gui_env(line
, sizeof(line
), &ver
))
1164 vlog("distro_check_gui <%s%d> ...\n", line
, ver
);
1166 else if (is_gtk_env())
1169 ver
= detect_gtk_version(libflag
);
1171 else if (is_qt_env())
1174 ver
= detect_qt_version(libflag
);
1178 vlog("Can not distinguish GTK and QT, default use GTK.\n");
1180 ver
= detect_gtk_version(libflag
);
1185 vlog("Current X environment is NOT supported.\n");
1190 snprintf(pathbuf
, buflen
, "%s/tool/%s/Ventoy2Disk.%s%d", curpath
, VTOY_GUI_ARCH
, guitype
, ver
);
1192 vlog("This is %s%d X environment.\n", guitype
, ver
);
1193 vlog("exe = %s\n", pathbuf
);
1195 if (access(pathbuf
, F_OK
) == -1)
1197 vlog("%s is not exist.\n", pathbuf
);
1201 if (access(pathbuf
, X_OK
) == -1)
1203 vlog("execute permission check fail, try chmod.\n", pathbuf
);
1204 if (stat(pathbuf
, &filestat
) == 0)
1206 mode
= filestat
.st_mode
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
1207 ret
= chmod(pathbuf
, mode
);
1208 vlog("old mode=%o new mode=%o ret=%d\n", filestat
.st_mode
, mode
, ret
);
1213 vlog("execute permission check success.\n");
1219 int real_main(int argc
, char **argv
)
1224 char path
[PATH_MAX
];
1225 char curpath
[PATH_MAX
];
1227 ret
= adjust_cur_dir(argv
[0]);
1230 vlog("=========================================================\n");
1231 vlog("=========================================================\n");
1232 vlog("=============== VentoyGui %s ===============\n", VTOY_GUI_ARCH
);
1233 vlog("=========================================================\n");
1234 vlog("=========================================================\n");
1235 vlog("log file is <%s>\n", g_log_file
);
1238 getcwd(curpath
, sizeof(curpath
));
1240 vlog("pid:%ld ppid:%ld uid:%d euid:%d\n", (long)getpid(), (long)getppid(), getuid(), euid
);
1241 vlog("adjust dir:%d current path:%s\n", ret
, curpath
);
1242 dump_args("RAW", argv
);
1244 if (access("./boot/boot.img", F_OK
) == -1)
1246 vlog("Please run under the correct directory!\n");
1250 exe
= find_argv(argc
, argv
, VTOY_GUI_PATH
);
1255 vlog("Invalid euid %d when restart.\n", euid
);
1259 return restart_main(argc
, argv
, exe
+ strlen(VTOY_GUI_PATH
));
1268 if (detect_gui_exe_path(argc
, argv
, curpath
, path
, sizeof(path
)))
1275 vlog("We have root privileges, just exec %s\n", path
);
1277 execv(argv
[0], argv
);
1281 vlog("EUID check failed.\n");
1284 restart_by_pkexec(argc
, argv
, curpath
, path
);
1286 vlog("### Please run with root privileges. ###\n");
1294 int main(int argc
, char **argv
)
1298 const char *env
= NULL
;
1300 snprintf(g_log_file
, sizeof(g_log_file
), "log.txt");
1301 for (i
= 0; i
< argc
; i
++)
1303 if (argv
[i
] && argv
[i
+ 1] && strcmp(argv
[i
], "-l") == 0)
1305 touch_new_file(argv
[i
+ 1]);
1306 snprintf(g_log_file
, sizeof(g_log_file
), "%s", argv
[i
+ 1]);
1308 else if (argv
[i
] && argv
[i
+ 1] && strcmp(argv
[i
], "-i") == 0)
1310 touch_new_file(argv
[i
+ 1]);
1312 else if (argv
[i
] && strcmp(argv
[i
], "--xdg") == 0)
1314 env
= getenv("XDG_CACHE_HOME");
1318 snprintf(g_log_file
, sizeof(g_log_file
), "%s/ventoy/ventoy.log", env
);
1319 touch_new_file(g_log_file
);
1323 env
= getenv("HOME");
1324 if (env
&& is_dir_exist("%s/.cache", env
))
1327 snprintf(g_log_file
, sizeof(g_log_file
), "%s/.cache/ventoy/ventoy.log", env
);
1328 touch_new_file(g_log_file
);
1332 env
= getenv("XDG_CONFIG_HOME");
1336 snprintf(g_ini_file
, sizeof(g_ini_file
), "%s/ventoy/Ventoy2Disk.ini", env
);
1337 touch_new_file(g_ini_file
);
1341 env
= getenv("HOME");
1342 if (env
&& is_dir_exist("%s/.config", env
))
1345 snprintf(g_ini_file
, sizeof(g_ini_file
), "%s/.config/ventoy/Ventoy2Disk.ini", env
);
1346 touch_new_file(g_ini_file
);
1352 g_log_buf
= malloc(MAX_LOG_BUF
);
1355 vlog("Failed to malloc log buffer %d\n", MAX_LOG_BUF
);
1359 ret
= real_main(argc
, argv
);