forked from rpm-software-management/rpm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpmrc.c
1907 lines (1636 loc) · 46.5 KB
/
rpmrc.c
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
#include "system.h"
#include <stdarg.h>
#include <pthread.h>
#if defined(__linux__)
#include <elf.h>
#include <link.h>
#endif
#if HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#include <ctype.h> /* XXX for /etc/rpm/platform contents */
#if HAVE_SYS_SYSTEMCFG_H
#include <sys/systemcfg.h>
#else
#define __power_pc() 0
#endif
#ifdef HAVE_SYS_AUXV_H
#include <sys/auxv.h>
#endif
#include <rpm/rpmlib.h> /* RPM_MACTABLE*, Rc-prototypes */
#include <rpm/rpmmacro.h>
#include <rpm/rpmfileutil.h>
#include <rpm/rpmstring.h>
#include <rpm/rpmlog.h>
#include <rpm/argv.h>
#include "rpmio/rpmlua.h"
#include "rpmio/rpmio_internal.h" /* XXX for rpmioSlurp */
#include "lib/misc.h"
#include "lib/rpmliblua.h"
#include "lib/rpmug.h"
#include "debug.h"
static const char * defrcfiles = NULL;
const char * macrofiles = NULL;
typedef struct machCacheEntry_s {
char * name;
int count;
char ** equivs;
int visited;
} * machCacheEntry;
typedef struct machCache_s {
machCacheEntry cache;
int size;
} * machCache;
typedef struct machEquivInfo_s {
char * name;
int score;
} * machEquivInfo;
typedef struct machEquivTable_s {
int count;
machEquivInfo list;
} * machEquivTable;
struct rpmvarValue {
char * value;
/* eventually, this arch will be replaced with a generic condition */
char * arch;
struct rpmvarValue * next;
};
struct rpmOption {
char * name;
int var;
int archSpecific;
int macroize;
int localize;
};
static struct rpmat_s {
const char *platform;
uint64_t hwcap;
uint64_t hwcap2;
} rpmat;
typedef struct defaultEntry_s {
char * name;
char * defName;
} * defaultEntry;
typedef struct canonEntry_s {
char * name;
char * short_name;
short num;
} * canonEntry;
/* tags are 'key'canon, 'key'translate, 'key'compat
*
* for giggles, 'key'_canon, 'key'_compat, and 'key'_canon will also work
*/
typedef struct tableType_s {
char * const key;
const int hasCanon;
const int hasTranslate;
struct machEquivTable_s equiv;
struct machCache_s cache;
defaultEntry defaults;
canonEntry canons;
int defaultsLength;
int canonsLength;
} * tableType;
/* XXX get rid of this stuff... */
/* Stuff for maintaining "variables" like SOURCEDIR, BUILDDIR, etc */
#define RPMVAR_OPTFLAGS 3
#define RPMVAR_ARCHCOLOR 42
#define RPMVAR_INCLUDE 43
#define RPMVAR_MACROFILES 49
#define RPMVAR_NUM 55 /* number of RPMVAR entries */
/* this *must* be kept in alphabetical order */
/* The order of the flags is archSpecific, macroize, localize */
static const struct rpmOption optionTable[] = {
{ "archcolor", RPMVAR_ARCHCOLOR, 1, 0, 0 },
{ "include", RPMVAR_INCLUDE, 0, 0, 2 },
{ "macrofiles", RPMVAR_MACROFILES, 0, 0, 1 },
{ "optflags", RPMVAR_OPTFLAGS, 1, 1, 0 },
};
static const size_t optionTableSize = sizeof(optionTable) / sizeof(*optionTable);
#define OS 0
#define ARCH 1
typedef struct rpmrcCtx_s * rpmrcCtx;
struct rpmrcCtx_s {
ARGV_t platpat;
char *current[2];
int currTables[2];
struct rpmvarValue values[RPMVAR_NUM];
struct tableType_s tables[RPM_MACHTABLE_COUNT];
int machDefaults;
int pathDefaults;
pthread_rwlock_t lock;
};
/* prototypes */
static rpmRC doReadRC(rpmrcCtx ctx, const char * urlfn);
static void rpmSetVarArch(rpmrcCtx ctx,
int var, const char * val, const char * arch);
static void rebuildCompatTables(rpmrcCtx ctx, int type, const char * name);
static void rpmRebuildTargetVars(rpmrcCtx ctx, const char **target, const char ** canontarget);
/* Force context (lock) acquisition through a function */
static rpmrcCtx rpmrcCtxAcquire(int write)
{
static struct rpmrcCtx_s _globalCtx = {
.lock = PTHREAD_RWLOCK_INITIALIZER,
.currTables = { RPM_MACHTABLE_INSTOS, RPM_MACHTABLE_INSTARCH },
.tables = {
{ "arch", 1, 0 },
{ "os", 1, 0 },
{ "buildarch", 0, 1 },
{ "buildos", 0, 1 }
},
};
rpmrcCtx ctx = &_globalCtx;
/* XXX: errors should be handled */
if (write)
pthread_rwlock_wrlock(&ctx->lock);
else
pthread_rwlock_rdlock(&ctx->lock);
return ctx;
}
/* Release context (lock) */
static rpmrcCtx rpmrcCtxRelease(rpmrcCtx ctx)
{
pthread_rwlock_unlock(&ctx->lock);
return NULL;
}
static int optionCompare(const void * a, const void * b)
{
return rstrcasecmp(((const struct rpmOption *) a)->name,
((const struct rpmOption *) b)->name);
}
static machCacheEntry
machCacheFindEntry(const machCache cache, const char * key)
{
int i;
for (i = 0; i < cache->size; i++)
if (rstreq(cache->cache[i].name, key)) return cache->cache + i;
return NULL;
}
static int machCompatCacheAdd(char * name, const char * fn, int linenum,
machCache cache)
{
machCacheEntry entry = NULL;
char * chptr;
char * equivs;
int delEntry = 0;
int i;
while (*name && risspace(*name)) name++;
chptr = name;
while (*chptr && *chptr != ':') chptr++;
if (!*chptr) {
rpmlog(RPMLOG_ERR, _("missing second ':' at %s:%d\n"), fn, linenum);
return 1;
} else if (chptr == name) {
rpmlog(RPMLOG_ERR, _("missing architecture name at %s:%d\n"), fn,
linenum);
return 1;
}
while (*chptr == ':' || risspace(*chptr)) chptr--;
*(++chptr) = '\0';
equivs = chptr + 1;
while (*equivs && risspace(*equivs)) equivs++;
if (!*equivs) {
delEntry = 1;
}
if (cache->size) {
entry = machCacheFindEntry(cache, name);
if (entry) {
for (i = 0; i < entry->count; i++)
entry->equivs[i] = _free(entry->equivs[i]);
entry->equivs = _free(entry->equivs);
entry->count = 0;
}
}
if (!entry) {
cache->cache = xrealloc(cache->cache,
(cache->size + 1) * sizeof(*cache->cache));
entry = cache->cache + cache->size++;
entry->name = xstrdup(name);
entry->count = 0;
entry->visited = 0;
}
if (delEntry) return 0;
while ((chptr = strtok(equivs, " ")) != NULL) {
equivs = NULL;
if (chptr[0] == '\0') /* does strtok() return "" ever?? */
continue;
if (entry->count)
entry->equivs = xrealloc(entry->equivs, sizeof(*entry->equivs)
* (entry->count + 1));
else
entry->equivs = xmalloc(sizeof(*entry->equivs));
entry->equivs[entry->count] = xstrdup(chptr);
entry->count++;
}
return 0;
}
static machEquivInfo
machEquivSearch(const machEquivTable table, const char * name)
{
int i;
for (i = 0; i < table->count; i++)
if (!rstrcasecmp(table->list[i].name, name))
return table->list + i;
return NULL;
}
static void machAddEquiv(machEquivTable table, const char * name,
int distance)
{
machEquivInfo equiv;
equiv = machEquivSearch(table, name);
if (!equiv) {
if (table->count)
table->list = xrealloc(table->list, (table->count + 1)
* sizeof(*table->list));
else
table->list = xmalloc(sizeof(*table->list));
table->list[table->count].name = xstrdup(name);
table->list[table->count++].score = distance;
}
}
static void machCacheEntryVisit(machCache cache,
machEquivTable table, const char * name, int distance)
{
machCacheEntry entry;
int i;
entry = machCacheFindEntry(cache, name);
if (!entry || entry->visited) return;
entry->visited = 1;
for (i = 0; i < entry->count; i++) {
machAddEquiv(table, entry->equivs[i], distance);
}
for (i = 0; i < entry->count; i++) {
machCacheEntryVisit(cache, table, entry->equivs[i], distance + 1);
}
}
static void machFindEquivs(machCache cache, machEquivTable table,
const char * key)
{
int i;
for (i = 0; i < cache->size; i++)
cache->cache[i].visited = 0;
while (table->count > 0) {
--table->count;
table->list[table->count].name = _free(table->list[table->count].name);
}
table->count = 0;
table->list = _free(table->list);
/*
* We have a general graph built using strings instead of pointers.
* Yuck. We have to start at a point at traverse it, remembering how
* far away everything is.
*/
/* FIX: table->list may be NULL. */
machAddEquiv(table, key, 1);
machCacheEntryVisit(cache, table, key, 2);
return;
}
static rpmRC addCanon(canonEntry * table, int * tableLen, char * line,
const char * fn, int lineNum)
{
canonEntry t;
char *s, *s1;
const char * tname;
const char * tshort_name;
int tnum;
(*tableLen) += 2;
*table = xrealloc(*table, sizeof(**table) * (*tableLen));
t = & ((*table)[*tableLen - 2]);
tname = strtok(line, ": \t");
tshort_name = strtok(NULL, " \t");
s = strtok(NULL, " \t");
if (! (tname && tshort_name && s)) {
rpmlog(RPMLOG_ERR, _("Incomplete data line at %s:%d\n"),
fn, lineNum);
return RPMRC_FAIL;
}
if (strtok(NULL, " \t")) {
rpmlog(RPMLOG_ERR, _("Too many args in data line at %s:%d\n"),
fn, lineNum);
return RPMRC_FAIL;
}
tnum = strtoul(s, &s1, 10);
if ((*s1) || (s1 == s) || (tnum == ULONG_MAX)) {
rpmlog(RPMLOG_ERR, _("Bad arch/os number: %s (%s:%d)\n"), s,
fn, lineNum);
return RPMRC_FAIL;
}
t[0].name = xstrdup(tname);
t[0].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
t[0].num = tnum;
/* From A B C entry */
/* Add B B C entry */
t[1].name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
t[1].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
t[1].num = tnum;
return RPMRC_OK;
}
static rpmRC addDefault(defaultEntry * table, int * tableLen, char * line,
const char * fn, int lineNum)
{
defaultEntry t;
(*tableLen)++;
*table = xrealloc(*table, sizeof(**table) * (*tableLen));
t = & ((*table)[*tableLen - 1]);
t->name = strtok(line, ": \t");
t->defName = strtok(NULL, " \t");
if (! (t->name && t->defName)) {
rpmlog(RPMLOG_ERR, _("Incomplete default line at %s:%d\n"),
fn, lineNum);
return RPMRC_FAIL;
}
if (strtok(NULL, " \t")) {
rpmlog(RPMLOG_ERR, _("Too many args in default line at %s:%d\n"),
fn, lineNum);
return RPMRC_FAIL;
}
t->name = xstrdup(t->name);
t->defName = (t->defName ? xstrdup(t->defName) : NULL);
return RPMRC_OK;
}
static canonEntry lookupInCanonTable(const char * name,
const canonEntry table, int tableLen)
{
while (tableLen) {
tableLen--;
if (!rstreq(name, table[tableLen].name))
continue;
return &(table[tableLen]);
}
return NULL;
}
static
const char * lookupInDefaultTable(const char * name,
const defaultEntry table, int tableLen)
{
while (tableLen) {
tableLen--;
if (table[tableLen].name && rstreq(name, table[tableLen].name))
return table[tableLen].defName;
}
return name;
}
static void setDefaults(void)
{
const char *confdir = rpmConfigDir();
if (!defrcfiles) {
defrcfiles = rstrscat(NULL, confdir, "/rpmrc", ":",
confdir, "/" RPMCANONVENDOR "/rpmrc", ":",
SYSCONFDIR "/rpmrc", ":",
"~/.rpmrc", NULL);
}
#ifndef MACROFILES
if (!macrofiles) {
macrofiles = rstrscat(NULL, confdir, "/macros", ":",
confdir, "/macros.d/macros.*", ":",
confdir, "/platform/%{_target}/macros", ":",
confdir, "/fileattrs/*.attr", ":",
confdir, "/" RPMCANONVENDOR "/macros", ":",
SYSCONFDIR "/rpm/macros.*", ":",
SYSCONFDIR "/rpm/macros", ":",
SYSCONFDIR "/rpm/%{_target}/macros", ":",
"~/.rpmmacros", NULL);
}
#else
macrofiles = MACROFILES;
#endif
}
/* FIX: se usage inconsistent, W2DO? */
static rpmRC doReadRC(rpmrcCtx ctx, const char * urlfn)
{
char *s;
char *se, *next, *buf = NULL, *fn;
int linenum = 0;
struct rpmOption searchOption, * option;
rpmRC rc = RPMRC_FAIL;
fn = rpmGetPath(urlfn, NULL);
if (rpmioSlurp(fn, (uint8_t **) &buf, NULL) || buf == NULL) {
goto exit;
}
next = buf;
while (*next != '\0') {
linenum++;
s = se = next;
/* Find end-of-line. */
while (*se && *se != '\n') se++;
if (*se != '\0') *se++ = '\0';
next = se;
/* Trim leading spaces */
while (*s && risspace(*s)) s++;
/* We used to allow comments to begin anywhere, but not anymore. */
if (*s == '#' || *s == '\0') continue;
/* Find end-of-keyword. */
se = (char *)s;
while (*se && !risspace(*se) && *se != ':') se++;
if (risspace(*se)) {
*se++ = '\0';
while (*se && risspace(*se) && *se != ':') se++;
}
if (*se != ':') {
rpmlog(RPMLOG_ERR, _("missing ':' (found 0x%02x) at %s:%d\n"),
(unsigned)(0xff & *se), fn, linenum);
goto exit;
}
*se++ = '\0'; /* terminate keyword or option, point to value */
while (*se && risspace(*se)) se++;
/* Find keyword in table */
searchOption.name = s;
option = bsearch(&searchOption, optionTable, optionTableSize,
sizeof(optionTable[0]), optionCompare);
if (option) { /* For configuration variables ... */
const char *arch, *val;
arch = val = NULL;
if (*se == '\0') {
rpmlog(RPMLOG_ERR, _("missing argument for %s at %s:%d\n"),
option->name, fn, linenum);
goto exit;
}
if (option->var == RPMVAR_INCLUDE) {
s = se;
while (*se && !risspace(*se)) se++;
if (*se != '\0') *se = '\0';
if (doReadRC(ctx, s)) {
rpmlog(RPMLOG_ERR, _("cannot open %s at %s:%d: %m\n"),
s, fn, linenum);
goto exit;
}
/* XXX don't save include value as var/macro */
continue;
}
if (option->archSpecific) {
arch = se;
while (*se && !risspace(*se)) se++;
if (*se == '\0') {
rpmlog(RPMLOG_ERR,
_("missing architecture for %s at %s:%d\n"),
option->name, fn, linenum);
goto exit;
}
*se++ = '\0';
while (*se && risspace(*se)) se++;
if (*se == '\0') {
rpmlog(RPMLOG_ERR,
_("missing argument for %s at %s:%d\n"),
option->name, fn, linenum);
goto exit;
}
}
val = se;
/* Only add macros if appropriate for this arch */
if (option->macroize &&
(arch == NULL || rstreq(arch, ctx->current[ARCH]))) {
char *n, *name;
n = name = xmalloc(strlen(option->name)+2);
if (option->localize)
*n++ = '_';
strcpy(n, option->name);
rpmPushMacro(NULL, name, NULL, val, RMIL_RPMRC);
free(name);
}
rpmSetVarArch(ctx, option->var, val, arch);
fn = _free(fn);
} else { /* For arch/os compatibility tables ... */
int gotit;
int i;
gotit = 0;
for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
if (rstreqn(ctx->tables[i].key, s, strlen(ctx->tables[i].key)))
break;
}
if (i < RPM_MACHTABLE_COUNT) {
const char *rest = s + strlen(ctx->tables[i].key);
if (*rest == '_') rest++;
if (rstreq(rest, "compat")) {
if (machCompatCacheAdd(se, fn, linenum,
&ctx->tables[i].cache))
goto exit;
gotit = 1;
} else if (ctx->tables[i].hasTranslate &&
rstreq(rest, "translate")) {
if (addDefault(&ctx->tables[i].defaults,
&ctx->tables[i].defaultsLength,
se, fn, linenum))
goto exit;
gotit = 1;
} else if (ctx->tables[i].hasCanon &&
rstreq(rest, "canon")) {
if (addCanon(&ctx->tables[i].canons,
&ctx->tables[i].canonsLength,
se, fn, linenum))
goto exit;
gotit = 1;
}
}
if (!gotit) {
rpmlog(RPMLOG_ERR, _("bad option '%s' at %s:%d\n"),
s, fn, linenum);
goto exit;
}
}
}
rc = RPMRC_OK;
exit:
free(fn);
free(buf);
return rc;
}
/**
*/
static rpmRC rpmPlatform(rpmrcCtx ctx, const char * platform)
{
const char *cpu = NULL, *vendor = NULL, *os = NULL, *gnu = NULL;
uint8_t * b = NULL;
ssize_t blen = 0;
int init_platform = 0;
char * p, * pe;
rpmRC rc;
rc = (rpmioSlurp(platform, &b, &blen) == 0) ? RPMRC_OK : RPMRC_FAIL;
if (rc || b == NULL || blen <= 0) {
rc = RPMRC_FAIL;
goto exit;
}
p = (char *)b;
for (pe = p; p && *p; p = pe) {
pe = strchr(p, '\n');
if (pe)
*pe++ = '\0';
while (*p && isspace(*p))
p++;
if (*p == '\0' || *p == '#')
continue;
if (init_platform) {
char * t = p + strlen(p);
while (--t > p && isspace(*t))
*t = '\0';
if (t > p) {
argvAdd(&ctx->platpat, p);
}
continue;
}
cpu = p;
vendor = "unknown";
os = "unknown";
gnu = NULL;
while (*p && !(*p == '-' || isspace(*p)))
p++;
if (*p != '\0') *p++ = '\0';
vendor = p;
while (*p && !(*p == '-' || isspace(*p)))
p++;
if (*p != '-') {
if (*p != '\0') *p = '\0';
os = vendor;
vendor = "unknown";
} else {
if (*p != '\0') *p++ = '\0';
os = p;
while (*p && !(*p == '-' || isspace(*p)))
p++;
if (*p == '-') {
*p++ = '\0';
gnu = p;
while (*p && !(*p == '-' || isspace(*p)))
p++;
}
if (*p != '\0') *p = '\0';
}
rpmPushMacro(NULL, "_host_cpu", NULL, cpu, -1);
rpmPushMacro(NULL, "_host_vendor", NULL, vendor, -1);
rpmPushMacro(NULL, "_host_os", NULL, os, -1);
char *plat = rpmExpand("%{_host_cpu}-%{_host_vendor}-%{_host_os}",
(gnu && *gnu ? "-" : NULL), gnu, NULL);
argvAdd(&ctx->platpat, plat);
free(plat);
init_platform++;
}
rc = (init_platform ? RPMRC_OK : RPMRC_FAIL);
exit:
b = _free(b);
return rc;
}
# if defined(__linux__) && defined(__i386__)
#include <setjmp.h>
#include <signal.h>
/*
* Generic CPUID function
*/
static inline void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
{
asm volatile (
"pushl %%ebx \n"
"cpuid \n"
"movl %%ebx, %%esi \n"
"popl %%ebx \n"
: "=a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
: "a" (op));
}
/*
* CPUID functions returning a single datum
*/
static inline unsigned int cpuid_eax(unsigned int op)
{
unsigned int tmp, val;
cpuid(op, &val, &tmp, &tmp, &tmp);
return val;
}
static inline unsigned int cpuid_ebx(unsigned int op)
{
unsigned int tmp, val;
cpuid(op, &tmp, &val, &tmp, &tmp);
return val;
}
static inline unsigned int cpuid_ecx(unsigned int op)
{
unsigned int tmp, val;
cpuid(op, &tmp, &tmp, &val, &tmp);
return val;
}
static inline unsigned int cpuid_edx(unsigned int op)
{
unsigned int tmp, val;
cpuid(op, &tmp, &tmp, &tmp, &val);
return val;
}
static sigjmp_buf jenv;
static inline void model3(int _unused)
{
siglongjmp(jenv, 1);
}
static inline int RPMClass(void)
{
int cpu;
unsigned int tfms, junk, cap, capamd;
struct sigaction oldsa;
sigaction(SIGILL, NULL, &oldsa);
signal(SIGILL, model3);
if (sigsetjmp(jenv, 1)) {
sigaction(SIGILL, &oldsa, NULL);
return 3;
}
if (cpuid_eax(0x000000000)==0) {
sigaction(SIGILL, &oldsa, NULL);
return 4;
}
cpuid(0x00000001, &tfms, &junk, &junk, &cap);
cpuid(0x80000001, &junk, &junk, &junk, &capamd);
cpu = (tfms>>8)&15;
if (cpu == 5
&& cpuid_ecx(0) == '68xM'
&& cpuid_edx(0) == 'Teni'
&& (cpuid_edx(1) & ((1<<8)|(1<<15))) == ((1<<8)|(1<<15))) {
sigaction(SIGILL, &oldsa, NULL);
return 6; /* has CX8 and CMOV */
}
sigaction(SIGILL, &oldsa, NULL);
#define USER686 ((1<<4) | (1<<8) | (1<<15))
/* Transmeta Crusoe CPUs say that their CPU family is "5" but they have enough features for i686. */
if (cpu == 5 && (cap & USER686) == USER686)
return 6;
if (cpu < 6)
return cpu;
if (cap & (1<<15)) {
/* CMOV supported? */
if (capamd & (1<<30))
return 7; /* 3DNOWEXT supported */
return 6;
}
return 5;
}
/* should only be called for model 6 CPU's */
static int is_athlon(void)
{
unsigned int eax, ebx, ecx, edx;
char vendor[16];
int i;
cpuid (0, &eax, &ebx, &ecx, &edx);
memset(vendor, 0, sizeof(vendor));
for (i=0; i<4; i++)
vendor[i] = (unsigned char) (ebx >>(8*i));
for (i=0; i<4; i++)
vendor[4+i] = (unsigned char) (edx >>(8*i));
for (i=0; i<4; i++)
vendor[8+i] = (unsigned char) (ecx >>(8*i));
if (!rstreqn(vendor, "AuthenticAMD", 12))
return 0;
return 1;
}
static int is_pentium3(void)
{
unsigned int eax, ebx, ecx, edx, family, model;
char vendor[16];
cpuid(0, &eax, &ebx, &ecx, &edx);
memset(vendor, 0, sizeof(vendor));
*((unsigned int *)&vendor[0]) = ebx;
*((unsigned int *)&vendor[4]) = edx;
*((unsigned int *)&vendor[8]) = ecx;
if (!rstreqn(vendor, "GenuineIntel", 12))
return 0;
cpuid(1, &eax, &ebx, &ecx, &edx);
family = (eax >> 8) & 0x0f;
model = (eax >> 4) & 0x0f;
if (family == 6)
switch (model)
{
case 7: // Pentium III, Pentium III Xeon (model 7)
case 8: // Pentium III, Pentium III Xeon, Celeron (model 8)
case 9: // Pentium M
case 10: // Pentium III Xeon (model A)
case 11: // Pentium III (model B)
return 1;
}
return 0;
}
static int is_pentium4(void)
{
unsigned int eax, ebx, ecx, edx, family, model;
char vendor[16];
cpuid(0, &eax, &ebx, &ecx, &edx);
memset(vendor, 0, sizeof(vendor));
*((unsigned int *)&vendor[0]) = ebx;
*((unsigned int *)&vendor[4]) = edx;
*((unsigned int *)&vendor[8]) = ecx;
if (!rstreqn(vendor, "GenuineIntel", 12))
return 0;
cpuid(1, &eax, &ebx, &ecx, &edx);
family = (eax >> 8) & 0x0f;
model = (eax >> 4) & 0x0f;
if (family == 15)
switch (model)
{
case 0: // Pentium 4, Pentium 4 Xeon (0.18um)
case 1: // Pentium 4, Pentium 4 Xeon MP, Celeron (0.18um)
case 2: // Pentium 4, Mobile Pentium 4-M,
// Pentium 4 Xeon, Pentium 4 Xeon MP,
// Celeron, Mobile Celron (0.13um)
case 3: // Pentium 4, Celeron (0.09um)
return 1;
}
return 0;
}
static int is_geode(void)
{
unsigned int eax, ebx, ecx, edx, family, model;
char vendor[16];
memset(vendor, 0, sizeof(vendor));
cpuid(0, &eax, &ebx, &ecx, &edx);
memset(vendor, 0, sizeof(vendor));
*((unsigned int *)&vendor[0]) = ebx;
*((unsigned int *)&vendor[4]) = edx;
*((unsigned int *)&vendor[8]) = ecx;
if (!rstreqn(vendor, "AuthenticAMD", 12))
return 0;
cpuid(1, &eax, &ebx, &ecx, &edx);
family = (eax >> 8) & 0x0f;
model = (eax >> 4) & 0x0f;
if (family == 5)
switch (model)
{
case 10: // Geode
return 1;
}
return 0;
}
#endif
#if defined(__linux__)
#ifndef AT_HWCAP2 /* glibc < 2.18 */
#define AT_HWCAP2 26
#endif
/**
* Populate rpmat structure with auxv values
*/
static void read_auxv(void)
{
static int oneshot = 1;
if (oneshot) {
#ifdef HAVE_GETAUXVAL
rpmat.platform = (char *) getauxval(AT_PLATFORM);
if (!rpmat.platform)
rpmat.platform = "";
rpmat.hwcap = getauxval(AT_HWCAP);
rpmat.hwcap2 = getauxval(AT_HWCAP2);
#else
rpmat.platform = "";
int fd = open("/proc/self/auxv", O_RDONLY);
if (fd == -1) {
rpmlog(RPMLOG_WARNING,
_("Failed to read auxiliary vector, /proc not mounted?\n"));
return;
} else {
ElfW(auxv_t) auxv;
while (read(fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
switch (auxv.a_type)
{
case AT_NULL:
break;
case AT_PLATFORM:
rpmat.platform = strdup((char *) auxv.a_un.a_val);
break;
case AT_HWCAP:
rpmat.hwcap = auxv.a_un.a_val;
break;
case AT_HWCAP2:
rpmat.hwcap2 = auxv.a_un.a_val;
break;
}
}
close(fd);
}
#endif
oneshot = 0; /* only try once even if it fails */