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
| /*
** Copyright (c) 2006 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code for miscellaneous utility routines.
*/
#include "config.h"
#include "util.h"
#if defined(USE_MMAN_H)
# include <sys/mman.h>
# include <unistd.h>
#endif
#include <math.h>
/*
** For the fossil_timer_xxx() family of functions...
*/
#ifdef _WIN32
# include <windows.h>
#else
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include <fcntl.h>
# include <errno.h>
#endif
/*
** Exit. Take care to close the database first.
*/
NORETURN void fossil_exit(int rc){
db_close(1);
#ifndef _WIN32
if( g.fAnyTrace ){
fprintf(stderr, "/***** Subprocess %d exit(%d) *****/\n", getpid(), rc);
fflush(stderr);
}
#endif
exit(rc);
}
/*
** Malloc and free routines that cannot fail
*/
void *fossil_malloc(size_t n){
void *p = malloc(n==0 ? 1 : n);
if( p==0 ) fossil_fatal("out of memory");
return p;
}
void *fossil_malloc_zero(size_t n){
void *p = malloc(n==0 ? 1 : n);
if( p==0 ) fossil_fatal("out of memory");
memset(p, 0, n);
return p;
}
void fossil_free(void *p){
free(p);
}
void *fossil_realloc(void *p, size_t n){
p = realloc(p, n);
if( p==0 ) fossil_fatal("out of memory");
return p;
}
void fossil_secure_zero(void *p, size_t n){
volatile unsigned char *vp = (volatile unsigned char *)p;
size_t i;
if( p==0 ) return;
assert( n>0 );
if( n==0 ) return;
for(i=0; i<n; i++){ vp[i] ^= 0xFF; }
for(i=0; i<n; i++){ vp[i] ^= vp[i]; }
}
void fossil_get_page_size(size_t *piPageSize){
#if defined(_WIN32)
SYSTEM_INFO sysInfo;
memset(&sysInfo, 0, sizeof(SYSTEM_INFO));
GetSystemInfo(&sysInfo);
*piPageSize = (size_t)sysInfo.dwPageSize;
#elif defined(USE_MMAN_H)
*piPageSize = (size_t)sysconf(_SC_PAGE_SIZE);
#else
*piPageSize = 4096; /* FIXME: What for POSIX? */
#endif
}
void *fossil_secure_alloc_page(size_t *pN){
void *p;
size_t pageSize = 0;
fossil_get_page_size(&pageSize);
assert( pageSize>0 );
assert( pageSize%2==0 );
#if defined(_WIN32)
p = VirtualAlloc(NULL, pageSize, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
if( p==NULL ){
fossil_fatal("VirtualAlloc failed: %lu\n", GetLastError());
}
if( !VirtualLock(p, pageSize) ){
fossil_fatal("VirtualLock failed: %lu\n", GetLastError());
}
#elif defined(USE_MMAN_H)
p = mmap(0, pageSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if( p==MAP_FAILED ){
fossil_fatal("mmap failed: %d\n", errno);
}
if( mlock(p, pageSize) ){
fossil_fatal("mlock failed: %d\n", errno);
}
#else
p = fossil_malloc(pageSize);
#endif
fossil_secure_zero(p, pageSize);
if( pN ) *pN = pageSize;
return p;
}
void fossil_secure_free_page(void *p, size_t n){
if( !p ) return;
assert( n>0 );
fossil_secure_zero(p, n);
#if defined(_WIN32)
if( !VirtualUnlock(p, n) ){
fossil_panic("VirtualUnlock failed: %lu\n", GetLastError());
}
if( !VirtualFree(p, 0, MEM_RELEASE) ){
fossil_panic("VirtualFree failed: %lu\n", GetLastError());
}
#elif defined(USE_MMAN_H)
if( munlock(p, n) ){
fossil_panic("munlock failed: %d\n", errno);
}
if( munmap(p, n) ){
fossil_panic("munmap failed: %d\n", errno);
}
#else
fossil_free(p);
#endif
}
/*
** Translate every upper-case character in the input string into
** its equivalent lower-case.
*/
char *fossil_strtolwr(char *zIn){
char *zStart = zIn;
if( zIn ){
while( *zIn ){
*zIn = fossil_tolower(*zIn);
zIn++;
}
}
return zStart;
}
/*
** This local variable determines the behavior of
** fossil_assert_safe_command_string():
**
** 0 (default) fossil_panic() on an unsafe command string
**
** 1 Print an error but continue process. Used for
** testing of fossil_assert_safe_command_string().
**
** 2 No-op. Used to allow any arbitrary command string
** through fossil_system(), such as when invoking
** COMMAND in "fossil bisect run COMMAND".
*/
static int safeCmdStrTest = 0;
/*
** Check the input string to ensure that it is safe to pass into system().
** A string is unsafe for system() on unix if it contains any of the following:
**
** * Any occurrance of '$' or '`' except single-quoted or after \
** * Any of the following characters, unquoted: ;|& or \n except
** these characters are allowed as the very last character in the
** string.
** * Unbalanced single or double quotes
**
** This routine is intended as a second line of defense against attack.
** It should never fail. Dangerous shell strings should be detected and
** fixed before calling fossil_system(). This routine serves only as a
** safety net in case of bugs elsewhere in the system.
**
** If an unsafe string is seen, either abort (default) or print
** a warning message (if safeCmdStrTest is true).
*/
static void fossil_assert_safe_command_string(const char *z){
int unsafe = 0;
#ifndef _WIN32
/* Unix */
int inQuote = 0;
int i, c;
for(i=0; !unsafe && (c = z[i])!=0; i++){
switch( c ){
case '$':
case '`': {
if( inQuote!='\'' ) unsafe = i+1;
break;
}
case ';':
case '|':
case '&':
case '\n': {
if( inQuote!='\'' && z[i+1]!=0 ) unsafe = i+1;
break;
}
case '"':
case '\'': {
if( inQuote==0 ){
inQuote = c;
}else if( inQuote==c ){
inQuote = 0;
}
break;
}
case '\\': {
if( z[i+1]==0 ){
unsafe = i+1;
}else if( inQuote!='\'' ){
i++;
}
break;
}
}
}
if( inQuote ) unsafe = i;
#else
/* Windows */
int i, c;
int inQuote = 0;
for(i=0; !unsafe && (c = z[i])!=0; i++){
switch( c ){
case '>':
case '<':
case '|':
case '&':
case '\n': {
if( inQuote==0 && z[i+1]!=0 ) unsafe = i+1;
break;
}
case '"': {
if( inQuote==c ){
inQuote = 0;
}else{
inQuote = c;
}
break;
}
case '^': {
if( !inQuote && z[i+1]!=0 ){
i++;
}
break;
}
}
}
if( inQuote ) unsafe = i;
#endif
if( unsafe && safeCmdStrTest<2 ){
char *zMsg = mprintf("Unsafe command string: %s\n%*shere ----^",
z, unsafe+13, "");
if( safeCmdStrTest ){
fossil_print("%z\n", zMsg);
fossil_free(zMsg);
}else{
fossil_panic("%s", zMsg);
}
}
}
/*
** This function implements a cross-platform "system()" interface.
*/
int fossil_system(const char *zOrigCmd){
int rc;
#if defined(_WIN32)
/* On windows, we have to put double-quotes around the entire command.
** Who knows why - this is just the way windows works.
*/
char *zNewCmd = mprintf("\"%s\"", zOrigCmd);
wchar_t *zUnicode = fossil_utf8_to_unicode(zNewCmd);
if( g.fSystemTrace ) {
fossil_trace("SYSTEM: %s\n", zNewCmd);
}
fossil_assert_safe_command_string(zOrigCmd);
rc = _wsystem(zUnicode);
fossil_unicode_free(zUnicode);
free(zNewCmd);
#else
/* On unix, evaluate the command directly.
*/
if( g.fSystemTrace ) fprintf(stderr, "SYSTEM: %s\n", zOrigCmd);
fossil_assert_safe_command_string(zOrigCmd);
/* Unix systems should never shell-out while processing an HTTP request,
** either via CGI, SCGI, or direct HTTP. The following assert verifies
** this. And the following assert proves that Fossil is not vulnerable
** to the ShellShock or BashDoor bug.
*/
assert( g.cgiOutput==0 );
/* The regular system() call works to get a shell on unix */
fossil_limit_memory(0);
rc = system(zOrigCmd);
fossil_limit_memory(1);
#endif
return rc;
}
/*
** Like "fossil_system()" but does not check the command-string for
** potential security problems.
*/
int fossil_unsafe_system(const char *zOrigCmd){
int rc;
safeCmdStrTest = 2;
rc = fossil_system(zOrigCmd);
safeCmdStrTest = 0;
return rc;
}
/*
** COMMAND: test-fossil-system
**
** Read lines of input and send them to fossil_system() for evaluation.
** Use this command to verify that fossil_system() will not run "unsafe"
** commands.
*/
void test_fossil_system_cmd(void){
char zLine[10000];
safeCmdStrTest = 1;
while(1){
size_t n;
int rc;
printf("system-test> ");
fflush(stdout);
if( !fgets(zLine, sizeof(zLine), stdin) ) break;
n = strlen(zLine);
while( n>0 && fossil_isspace(zLine[n-1]) ) n--;
zLine[n] = 0;
printf("cmd: [%s]\n", zLine);
fflush(stdout);
rc = fossil_system(zLine);
printf("result: %d\n", rc);
}
}
/*
** Like strcmp() except that it accepts NULL pointers. NULL sorts before
** all non-NULL string pointers. Also, this strcmp() is a binary comparison
** that does not consider locale.
*/
int fossil_strcmp(const char *zA, const char *zB){
if( zA==0 ){
if( zB==0 ) return 0;
return -1;
}else if( zB==0 ){
return +1;
}else{
return strcmp(zA,zB);
}
}
int fossil_strncmp(const char *zA, const char *zB, int nByte){
if( zA==0 ){
if( zB==0 ) return 0;
return -1;
}else if( zB==0 ){
return +1;
}else if( nByte>0 ){
int a, b;
do{
a = *zA++;
b = *zB++;
}while( a==b && a!=0 && (--nByte)>0 );
return ((unsigned char)a) - (unsigned char)b;
}else{
return 0;
}
}
/*
** Case insensitive string comparison.
*/
int fossil_strnicmp(const char *zA, const char *zB, int nByte){
if( zA==0 ){
if( zB==0 ) return 0;
return -1;
}else if( zB==0 ){
return +1;
}
if( nByte<0 ) nByte = strlen(zB);
return sqlite3_strnicmp(zA, zB, nByte);
}
int fossil_stricmp(const char *zA, const char *zB){
int nByte;
int rc;
if( zA==0 ){
if( zB==0 ) return 0;
return -1;
}else if( zB==0 ){
return +1;
}
nByte = strlen(zB);
rc = sqlite3_strnicmp(zA, zB, nByte);
if( rc==0 && zA[nByte] ) rc = 1;
return rc;
}
/*
** Get user and kernel times in microseconds.
*/
void fossil_cpu_times(sqlite3_uint64 *piUser, sqlite3_uint64 *piKernel){
#ifdef _WIN32
FILETIME not_used;
FILETIME kernel_time;
FILETIME user_time;
GetProcessTimes(GetCurrentProcess(), ¬_used, ¬_used,
&kernel_time, &user_time);
if( piUser ){
*piUser = ((((sqlite3_uint64)user_time.dwHighDateTime)<<32) +
(sqlite3_uint64)user_time.dwLowDateTime + 5)/10;
}
if( piKernel ){
*piKernel = ((((sqlite3_uint64)kernel_time.dwHighDateTime)<<32) +
(sqlite3_uint64)kernel_time.dwLowDateTime + 5)/10;
}
#else
struct rusage s;
getrusage(RUSAGE_SELF, &s);
if( piUser ){
*piUser = ((sqlite3_uint64)s.ru_utime.tv_sec)*1000000 + s.ru_utime.tv_usec;
}
if( piKernel ){
*piKernel =
((sqlite3_uint64)s.ru_stime.tv_sec)*1000000 + s.ru_stime.tv_usec;
}
#endif
}
/*
** Return the resident set size for this process
*/
sqlite3_uint64 fossil_rss(void){
#ifdef _WIN32
return 0;
#else
struct rusage s;
getrusage(RUSAGE_SELF, &s);
return s.ru_maxrss*1024;
#endif
}
/*
** Internal helper type for fossil_timer_xxx().
*/
enum FossilTimerEnum {
FOSSIL_TIMER_COUNT = 10 /* Number of timers we can track. */
};
static struct FossilTimer {
sqlite3_uint64 u; /* "User" CPU times */
sqlite3_uint64 s; /* "System" CPU times */
int id; /* positive if allocated, else 0. */
} fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}};
/*
** Stores the current CPU times into the shared timer list
** and returns that timer's internal ID. Pass that ID to
** fossil_timer_fetch() to get the elapsed time for that
** timer.
**
** The system has a fixed number of timers, and they can be
** "deallocated" by passing this function's return value to
** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of
** available timers.
**
** Returns 0 on error (no more timers available), with 1+ being valid
** timer IDs.
*/
int fossil_timer_start(){
int i;
for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){
struct FossilTimer * ft = &fossilTimerList[i];
if(ft->id) continue;
ft->id = i+1;
fossil_cpu_times( &ft->u, &ft->s );
break;
}
return (i<FOSSIL_TIMER_COUNT) ? i+1 : 0;
}
/*
** Returns the difference in CPU times in microseconds since
** fossil_timer_start() was called and returned the given timer ID (or
** since it was last reset). Returns 0 if timerId is out of range.
*/
sqlite3_uint64 fossil_timer_fetch(int timerId){
if( timerId>0 && timerId<=FOSSIL_TIMER_COUNT ){
struct FossilTimer * start = &fossilTimerList[timerId-1];
if( !start->id ){
fossil_panic("Invalid call to fetch a non-allocated "
"timer (#%d)", timerId);
/*NOTREACHED*/
}else{
sqlite3_uint64 eu = 0, es = 0;
fossil_cpu_times( &eu, &es );
return (eu - start->u) + (es - start->s);
}
}
return 0;
}
/*
** Resets the timer associated with the given ID, as obtained via
** fossil_timer_start(), to the current CPU time values.
*/
sqlite3_uint64 fossil_timer_reset(int timerId){
if( timerId>0 && timerId<=FOSSIL_TIMER_COUNT ){
struct FossilTimer * start = &fossilTimerList[timerId-1];
if( !start->id ){
fossil_panic("Invalid call to reset a non-allocated "
"timer (#%d)", timerId);
/*NOTREACHED*/
}else{
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
fossil_cpu_times( &start->u, &start->s );
return rc;
}
}
return 0;
}
/**
"Deallocates" the fossil timer identified by the given timer ID.
returns the difference (in uSec) between the last time that timer
was started or reset. Returns 0 if timerId is out of range (but
note that, due to system-level precision restrictions, this
function might return 0 on success, too!). It is not legal to
re-use the passed-in timerId after calling this until/unless it is
re-initialized using fossil_timer_start() (NOT
fossil_timer_reset()).
*/
sqlite3_uint64 fossil_timer_stop(int timerId){
if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
return 0;
}else{
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
struct FossilTimer * t = &fossilTimerList[timerId-1];
t->id = 0;
t->u = t->s = 0U;
return rc;
}
}
/*
** Returns true (non-0) if the given timer ID (as returned from
** fossil_timer_start() is currently active.
*/
int fossil_timer_is_active( int timerId ){
if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
return 0;
}else{
const int rc = fossilTimerList[timerId-1].id;
assert(!rc || (rc == timerId));
return fossilTimerList[timerId-1].id;
}
}
/*
** Return TRUE if fd is a valid open file descriptor. This only
** works on unix. The function always returns true on Windows.
*/
int is_valid_fd(int fd){
#ifdef _WIN32
return 1;
#else
return fcntl(fd, F_GETFL)!=(-1) || errno!=EBADF;
#endif
}
/*
** Returns TRUE if zSym is exactly HNAME_LEN_SHA1 or HNAME_LEN_K256
** bytes long and contains only lower-case ASCII hexadecimal values.
*/
int fossil_is_artifact_hash(const char *zSym){
int sz = zSym ? (int)strlen(zSym) : 0;
return (HNAME_LEN_SHA1==sz || HNAME_LEN_K256==sz) && validate16(zSym, sz);
}
/*
** Return true if the input string is NULL or all whitespace.
** Return false if the input string contains text.
*/
int fossil_all_whitespace(const char *z){
if( z==0 ) return 1;
while( fossil_isspace(z[0]) ){ z++; }
return z[0]==0;
}
/*
** Return the name of the users preferred text editor. Return NULL if
** not found.
**
** Search algorithm:
** (1) The local "editor" setting
** (2) The global "editor" setting
** (3) The VISUAL environment variable
** (4) The EDITOR environment variable
** (5) (Windows only:) "notepad.exe"
*/
const char *fossil_text_editor(void){
const char *zEditor = db_get("editor", 0);
if( zEditor==0 ){
zEditor = fossil_getenv("VISUAL");
}
if( zEditor==0 ){
zEditor = fossil_getenv("EDITOR");
}
#if defined(_WIN32) || defined(__CYGWIN__)
if( zEditor==0 ){
zEditor = mprintf("%s\\notepad.exe", fossil_getenv("SYSTEMROOT"));
#if defined(__CYGWIN__)
zEditor = fossil_utf8_to_path(zEditor, 0);
#endif
}
#endif
return zEditor;
}
/*
** Construct a temporary filename.
**
** The returned string is obtained from sqlite3_malloc() and must be
** freed by the caller.
**
** See also: file_tempname() and file_time_timename();
*/
char *fossil_temp_filename(void){
char *zTFile = 0;
const char *zDir;
char cDirSep;
char zSep[2];
size_t nDir;
u64 r[2];
#ifdef _WIN32
char *zTempDirA = NULL;
WCHAR zTempDirW[MAX_PATH+1];
const DWORD dwTempSizeW = sizeof(zTempDirW)/sizeof(zTempDirW[0]);
DWORD dwTempLenW;
#else
int i;
static const char *azTmp[] = {"/var/tmp","/usr/tmp","/tmp"};
#endif
if( g.db ){
sqlite3_file_control(g.db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile);
if( zTFile ) return zTFile;
}
sqlite3_randomness(sizeof(r), &r);
#if _WIN32
cDirSep = '\\';
dwTempLenW = GetTempPathW(dwTempSizeW, zTempDirW);
if( dwTempLenW>0 && dwTempLenW<dwTempSizeW
&& ( zTempDirA = fossil_path_to_utf8(zTempDirW) )){
zDir = zTempDirA;
}else{
zDir = fossil_getenv("LOCALAPPDATA");
if( zDir==0 ) zDir = ".";
}
#else
for(i=0; i<(int)(sizeof(azTmp)/sizeof(azTmp[0])); i++){
struct stat buf;
zDir = azTmp[i];
if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){
break;
}
}
if( i>=(int)(sizeof(azTmp)/sizeof(azTmp[0])) ) zDir = ".";
cDirSep = '/';
#endif
nDir = strlen(zDir);
zSep[1] = 0;
zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
zTFile = sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
#ifdef _WIN32
if( zTempDirA ) fossil_path_free(zTempDirA);
#endif
return zTFile;
}
/*
** Turn memory limits for stack and heap on and off. The argument
** is true to turn memory limits on and false to turn them off.
**
** Memory limits should be enabled at startup, but then turned off
** before starting subprocesses.
*/
void fossil_limit_memory(int onOff){
#if defined(__unix__)
static sqlite3_int64 origHeap = 10000000000LL; /* 10GB */
static sqlite3_int64 origStack = 8000000 ; /* 8MB */
struct rlimit x;
#if defined(RLIMIT_DATA)
getrlimit(RLIMIT_DATA, &x);
if( onOff ){
origHeap = x.rlim_cur;
if( sizeof(void*)<8 || sizeof(x.rlim_cur)<8 ){
x.rlim_cur = 1000000000 ; /* 1GB on 32-bit systems */
}else{
x.rlim_cur = 10000000000LL; /* 10GB on 64-bit systems */
}
}else{
x.rlim_cur = origHeap;
}
setrlimit(RLIMIT_DATA, &x);
#endif /* defined(RLIMIT_DATA) */
#if defined(RLIMIT_STACK)
getrlimit(RLIMIT_STACK, &x);
if( onOff ){
origStack = x.rlim_cur;
x.rlim_cur = 8000000; /* 8MB */
}else{
x.rlim_cur = origStack;
}
setrlimit(RLIMIT_STACK, &x);
#endif /* defined(RLIMIT_STACK) */
#endif /* defined(__unix__) */
}
#if defined(HAVE_PLEDGE)
/*
** Interface to pledge() on OpenBSD 5.9 and later.
**
** On platforms that have pledge(), use this routine.
** On all other platforms, this routine does not exist, but instead
** a macro defined in config.h is used to provide a no-op.
*/
void fossil_pledge(const char *promises){
if( pledge(promises, 0) ){
fossil_panic("pledge(\"%s\",NULL) fails with errno=%d",
promises, (int)errno);
}
}
#endif /* defined(HAVE_PLEDGE) */
/*
** Construct a random password and return it as a string. N is the
** recommended number of characters for the password.
**
** Space to hold the returned string is obtained from fossil_malloc()
** and should be freed by the caller.
*/
char *fossil_random_password(int N){
char zSrc[60];
int nSrc;
int i;
char z[60];
/* Source characters for the password. Omit characters like "0", "O",
** "1" and "I" that might be easily confused */
static const char zAlphabet[] =
/* 0 1 2 3 4 5 */
/* 123456789 123456789 123456789 123456789 123456789 123456 */
"23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
if( N<8 ) N = 8;
nSrc = sizeof(zAlphabet) - 1;
if( N>nSrc ) N = nSrc;
memcpy(zSrc, zAlphabet, nSrc);
for(i=0; i<N; i++){
unsigned r;
sqlite3_randomness(sizeof(r), &r);
r %= nSrc;
z[i] = zSrc[r];
zSrc[r] = zSrc[--nSrc];
}
z[i] = 0;
return fossil_strdup(z);
}
/*
** COMMAND: test-random-password
**
** Usage: %fossil test-random-password [N] [--entropy]
**
** Generate a random password string of approximately N characters in length.
** If N is omitted, use 12. Values of N less than 8 are changed to 8
** and greater than 57 and changed to 57.
**
** If the --entropy flag is included, the number of bits of entropy in
** the password is show as well.
*/
void test_random_password(void){
int N = 12;
int showEntropy = 0;
int i;
char *zPassword;
for(i=2; i<g.argc; i++){
const char *z = g.argv[i];
if( z[0]=='-' && z[1]=='-' ) z++;
if( strcmp(z,"-entropy")==0 ){
showEntropy = 1;
}else if( fossil_isdigit(z[0]) ){
N = atoi(z);
if( N<8 ) N = 8;
if( N>57 ) N = 57;
}else{
usage("[N] [--entropy]");
}
}
zPassword = fossil_random_password(N);
if( showEntropy ){
double et = 57.0;
for(i=1; i<N; i++) et *= 57-i;
fossil_print("%s (%d bits of entropy)\n", zPassword,
(int)(log(et)/log(2.0)));
}else{
fossil_print("%s\n", zPassword);
}
fossil_free(zPassword);
}
/*
** Return the number of decimal digits in a nonnegative integer. This is useful
** when formatting text.
*/
int fossil_num_digits(int n){
return n< 10 ? 1 : n< 100 ? 2 : n< 1000 ? 3
: n< 10000 ? 4 : n< 100000 ? 5 : n< 1000000 ? 6
: n<10000000 ? 7 : n<100000000 ? 8 : n<1000000000 ? 9 : 10;
}
#if !defined(_WIN32)
#if !defined(__DARWIN__) && !defined(__APPLE__) && !defined(__HAIKU__)
/*
** Search for an executable on the PATH environment variable.
** Return true (1) if found and false (0) if not found.
*/
static int binaryOnPath(const char *zBinary){
const char *zPath = fossil_getenv("PATH");
char *zFull;
int i;
int bExists;
while( zPath && zPath[0] ){
while( zPath[0]==':' ) zPath++;
for(i=0; zPath[i] && zPath[i]!=':'; i++){}
zFull = mprintf("%.*s/%s", i, zPath, zBinary);
bExists = file_access(zFull, X_OK);
fossil_free(zFull);
if( bExists==0 ) return 1;
zPath += i;
}
return 0;
}
#endif
#endif
/*
** Return the name of a command that will launch a web-browser.
*/
const char *fossil_web_browser(void){
const char *zBrowser = 0;
#if defined(_WIN32)
zBrowser = db_get("web-browser", "start \"\"");
#elif defined(__DARWIN__) || defined(__APPLE__) || defined(__HAIKU__)
zBrowser = db_get("web-browser", "open");
#else
zBrowser = db_get("web-browser", 0);
if( zBrowser==0 ){
static const char *const azBrowserProg[] =
{ "xdg-open", "gnome-open", "firefox", "google-chrome" };
int i;
zBrowser = "echo";
for(i=0; i<count(azBrowserProg); i++){
if( binaryOnPath(azBrowserProg[i]) ){
zBrowser = azBrowserProg[i];
break;
}
}
}
#endif
return zBrowser;
}
/*
** On non-Windows systems, calls nice(2) with the given level. Errors
** are ignored. On Windows this is a no-op.
*/
void fossil_nice(int level){
#ifndef _WIN32
/* dummy if() condition to avoid nuisance warning about unused result on
certain compiler */
if( nice(level) ){ /*ignored*/ }
#else
(void)level;
#endif
}
/*
** Calls fossil_nice() with a default level.
*/
void fossil_nice_default(void){
fossil_nice(19);
}
|