ventoy_gui.c 21.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <dirent.h> 
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/mman.h>

#define LIB_FLAG_GTK2   (1 << 0)
#define LIB_FLAG_GTK3   (1 << 1)
#define LIB_FLAG_GTK4   (1 << 2)
#define LIB_FLAG_QT4    (1 << 3)
#define LIB_FLAG_QT5    (1 << 4)
#define LIB_FLAG_QT6    (1 << 5)
#define LIB_FLAG_GLADE2 (1 << 30)

#define LIB_FLAG_GTK    (LIB_FLAG_GTK2 | LIB_FLAG_GTK3 | LIB_FLAG_GTK4)
#define LIB_FLAG_QT     (LIB_FLAG_QT4 | LIB_FLAG_QT5 | LIB_FLAG_QT6)

#define MAX_PARAS       64
#define MAX_LOG_BUF     (1024 * 1024)
#define VTOY_GUI_PATH   "_vtoy_gui_path_="
#define VTOY_ENV_STR    "_vtoy_env_str_="
#define LD_CACHE_FILE   "/etc/ld.so.cache"
#define INT2STR_YN(a)   ((a) == 0 ? "NO" : "YES")

static char *g_log_buf = NULL;
extern char ** environ;

#define CACHEMAGIC "ld.so-1.7.0"

struct file_entry
{
  int flags;		/* This is 1 for an ELF library.  */
  unsigned int key, value; /* String table indices.  */
};

struct cache_file
{
  char magic[sizeof CACHEMAGIC - 1];
  unsigned int nlibs;
  struct file_entry libs[0];
};

#define CACHEMAGIC_NEW "glibc-ld.so.cache"
#define CACHE_VERSION "1.1"
#define CACHEMAGIC_VERSION_NEW CACHEMAGIC_NEW CACHE_VERSION

struct file_entry_new
{
  int32_t flags;		/* This is 1 for an ELF library.  */
  uint32_t key, value;		/* String table indices.  */
  uint32_t osversion;		/* Required OS version.	 */
  uint64_t hwcap;		/* Hwcap entry.	 */
};

struct cache_file_new
{
  char magic[sizeof CACHEMAGIC_NEW - 1];
  char version[sizeof CACHE_VERSION - 1];
  uint32_t nlibs;		/* Number of entries.  */
  uint32_t len_strings;		/* Size of string table. */
  uint32_t unused[5];		/* Leave space for future extensions
				   and align to 8 byte boundary.  */
  struct file_entry_new libs[0]; /* Entries describing libraries.  */
  /* After this the string table of size len_strings is found.	*/
};

/* Used to align cache_file_new.  */
#define ALIGN_CACHE(addr)				\
(((addr) + __alignof__ (struct cache_file_new) -1)	\
 & (~(__alignof__ (struct cache_file_new) - 1)))

static void vlog(const char *Fmt, ...)
{
    int buflen;
    char *buf = NULL;
    char log[512];
    va_list arg;
    time_t stamp;
    struct tm ttm;
    FILE *fp;
    
    time(&stamp);
    localtime_r(&stamp, &ttm);

    if (g_log_buf)
    {
        buf = g_log_buf;
        buflen = MAX_LOG_BUF;
    }
    else
    {
        buf = log;
        buflen = sizeof(log);
    }

    va_start(arg, Fmt);
    vsnprintf(buf, buflen, Fmt, arg);
    va_end(arg);

    fp = fopen("log.txt", "a+");
    if (fp)
    {
        fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s", 
           ttm.tm_year + 1900, ttm.tm_mon, ttm.tm_mday,
           ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
           buf);
        fclose(fp);
    }

    printf("[%04u/%02u/%02u %02u:%02u:%02u] %s", 
           ttm.tm_year + 1900, ttm.tm_mon, ttm.tm_mday,
           ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
           buf);
}

static int is_gtk_env(void)
{
    const char *env = NULL;
    
    env = getenv("GNOME_SETUP_DISPLAY");
    if (env && env[0] == ':')
    {
        vlog("GNOME_SETUP_DISPLAY=%s\n", env);
        return 1;
    }
    
    env = getenv("DESKTOP_SESSION");
    if (env && strcasecmp(env, "xfce") == 0)
    {
        vlog("DESKTOP_SESSION=%s\n", env);
        return 1;
    }

    return 0;
}

static int is_qt_env(void)
{
    return 0;
}

static int detect_gtk_version(int libflag)
{
    int gtk2;
    int gtk3;
    int gtk4;
    int glade2;

    gtk2 = libflag & LIB_FLAG_GTK2;
    gtk3 = libflag & LIB_FLAG_GTK3;
    gtk4 = libflag & LIB_FLAG_GTK4;
    glade2 = libflag & LIB_FLAG_GLADE2;

    if (gtk2 > 0 && glade2 > 0 && (gtk3 == 0 && gtk4 == 0))
    {
        return 2;
    }

    if (gtk3 > 0 && (gtk2 == 0 && gtk4 == 0))
    {
        return 3;
    }
    
    if (gtk4 > 0 && (gtk2 == 0 && gtk3 == 0))
    {
        return 4;
    }

    if (gtk3 > 0)
    {
        return 3;
    }

    if (gtk4 > 0)
    {
        return 4;
    }

    if (gtk2 > 0 && glade2 > 0)
    {
        return 2;
    }

    return 0;
}

static int detect_qt_version(int libflag)
{
    int qt4;
    int qt5;
    int qt6;

    qt4 = libflag & LIB_FLAG_QT4;
    qt5 = libflag & LIB_FLAG_QT5;
    qt6 = libflag & LIB_FLAG_QT6;

    if (qt4 > 0 && (qt5 == 0 && qt6 == 0))
    {
        return 4;
    }

    if (qt5 > 0 && (qt4 == 0 && qt6 == 0))
    {
        return 5;
    }
    
    if (qt6 > 0 && (qt4 == 0 && qt5 == 0))
    {
        return 6;
    }

    if (qt5 > 0)
    {
        return 5;
    }

    if (qt4 > 0)
    {
        return 4;
    }

    if (qt6 > 0)
    {
        return 6;
    }

    return 0;
}

int bit_from_machine(const char *machine)
{
    if (strstr(machine, "64"))
    {
        return 64;
    }
    else
    {
        return 32;
    }
}

int get_os_bit(int *bit)
{
    int ret;
    struct utsname unameData;

    memset(&unameData, 0, sizeof(unameData));
    ret = uname(&unameData);
    if (ret != 0)
    {
        vlog("uname error, code: %d\n", errno);
        return 1;
    }

    *bit = strstr(unameData.machine, "64") ? 64 : 32;
    vlog("uname -m <%s> %dbit\n", unameData.machine, *bit);
    
    return 0;
}

int read_file_1st_line(const char *file, char *buffer, int buflen)
{
    FILE *fp = NULL;

    fp = fopen(file, "r");
    if (fp == NULL)
    {
        vlog("Failed to open file %s code:%d", file, errno);
        return 1;
    }

    fgets(buffer, buflen, fp);
    fclose(fp);
    return 0;
}

static int read_pid_cmdline(long pid, char *Buffer, int BufLen)
{
    char path[256];
    
    snprintf(path, sizeof(path), "/proc/%ld/cmdline", pid);
    return read_file_1st_line(path, Buffer, BufLen);
}

static int find_exe_path(const char *exe, char *pathbuf, int buflen)
{
    int i;
    char path[PATH_MAX];
    char *tmpptr = NULL;
    char *saveptr = NULL;
    char *newenv = NULL;
    const char *env = getenv("PATH");

    if (NULL == env)
    {
        return 0;
    }

    newenv = strdup(env);
    if (!newenv)
    {
        return 0;
    }

    tmpptr = newenv;
    while (NULL != (tmpptr = strtok_r(tmpptr, ":", &saveptr)))
	{
        snprintf(path, sizeof(path), "%s/%s", tmpptr, exe);
        if (access(path, F_OK) != -1)
        {
            snprintf(pathbuf, buflen, "%s", path);
            free(newenv);
            return 1;
        }
		tmpptr = NULL;
    }
    
    free(newenv);
    return 0;
}

void dump_args(const char *prefix, char **argv)
{
    int i = 0;
    
    vlog("=========%s ARGS BEGIN===========\n", prefix);
    while (argv[i])
    {
        vlog("argv[%d]=<%s>\n", i, argv[i]);
        i++;
    }
    vlog("=========%s ARGS END===========\n", prefix);
}

int pre_check(void)
{
    int ret;
    int bit;
    int buildbit;
    const char *env = NULL;

    env = getenv("DISPLAY");
    if (NULL == env || env[0] != ':')
    {
        vlog("DISPLAY not exist(%p). Not in X environment.\n", env);
        return 1;
    }

    ret = get_os_bit(&bit);
    if (ret)
    {
        vlog("Failed to get os bit.\n");
        return 1;
    }

    buildbit = strstr(VTOY_GUI_ARCH, "64") ? 64 : 32;
    vlog("Build bit is %d (%s)\n", buildbit, VTOY_GUI_ARCH);

    if (bit != buildbit)
    {
        vlog("Current system is %d bit (%s). Please run the correct VentoyGUI.\n", bit, VTOY_GUI_ARCH);
        return 1;
    }

    return 0;
}

static char * find_argv(int argc, char **argv, char *key)
{
    int i;
    int len;

    len = (int)strlen(key);
    for (i = 0; i < argc; i++)
    {
        if (strncmp(argv[i], key, len) == 0)
        {
            return argv[i];
        }
    }

    return NULL;
}

static int adjust_cur_dir(char *argv0)
{
    int ret = 2;
    char c;
    char *pos = NULL;
    char *end = NULL;

    if (argv0[0] == '.')
    {
        return 1;
    }

    for (pos = argv0; pos && *pos; pos++)
    {
        if (*pos == '/')
        {
            end = pos;
        }
    }

    if (end)
    {
        c = *end;
        *end = 0;
        ret = chdir(argv0);
        *end = c;
    }

    return ret;
}



static char **recover_environ_param(char *env)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int cnt = 0;
    char **newenvs = NULL;

    for (i = 0; env[i]; i++)
    {
        if (env[i] == '\n')
        {
            cnt++;
        }
    }

    newenvs = malloc(sizeof(char *) * (cnt + 1));
    if (!newenvs)
    {
        vlog("malloc new envs fail %d\n", cnt + 1);
        return NULL;
    }
    memset(newenvs, 0, sizeof(char *) * (cnt + 1));

    for (j = i = 0; env[i]; i++)
    {
        if (env[i] == '\n')
        {
            env[i] = 0;
            newenvs[k++] = env + j;
            j = i + 1;
        }
    }

    vlog("recover environ %d %d\n", cnt, k);
    return newenvs;
}

static int restart_main(int argc, char **argv, char *guiexe)
{
    int i = 0;
    int j = 0;
    char *para = NULL;
    char **envs = NULL;
    char *newargv[MAX_PARAS + 1] = { NULL };

    para = find_argv(argc, argv, VTOY_ENV_STR);
    if (!para)
    {
        vlog("failed to find %s\n", VTOY_ENV_STR);
        return 1;
    }

    newargv[j++] = guiexe;
    for (i = 1; i < argc && j < MAX_PARAS; i++)
    {
        if (strncmp(argv[i], "_vtoy_", 6) != 0)
        {
            newargv[j++] = argv[i];
        }
    }

    envs = recover_environ_param(para + strlen(VTOY_ENV_STR));
    if (envs)
    {
        vlog("recover success, argc=%d evecve <%s>\n", j, guiexe);
        execve(guiexe, newargv, envs); 
    }
    else
    {
        vlog("recover failed, argc=%d evecv <%s>\n", j, guiexe);
        execv(guiexe, newargv); 
    }

    return 1;
}

static char *create_environ_param(const char *prefix, char **envs)
{
    int i = 0;
    int cnt = 0;
    int envlen = 0;
    int prelen = 0;
    char *cur = NULL;
    char *para = NULL;

    prelen = strlen(prefix);
    for (i = 0; envs[i]; i++)
    {
        cnt++;
        envlen += strlen(envs[i]) + 1;
    }

    para = malloc(prelen + envlen);
    if (!para)
    {
        vlog("failed to malloc env str %d\n", prelen + envlen);
        return NULL;
    }

    cur = para;
    memcpy(cur, prefix, prelen);
    cur += prelen;

    for (i = 0; envs[i]; i++)
    {
        envlen = strlen(envs[i]);
        memcpy(cur, envs[i], envlen);

        cur[envlen] = '\n';
        cur += envlen + 1;
    }

    vlog("create environment param %d\n", cnt);
    return para;
}

static int restart_by_pkexec(int argc, char **argv, const char *curpath, const char *exe)
{
    int i = 0;
    int j = 0;
    char envcount[64];
    char path[PATH_MAX];
    char pkexec[PATH_MAX];
    char exepara[PATH_MAX];
    char *newargv[MAX_PARAS + 1] = { NULL };

    vlog("try restart self by pkexec ...\n");

    if (find_exe_path("pkexec", pkexec, sizeof(pkexec)))
    {
        vlog("Find pkexec at <%s>\n", pkexec);
    }
    else
    {
        vlog("pkexec not found\n");
        return 1;
    }

    if (argv[0][0] != '/')
    {
        snprintf(path, sizeof(path), "%s/%s", curpath, argv[0]);
    }
    else
    {
        snprintf(path, sizeof(path), "%s", argv[0]);
    }
    snprintf(exepara, sizeof(exepara), "%s%s", VTOY_GUI_PATH, exe);

    newargv[j++] = pkexec;
    newargv[j++] = path;
    for (i = 1; i < argc && j < MAX_PARAS - 2; i++)
    {
        newargv[j++] = argv[i];
    }
    newargv[j++] = create_environ_param(VTOY_ENV_STR, environ);
    newargv[j++] = exepara;

    dump_args("PKEXEC", newargv);
    execv(pkexec, newargv);

    return 1;
}

static int ld_cache_lib_check(const char *lib, int *flag)
{
    if (((*flag) & LIB_FLAG_GTK3) == 0)
    {
        if (strncmp(lib, "libgtk-3.so", 11) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_GTK3;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_GTK2) == 0)
    {
        if (strncmp(lib, "libgtk-x11-2.0.so", 17) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_GTK2;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_GTK4) == 0)
    {
        if (strncmp(lib, "libgtk-4.so", 11) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_GTK4;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_QT4) == 0)
    {
        if (strncmp(lib, "libqt4", 6) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_QT4;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_QT5) == 0)
    {
        if (strncmp(lib, "libqt5", 6) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_QT5;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_QT6) == 0)
    {
        if (strncmp(lib, "libqt6", 6) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_QT6;
            return 0;
        }
    }
    
    if (((*flag) & LIB_FLAG_GLADE2) == 0)
    {
        if (strncmp(lib, "libglade-2", 10) == 0)
        {
            vlog("LIB:<%s>\n", lib);
            *flag |= LIB_FLAG_GLADE2;
            return 0;
        }
    }

    return 0;
}

static int parse_ld_cache(int *flag)
{
    int fd;
    int format;
    unsigned int i;
    struct stat st;
    size_t offset = 0;
    size_t cache_size = 0;
    const char *cache_data = NULL;
    struct cache_file *cache = NULL;
    struct cache_file_new *cache_new = NULL;

    *flag = 0;
    
    fd = open(LD_CACHE_FILE, O_RDONLY);
    if (fd < 0)
    {
        vlog("failed to open %s err:%d\n", LD_CACHE_FILE, errno);
        return 1;
    }

    if (fstat(fd, &st) < 0 || st.st_size == 0)
    {
        close(fd);
        return 1;
    }

    cache = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (cache == MAP_FAILED)
    {
        close(fd);
        return 1;
    }

    cache_size = st.st_size;
    if (cache_size < sizeof (struct cache_file))
    {
        vlog("File is not a cache file.\n");
        munmap (cache, cache_size);
        close(fd);
        return 1;
    }

    if (memcmp(cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1))
    {
        /* This can only be the new format without the old one.  */
        cache_new = (struct cache_file_new *) cache;

        if (memcmp(cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1) ||
            memcmp (cache_new->version, CACHE_VERSION, sizeof CACHE_VERSION - 1))
        {
            munmap (cache, cache_size);
            close(fd);
            return 1;
        }
          
        format = 1;
        /* This is where the strings start.  */
        cache_data = (const char *) cache_new;
    }
    else
    {
        /* Check for corruption, avoiding overflow.  */
        if ((cache_size - sizeof (struct cache_file)) / sizeof (struct file_entry) < cache->nlibs)
        {
            vlog("File is not a cache file.\n");
            munmap (cache, cache_size);
            close(fd);
            return 1;
        }

        offset = ALIGN_CACHE(sizeof (struct cache_file) + (cache->nlibs * sizeof (struct file_entry)));
        
        /* This is where the strings start.  */
        cache_data = (const char *) &cache->libs[cache->nlibs];

        /* Check for a new cache embedded in the old format.  */
        if (cache_size > (offset + sizeof (struct cache_file_new)))
        {
            cache_new = (struct cache_file_new *) ((void *)cache + offset);

            if (memcmp(cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1) == 0 &&
                memcmp(cache_new->version, CACHE_VERSION, sizeof CACHE_VERSION - 1) == 0)
            {
                cache_data = (const char *) cache_new;
                format = 1;
            }
        }
    }

    if (format == 0)
    {
        vlog("%d libs found in cache format 0\n", cache->nlibs);
        for (i = 0; i < cache->nlibs; i++)
        {
            ld_cache_lib_check(cache_data + cache->libs[i].key, flag);
        }
    }
    else if (format == 1)
    {
        vlog("%d libs found in cache format 1\n", cache_new->nlibs);

        for (i = 0; i < cache_new->nlibs; i++)
        {
            ld_cache_lib_check(cache_data + cache_new->libs[i].key, flag);
        }
    }

    vlog("ldconfig lib flags 0x%x\n", *flag);
    vlog("lib flags GLADE2:[%s] GTK2:[%s] GTK3:[%s] GTK4:[%s] QT4:[%s] QT5:[%s] QT6:[%s]\n", 
        INT2STR_YN((*flag) & LIB_FLAG_GLADE2), INT2STR_YN((*flag) & LIB_FLAG_GTK2),
        INT2STR_YN((*flag) & LIB_FLAG_GTK3), INT2STR_YN((*flag) & LIB_FLAG_GTK4),
        INT2STR_YN((*flag) & LIB_FLAG_QT4), INT2STR_YN((*flag) & LIB_FLAG_QT5),
        INT2STR_YN((*flag) & LIB_FLAG_QT6));

    munmap (cache, cache_size);
    close (fd);
    return 0;
}

static int detect_gui_exe_path(const char *curpath, char *pathbuf, int buflen)
{
    int ret;
    int ver;
    int libflag = 0;
    const char *guitype = NULL;
    char line[256];
    mode_t mode;
    struct stat filestat;
    
    if (access("./ventoy_gui_type", F_OK) != -1)
    {
        vlog("Get GUI type from ventoy_gui_type file.\n");
    
        line[0] = 0;
        read_file_1st_line("./ventoy_gui_type", line, sizeof(line));
        if (strncmp(line, "gtk2", 4) == 0)
        {
            guitype = "gtk";
            ver = 2;

            parse_ld_cache(&libflag);
            if ((libflag & LIB_FLAG_GLADE2) == 0)
            {
                vlog("libglade2 is necessary for GTK2, but not found.\n");
                return 1;
            }
        }
        else if (strncmp(line, "gtk3", 4) == 0)
        {
            guitype = "gtk";
            ver = 3;
        }
        else if (strncmp(line, "gtk4", 4) == 0)
        {
            guitype = "gtk";
            ver = 4;
        }
        else if (strncmp(line, "qt4", 3) == 0)
        {
            guitype = "qt";
            ver = 4;
        }
        else if (strncmp(line, "qt5", 3) == 0)
        {
            guitype = "qt";
            ver = 5;
        }
        else if (strncmp(line, "qt6", 3) == 0)
        {
            guitype = "qt";
            ver = 6;
        }
        else
        {
            vlog("Current X environment is NOT supported.\n");
            return 1;
        }
    }
    else
    {
        vlog("Now detect the GUI type ...\n");

        parse_ld_cache(&libflag);

        if ((LIB_FLAG_GTK & libflag) > 0 && (LIB_FLAG_QT & libflag) == 0)
        {
            guitype = "gtk";
            ver = detect_gtk_version(libflag);
        }
        else if ((LIB_FLAG_GTK & libflag) == 0 && (LIB_FLAG_QT & libflag) > 0)
        {
            guitype = "qt";
            ver = detect_qt_version(libflag);
        }
        else if ((LIB_FLAG_GTK & libflag) > 0 && (LIB_FLAG_QT & libflag) > 0)
        {
            if (is_gtk_env())
            {
                guitype = "gtk";
                ver = detect_gtk_version(libflag);
            }
            else if (is_qt_env())
            {
                guitype = "qt";
                ver = detect_qt_version(libflag);
            }
            else
            {
                vlog("Current X environment is NOT supported.\n");
                return 1;
            }
        }
        else
        {
            vlog("Current X environment is NOT supported.\n");
            return 1;
        }
    }

    snprintf(pathbuf, buflen, "%s/tool/%s/Ventoy2Disk.%s%d", curpath, VTOY_GUI_ARCH, guitype, ver);

    vlog("This is %s%d X environment.\n", guitype, ver);
    vlog("exe = %s\n", pathbuf);
    
    if (access(pathbuf, F_OK) == -1)
    {
        vlog("%s is not exist.\n", pathbuf);
        return 1;
    }

    if (access(pathbuf, X_OK) == -1)
    {
        vlog("execute permission check fail, try chmod.\n", pathbuf);
        if (stat(pathbuf, &filestat) == 0)
        {
            mode = filestat.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
            ret = chmod(pathbuf, mode);
            vlog("old mode=%o new mode=%o ret=%d\n", filestat.st_mode, mode, ret);
        }
    }
    else
    {
        vlog("execute permission check success.\n");
    }

    return 0;
}

int real_main(int argc, char **argv)
{
    int ret;
    int euid;
    char *exe = NULL;
    char path[PATH_MAX];
    char curpath[PATH_MAX];

    ret = adjust_cur_dir(argv[0]);

    vlog("\n");
    vlog("=========================================================\n");
    vlog("=========================================================\n");
    vlog("=============== VentoyGui %s ===============\n", VTOY_GUI_ARCH);
    vlog("=========================================================\n");
    vlog("=========================================================\n");

    euid = geteuid();
    getcwd(curpath, sizeof(curpath));

    vlog("pid:%ld ppid:%ld uid:%d euid:%d\n", (long)getpid(), (long)getppid(), getuid(), euid);
    vlog("adjust dir:%d  current path:%s\n", ret, curpath);
    dump_args("RAW", argv);

    if (access("./boot/boot.img", F_OK) == -1)
    {
        vlog("Please run under the correct directory!\n");
        return 1;
    }

    exe = find_argv(argc, argv, VTOY_GUI_PATH);
    if (exe)
    {
        if (euid != 0)
        {
            vlog("Invalid euid %d when restart.\n", euid);
            return 1;
        }

        return restart_main(argc, argv, exe + strlen(VTOY_GUI_PATH));
    }
    else
    {
        if (pre_check())
        {
            return 1;
        }

        if (detect_gui_exe_path(curpath, path, sizeof(path)))
        {
            return 1;
        }

        if (euid == 0)
        {
            vlog("We have root privileges, just exec %s\n", path);
            argv[0] = path;
            argv[1] = NULL;
            execv(argv[0], argv);
        }
        else
        {
            vlog("EUID check failed.\n");

            /* try pkexec */
            restart_by_pkexec(argc, argv, curpath, path);

            vlog("### Please run with root privileges. ###\n");
            return 1;
        }
    }

    return 1;
}

int main(int argc, char **argv)
{
    int ret;

    g_log_buf = malloc(MAX_LOG_BUF);
    if (!g_log_buf)
    {
        vlog("Failed to malloc log buffer %d\n", MAX_LOG_BUF);
        return 1;
    }

    ret = real_main(argc, argv);
    free(g_log_buf);
    return ret;
}