-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdtunix.c
More file actions
1985 lines (1768 loc) · 56.8 KB
/
dtunix.c
File metadata and controls
1985 lines (1768 loc) · 56.8 KB
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
/****************************************************************************
* *
* COPYRIGHT (c) 2006 - 2026 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtunix.c
* Author: Robin T. Miller
*
* Description:
* This module contains *unix OS specific functions.
*
* Modification History:
*
* March 31st, 2021 by Robin T. Miller
* For Solaris mount lookups, save the FS type and mount options.
*
* March 30th, 2021 by Robin T. Miller
* In os_get_fs_information() switch to fundamental block size, required
* for accurate free space on Solaris. Revert to block size as required.
*
* March 10th, 2021 by Robin T. Miller
* For Solaris, clear O_DIRECT flag in os_open_file(), omitted previously!
*
* July 17th, 2020 by Robin T. Miller
* Add a hack workaround for Linux disk names in ConvertDeviceToScsiDevice().
*
* June 3rd, 2020 by Robin T. Miller
* Update os_report_file_map() to use NO_OFFSET value to report all
* file extents, rather than failing to report extents of sparse files.
* Note: We would not report any extents if the offset specified was sparse.
*
* December 24th, 2019 by Robin T. Miller
* Added Linux support to map file offsets to physical LBA's.
*
* March 1st, 2018 by Robin T Miller
* Add isDeviceMounted() for Linux only (right now), to determine if
* a /dev disk name is mounted to prevent overwriting mounted file systems.
*
* May 29th, 2015 by Robin T. Miller
* Increase the mount file system options buffer, since the previous
* of 128 bytes was too small for recent RHEL6 mounts with options >194!
* Also, changed strcpy() to strncpy() to avoid overwriting the stack!
*
* April 29th, 2015 by Robin T. Miller
* For HP-UX, Solaris, and Linux, increase the mount path buffer sizes
* from 128 bytes to 8k, since somone has decided to use rather long mounts!
*
* Date Unknown! (Robin forgot to add one)
* Updated AIX mount function to lookup file system type.
*/
#include "dt.h"
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
/*
* SHIT! Another area of incompatability between *nix OS's! :-(
*/
#if defined(__linux__)
# include <mntent.h>
# define MOUNT_FILE _PATH_MOUNTED
#elif defined(__hpux)
# include <mntent.h>
# define MOUNT_FILE MNT_MNTTAB
#elif defined(SOLARIS)
# include <sys/mnttab.h>
# define MOUNT_FILE MNTTAB
#endif /* defined(__linux__) */
#if defined(DEV_BDIR_LEN)
/*
* ConvertBlockToRawDevice() Convert a Block Device to a Raw Device.
*
* Inputs:
* block_device = Pointer to the block device.
*
* Return Value:
* Returns the raw device or NULL of it's not a block device.
*/
char *
ConvertBlockToRawDevice(char *block_device)
{
char *raw_device = NULL;
char *p = (block_device + DEV_BDIR_LEN);
if (strncmp(block_device, DEV_BDIR_PREFIX, DEV_BDIR_LEN) == 0) {
/* Note: Assumes raw device path is longer than block device! */
raw_device = Malloc(NULL, strlen(block_device) + (DEV_RDIR_LEN - DEV_BDIR_LEN));
if (raw_device) {
(void)sprintf(raw_device, "%s%s", DEV_RDIR_PREFIX, p);
}
}
return (raw_device);
}
#endif /* defined(DEV_BDIR_LEN) */
#if defined(__linux__)
#include <ctype.h>
/*
* ConvertDeviceToScsiDevice() Convert a Device to a SCSI Device.
*
* Note: This is being specific added for RHEL6 since SCSI IOCTL fails
* if the device name has trailing numbers for the partition. Error returned:
*
* SCSI request (SG_IO) failed on /dev/sdb1!, errno = 25 - Inappropriate ioctl for device
*
* Inputs:
* device = Pointer to the device.
*
* Return Value:
* Returns a pointer to new memory with SCSI device name.
*/
char *
ConvertDeviceToScsiDevice(char *device)
{
char *scsi_device, *p;
if (device == NULL) return(device);
scsi_device = strdup(device);
#if defined(__linux__)
/* Hack: The logic below is broken for: "/dev/mapper/20ef62e25253404fa6c9ce900ec009227" */
/* Therefore, if the wrong (mangled) disk name is returned, SCSI open will fail! */
if ( NEL(device, DISK_NAME, strlen(DISK_NAME)) ) {
return(scsi_device); /* Return original device name unless "/dev/sd"! */
}
#endif /* defined(__linux__) */
p = ( scsi_device + (strlen(scsi_device) - 1) );
/* TODO: Figure out a better way to strip partitions to get raw disk name! */
/* Note: Some OS's add a partition number, like /dev/sda1, others a letter. */
while ( isdigit(*p) ) {
*p = '\0';
--p;
}
return(scsi_device);
}
#else /* !defined(__linux__) */
char *
ConvertDeviceToScsiDevice(char *device)
{
return ( strdup(device) );
}
#endif /* defined(__linux__) */
/* ----------------------------------------------------------------------------------- */
#if defined(AIX)
#include <sys/mntctl.h>
#include <sys/vmount.h>
#define MNT_BUFFER_SIZE (32 * KBYTE_SIZE)
char *lookup_gfstype(int gfstype);
typedef struct aix_filesystem_types {
int gfstype;
char *gfsname;
} aix_filesystem_types_t;
aix_filesystem_types_t aix_gfstypes[] = {
{ MNT_J2, "jfs2" }, /* 0 - AIX physical fs "jfs2" */
{ MNT_NAMEFS, "namefs" }, /* 1 - AIX pseudo fs "namefs" */
{ MNT_NFS, "nfs" }, /* 2 - SUN Network File System "nfs" */
{ MNT_JFS, "jfs" }, /* 3 - AIX R3 physical fs "jfs" */
{ MNT_CDROM, "cdrom" }, /* 5 - CDROM File System "cdrom" */
{ MNT_PROCFS, "proc" }, /* 6 - PROCFS File System "proc" */
{ MNT_SFS, "sfs" }, /* 16 - AIX Special FS (STREAM mounts) */
{ MNT_CACHEFS, "cachefs" }, /* 17 - Cachefs file system */
{ MNT_NFS3, "nfs3" }, /* 18 - NFSv3 file system */
{ MNT_AUTOFS, "autofs" }, /* 19 - Automount file system */
{ MNT_VXFS, "vxfs" }, /* 32 - THRPGIO File System "vxfs" */
{ MNT_VXODM, "vxodm" }, /* 33 - For Veritas File System */
{ MNT_UDF, "udf" }, /* 34 - UDFS file system */
{ MNT_NFS4, "nfs4" }, /* 35 - NFSv4 file system */
{ MNT_RFS4, "rfs4" }, /* 36 - NFSv4 Pseudo file system */
{ MNT_CIFS, "cifs" } /* 37 - AIX SMBFS (CIFS client) */
};
int num_aix_gfstypes = sizeof(aix_gfstypes) / sizeof(aix_filesystem_types_t);
char *
lookup_gfstype(int gfstype)
{
int entry;
for (entry = 0; entry < num_aix_gfstypes; entry++) {
if (aix_gfstypes[entry].gfstype == gfstype) {
return(aix_gfstypes[entry].gfsname);
}
}
return(NULL);
}
hbool_t
FindMountDevice(dinfo_t *dip, char *path, hbool_t debug)
{
char path_dir[PATH_BUFFER_SIZE];
char *mounted_match = NULL;
char *mounted_path = NULL;
char *filesystem_type;
struct vmount *vmp;
char *mounted, *mounted_over, *vbp;
hbool_t match = False;
char *path_dirp = path;
int entries, size;
void *buffer;
if (path[0] != dip->di_dir_sep) {
memset(path_dir, '\0', sizeof(path_dir));
path_dirp = getcwd(path_dir, sizeof(path_dir));
if (path_dirp == NULL) return(match);
}
buffer = Malloc(dip, MNT_BUFFER_SIZE);
if (buffer == NULL) return(match);
if ( (entries = mntctl(MCTL_QUERY, MNT_BUFFER_SIZE, buffer)) < 0) {
Perror(dip, "mntctl() failed");
Free(dip, buffer);
return(match);
}
vbp = buffer;
while (entries--) {
vmp = (struct vmount *)vbp;
mounted = vmt2dataptr(vmp, VMT_OBJECT);
mounted_over = vmt2dataptr(vmp, VMT_STUB);
if (debug) {
Printf(dip, "mounted = %s, mounted_over = %s, type = %d\n", mounted, mounted_over, vmp->vmt_gfstype);
}
/*
* Loop through all mounted path entries to find the right match!
* Multiple matches via: /, /var, /var/tmp, /mnt, /mnt/nfs, etc
* So we want the match with the longest string in our path!
*/
if (strncmp(mounted_over, path_dirp, strlen(mounted_over)) == 0) {
/* Replace with entries matching more of the path. */
if ( (mounted_path == NULL) ||
(strlen(mounted_over) > strlen(mounted_path)) ) {
if (debug) {
Printf(dip, "Found match! -> %s on %s\n", mounted, mounted_over);
}
/* In memory buffer, so just save the pointed. */
mounted_path = mounted_over;
mounted_match = mounted;
filesystem_type = lookup_gfstype(vmp->vmt_gfstype);
match = True;
}
}
vbp += vmp->vmt_length;
}
if (match == True) {
dip->di_mounted_from_device = strdup(mounted_match);
dip->di_mounted_on_dir = strdup(mounted_path);
if (filesystem_type) {
dip->di_filesystem_type = strdup(filesystem_type);
}
}
Free(dip, buffer);
return(match);
}
hbool_t
isDeviceMounted(dinfo_t *dip, char *path, hbool_t debug)
{
if (debug) {
Printf(dip, "isDeviceMounted: This needs implmented for this OS!\n");
}
return(False);
}
/* ----------------------------------------------------------------------------------- */
#elif defined(SOLARIS)
hbool_t
FindMountDevice(dinfo_t *dip, char *path, hbool_t debug)
{
char mounted_path[PATH_BUFFER_SIZE];
char mounted_match[PATH_BUFFER_SIZE];
char filesystem_type[SMALL_BUFFER_SIZE];
char filesystem_options[PATH_BUFFER_SIZE];
char path_dir[PATH_BUFFER_SIZE];
struct mnttab mnttab;
struct mnttab *mnt = &mnttab;
hbool_t match = False;
char *path_dirp = path;
FILE *fp;
if (path[0] != dip->di_dir_sep) {
memset(path_dir, '\0', sizeof(path_dir));
path_dirp = getcwd(path_dir, sizeof(path_dir));
if (path_dirp == NULL) return(match);
}
fp = fopen(MOUNT_FILE, "r");
if (fp == NULL) return (match);
memset(mounted_path, '\0', sizeof(mounted_path));
memset(mounted_match, '\0', sizeof(mounted_match));
while ( getmntent(fp, mnt) == 0 ) {
if (debug) {
Printf(dip, "mount point = %s, special = %s, type = %s\n",
mnt->mnt_mountp, mnt->mnt_special, mnt->mnt_fstype);
}
/*
* Loop through all mounted path entries to find the right match!
* Multiple matches via: /, /var, /var/tmp, /mnt, /mnt/nfs, etc
* So we want the match with the longest string in our path!
*/
if (strncmp(mnt->mnt_mountp, path_dirp, strlen(mnt->mnt_mountp)) == 0) {
/* Replace with entries matching more of the path. */
if (strlen(mnt->mnt_mountp) > strlen(mounted_path)) {
if (debug) {
Printf(dip, "Found match! -> %s on %s\n", mnt->mnt_special, mnt->mnt_mountp);
}
strncpy(mounted_path, mnt->mnt_mountp, sizeof(mounted_path)-1);
strncpy(mounted_match, mnt->mnt_special, sizeof(mounted_match)-1);
strncpy(filesystem_type, mnt->mnt_fstype, sizeof(filesystem_type)-1);
strncpy(filesystem_options, mnt->mnt_mntopts, sizeof(filesystem_options)-1);
match = True;
}
}
}
if (match == True) {
dip->di_mounted_from_device = strdup(mounted_match);
dip->di_mounted_on_dir = strdup(mounted_path);
dip->di_filesystem_type = strdup(filesystem_type);
dip->di_filesystem_options = strdup(filesystem_options);
}
(void)fclose(fp);
return(match);
}
hbool_t
isDeviceMounted(dinfo_t *dip, char *path, hbool_t debug)
{
if (debug) {
Printf(dip, "isDeviceMounted: This needs implmented for this OS!\n");
}
return(False);
}
/* ----------------------------------------------------------------------------------- */
#elif defined(MOUNT_FILE) /* Linux, HP-UX */
hbool_t
FindMountDevice(dinfo_t *dip, char *path, hbool_t debug)
{
char mounted_path[PATH_BUFFER_SIZE];
char mounted_match[PATH_BUFFER_SIZE];
char filesystem_type[SMALL_BUFFER_SIZE];
char filesystem_options[PATH_BUFFER_SIZE];
char path_dir[PATH_BUFFER_SIZE];
struct mntent *mnt;
hbool_t match = False;
char *path_dirp = path;
FILE *fp;
if (path[0] != dip->di_dir_sep) {
memset(path_dir, '\0', sizeof(path_dir));
path_dirp = getcwd(path_dir, sizeof(path_dir));
if (path_dirp == NULL) return(match);
}
fp = setmntent(MOUNT_FILE, "r");
if (fp == NULL) return (match);
memset(mounted_path, '\0', sizeof(mounted_path));
memset(mounted_match, '\0', sizeof(mounted_match));
while ( (mnt = getmntent(fp)) != NULL ) {
if (debug) {
Printf(dip, "dir = %s, fsname = %s, type = %s\n", mnt->mnt_dir, mnt->mnt_fsname, mnt->mnt_type);
}
/*
* Loop through all mounted path entries to find the right match!
* Multiple matches via: /, /var, /var/tmp, /mnt, /mnt/nfs, etc
* So we want the match with the longest string in our path!
*/
if (strncmp(mnt->mnt_dir, path_dirp, strlen(mnt->mnt_dir)) == 0) {
/* Replace with entries matching more of the path. */
if (strlen(mnt->mnt_dir) > strlen(mounted_path)) {
if (debug) {
Printf(dip, "Found match! -> %s on %s\n", mnt->mnt_fsname, mnt->mnt_dir);
}
strncpy(mounted_path, mnt->mnt_dir, sizeof(mounted_path)-1);
strncpy(mounted_match, mnt->mnt_fsname, sizeof(mounted_match)-1);
strncpy(filesystem_type, mnt->mnt_type, sizeof(filesystem_type)-1);
strncpy(filesystem_options, mnt->mnt_opts, sizeof(filesystem_options)-1);
match = True;
}
}
}
if (match == True) {
dip->di_mounted_from_device = strdup(mounted_match);
dip->di_mounted_on_dir = strdup(mounted_path);
dip->di_filesystem_type = strdup(filesystem_type);
dip->di_filesystem_options = strdup(filesystem_options);
}
(void)endmntent(fp);
return(match);
}
hbool_t
isDeviceMounted(dinfo_t *dip, char *path, hbool_t debug)
{
char mounted_path[PATH_BUFFER_SIZE];
char mounted_match[PATH_BUFFER_SIZE];
char filesystem_type[SMALL_BUFFER_SIZE];
char filesystem_options[PATH_BUFFER_SIZE];
char path_dir[PATH_BUFFER_SIZE];
struct mntent *mnt;
size_t path_len = strlen(path);
hbool_t match = False;
FILE *fp;
fp = setmntent(MOUNT_FILE, "r");
if (fp == NULL) return (match);
memset(mounted_path, '\0', sizeof(mounted_path));
memset(mounted_match, '\0', sizeof(mounted_match));
while ( (mnt = getmntent(fp)) != NULL ) {
if (debug) {
Printf(dip, "dir = %s, fsname = %s, type = %s\n", mnt->mnt_dir, mnt->mnt_fsname, mnt->mnt_type);
}
/*
* Normally users will specific /dev/sda, for example, while file systems
* are mounted from partitions such as /dev/sda1. We also must be careful
* for matching /dev/sda and /dev/sdaa, etc. when we have many disk names.
* DM-MP paths looks like this: /dev/mapper/35000cca2510285c8-part1
*
* Note: We do *not* catch this type of mount today: (sigh)
* /dev/mapper/centos_cos--lab--l4--test01-root -> ../dm-0 -> /dev/sdm
*/
if ( (strncmp(path, mnt->mnt_fsname, path_len) == 0) && !isalpha(mnt->mnt_fsname[path_len]) ) {
if (debug) {
Printf(dip, "Found match! -> %s on %s\n", mnt->mnt_fsname, mnt->mnt_dir);
}
strncpy(mounted_path, mnt->mnt_dir, sizeof(mounted_path)-1);
strncpy(mounted_match, mnt->mnt_fsname, sizeof(mounted_match)-1);
strncpy(filesystem_type, mnt->mnt_type, sizeof(filesystem_type)-1);
strncpy(filesystem_options, mnt->mnt_opts, sizeof(filesystem_options)-1);
match = True;
break;
}
}
if (match == True) {
dip->di_mounted_from_device = strdup(mounted_match);
dip->di_mounted_on_dir = strdup(mounted_path);
dip->di_filesystem_type = strdup(filesystem_type);
dip->di_filesystem_options = strdup(filesystem_options);
}
(void)endmntent(fp);
return(match);
}
#elif defined(FreeBSD) || defined(MacDarwin)
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
hbool_t
FindMountDevice(dinfo_t *dip, char *path, hbool_t debug)
{
char path_dir[PATH_BUFFER_SIZE];
char *mounted_match = NULL;
char *mounted_path = NULL;
char *filesystem_type;
hbool_t match = False;
char *path_dirp = path;
int count, entries;
struct statfs *statfs_array;
struct statfs *sfsp;
long bufsize;
if (path[0] != dip->di_dir_sep) {
memset(path_dir, '\0', sizeof(path_dir));
path_dirp = getcwd(path_dir, sizeof(path_dir));
if (path_dirp == NULL) return(match);
}
/*
* int
* getfsstat(struct statfs *buf, long bufsize, int flags);
*
* DESCRIPTION
* The getfsstat() system call returns information about all mounted file
* systems. The buf argument is a pointer to statfs structures, as
* described in statfs(2).
*
* Fields that are undefined for a particular file system are set to -1.
* The buffer is filled with an array of fsstat structures, one for each
* mounted file system up to the byte count specified by bufsize. Note, the
* bufsize argument is the number of bytes that buf can hold, not the count
* of statfs structures it will hold.
*
* If buf is given as NULL, getfsstat() returns just the number of mounted
* file systems.
*
* Normally flags should be specified as MNT_WAIT. If flags is set to
* MNT_NOWAIT, getfsstat() will return the information it has available
* without requesting an update from each file system. Thus, some of the
* information will be out of date, but getfsstat() will not block waiting
* for information from a file system that is unable to respond.
*
* RETURN VALUES
* Upon successful completion, the number of fsstat structures is returned.
* Otherwise, -1 is returned and the global variable errno is set to indi-
* cate the error.
*/
/* Note: If we use more statfs information, we may need to switch to MNT_WAIT. */
entries = getfsstat(NULL, 0, MNT_NOWAIT);
if (entries == 0) return(False);
if (entries == FAILURE) {
Perror(dip, "getfsstat");
}
bufsize = (sizeof(*sfsp) * entries);
statfs_array = Malloc(dip, bufsize);
if (statfs_array == NULL) return(False);
entries = getfsstat(statfs_array, bufsize, MNT_NOWAIT);
if (entries == FAILURE) {
Perror(dip, "getfsstat");
Free(dip, statfs_array);
return(False);
}
for (count = 0; count < entries; count++) {
sfsp = &statfs_array[count];
if (debug) {
Printf(dip, "mount point = %s, mounted from = %s, type = %s\n",
sfsp->f_mntonname, sfsp->f_mntfromname, sfsp->f_fstypename);
}
/*
* Loop through all mounted path entries to find the right match!
* Multiple matches via: /, /var, /var/tmp, /mnt, /mnt/nfs, etc
* So we want the match with the longest string in our path!
*/
if (strncmp(sfsp->f_mntonname, path_dirp, strlen(sfsp->f_mntonname)) == 0) {
/* Replace with entries matching more of the path. */
if ( (mounted_path == NULL) ||
(strlen(sfsp->f_mntonname) > strlen(mounted_path)) ) {
if (debug) {
Printf(dip, "Found match! -> %s on %s\n", sfsp->f_mntfromname, sfsp->f_mntonname);
}
/* In memory buffer, so just save the pointed. */
mounted_path = sfsp->f_mntonname;
mounted_match = sfsp->f_mntfromname;
filesystem_type = sfsp->f_fstypename;
match = True;
}
}
}
if (match == True) {
dip->di_mounted_from_device = strdup(mounted_match);
dip->di_mounted_on_dir = strdup(mounted_path);
dip->di_filesystem_type = strdup(filesystem_type);
}
Free(dip, statfs_array);
return(match);
}
hbool_t
isDeviceMounted(dinfo_t *dip, char *path, hbool_t debug)
{
if (debug) {
Printf(dip, "isDeviceMounted: This needs implmented for this OS!\n");
}
return(False);
}
#else /* We don't have support for this OS yet! */
hbool_t
FindMountDevice(dinfo_t *dip, char *path, hbool_t debug)
{
if (debug) {
Printf(dip, "FindMountDevice: Don't know how to find mount device yet!\n");
}
return (False);
}
hbool_t
isDeviceMounted(dinfo_t *dip, char *path, hbool_t debug)
{
if (debug) {
Printf(dip, "isDeviceMounted: This needs implmented for this OS!\n");
}
return(False);
}
#endif /* defined(SOLARIS */
/*
* dt_open_file() - Open a file with retries.
*
* Inputs:
* dip = The device information pointer.
* file = File name to check for existance.
* flags = The file open flags.
* perm = The file permissions.
* isDiskFull = Pointer to disk full flag.
* isDirectory = Pointer to directory flag.
* errors = The error control flag.
* retries = The retries control flag.
*
* Return Value:
* Returns the file handle (NoFd on failures).
*/
HANDLE
dt_open_file(dinfo_t *dip, char *file, int flags, int perm,
hbool_t *isDiskFull, hbool_t *isDirectory,
hbool_t errors, hbool_t retrys)
{
HANDLE handle = NoFd;
int rc = SUCCESS;
if (isDiskFull) *isDiskFull = False;
if (isDirectory) *isDirectory = False;
if (dip->di_debug_flag) {
Printf(dip, "Opening file %s with POSIX open flags %#x...\n", file, flags);
if (dip->di_extended_errors == True) {
ReportOpenInformation(dip, file, OS_OPEN_FILE_OP, flags, 0, 0, 0, False);
}
}
if (retrys == True) dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, OPEN_OP);
handle = os_open_file(file, flags, perm);
DISABLE_NOPROG(dip);
if (handle == NoFd) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, file, OS_OPEN_FILE_OP, OPEN_OP, NULL, 0, (Offset_t)0, (size_t)0,
error, logLevelError, PRT_SYSLOG, RPT_NOXERRORS);
if (isDiskFull) {
*isDiskFull = os_isDiskFull(error);
if (*isDiskFull == True) return(handle);
}
if (isDirectory) {
*isDirectory = os_isADirectory(error);
if (*isDirectory == True) return(handle);
}
if (errors == False) eip->ei_rpt_flags |= RPT_NOERRORS;
if (retrys == False) eip->ei_rpt_flags |= RPT_NORETRYS;
rc = ReportRetryableError(dip, eip, "Failed to open file %s", file);
}
} while ( (handle == NoFd) && (rc == RETRYABLE) );
if ( (handle == NoFd) && (errors == True) ) {
if (dip->di_extended_errors == True) {
ReportOpenInformation(dip, file, OS_OPEN_FILE_OP, flags, 0, 0, 0, True);
}
} else if ( (handle != NoFd) && (dip->di_debug_flag == True) ) {
Printf(dip, "File %s successfully opened, fd = %d\n", file, handle);
}
return(handle);
}
#if defined(MacDarwin)
#define DIRECTIO_ON 1
#define DIRECTIO_OFF 0
HANDLE
os_open_file(char *name, int oflags, int perm)
{
HANDLE fd;
hbool_t dio_flag = (oflags & O_DIRECT) ? True : False;
oflags &= ~O_DIRECT; /* Clear the psuedo-flag. */
fd = open(name, oflags, perm);
/* If requested, try to enable Direct I/O (DIO). */
if ( (fd != NoFd) && (dio_flag == True) ) {
int status = fcntl(fd, F_NOCACHE, DIRECTIO_ON);
}
return(fd);
}
#elif defined(SOLARIS)
HANDLE
os_open_file(char *name, int oflags, int perm)
{
HANDLE fd;
hbool_t dio_flag = (oflags & O_DIRECT) ? True : False;
oflags &= ~O_DIRECT; /* Clear the psuedo-flag. */
/* Handle Direct I/O on HFS and Veritas file systems. */
fd = open(name, oflags, perm);
/* If requested, try to enable Direct I/O (DIO). */
if ( (fd != NoFd) && (dio_flag == True) ) {
int status;
oflags &= ~O_DIRECT; /* Clear the psuedo-flag. */
if ( (status = directio(fd, DIRECTIO_ON)) < 0) {
if (errno == ENOTTY) {
(void)ioctl(fd, VX_SETCACHE, VX_DIRECT);
}
}
}
return(fd);
}
#elif defined(__hpux)
HANDLE
os_open_file(char *name, int oflags, int perm)
{
HANDLE fd;
hbool_t dio_flag = (oflags & O_DIRECT) ? True : False;
oflags &= ~O_DIRECT; /* Clear the psuedo-flag. */
fd = open(name, oflags, perm);
/* If requested, try to enable Direct I/O (DIO). */
if ( (fd != NoFd) && (dio_flag == True) ) {
int status;
status = ioctl(fd, VX_SETCACHE, VX_DIRECT);
}
return(fd);
}
#endif /* defined(__hpux) || defined(MacDarwin) || defined(SOLARIS) */
char *
os_ctime(time_t *timep, char *time_buffer, int timebuf_size)
{
char *bp;
bp = ctime_r(timep, time_buffer);
if (bp == NULL) {
//Perror(NULL, "ctime_r() failed");
sprintf(time_buffer, "<no time available>");
} else {
if (bp = strrchr(time_buffer, '\n')) {
*bp = '\0';
}
}
return(time_buffer);
}
#if !defined(INLINE_FUNCS)
int
os_create_directory(char *dir_path, int permissions)
{
return( mkdir(dir_path, permissions) );
}
int
os_remove_directory(char *dir_path)
{
return( rmdir(dir_path) );
}
#endif /* !defined(INLINE_FUNCS) */
char *
os_getcwd(void)
{
char path[PATH_BUFFER_SIZE];
if ( getcwd(path, sizeof(path)) == NULL ) {
return(NULL);
} else {
return ( strdup(path) );
}
}
os_dev_t
os_get_devID(char *path, HANDLE handle)
{
struct stat st;
os_dev_t devID = (os_dev_t)FAILURE;
int status;
if (handle == INVALID_HANDLE_VALUE) {
status = stat(path, &st);
} else {
status = fstat(handle, &st);
}
if (status == SUCCESS) {
if (st.st_rdev) {
devID = st.st_rdev;
} else {
devID = st.st_dev;
}
}
return(devID);
}
os_ino_t
os_get_fileID(char *path, HANDLE handle)
{
struct stat st;
os_ino_t fileID = (os_ino_t)FAILURE;
int status;
if (handle == INVALID_HANDLE_VALUE) {
status = stat(path, &st);
} else {
status = fstat(handle, &st);
}
if (status == SUCCESS) {
fileID = st.st_ino;
}
return(fileID);
}
large_t
os_get_file_size(char *path, HANDLE handle)
{
struct stat sb, *stp = &sb;
slarge_t filesize = -1LL;
int status;
if (handle == INVALID_HANDLE_VALUE) {
status = stat(path, stp);
} else {
status = fstat(handle, stp);
}
if (status == SUCCESS) {
return( (large_t)stp->st_size );
} else {
return( (large_t)filesize );
}
}
char *
os_gethostname(void)
{
char hostname[MAXHOSTNAMELEN];
if (gethostname(hostname, sizeof(hostname)) == FAILURE) {
//os_perror(NULL, "gethostname() failed");
return(NULL);
}
return ( strdup(hostname) );
}
#define IPv4_ADDRSIZE 4
#define IPv4_STRSIZE INET_ADDRSTRLEN // 16
#define IPv6_ADDRSIZE 16
#define IPv6_STRSIZE INET6_ADDRSTRLEN // 46
/*
* Note: I'm new to network programming, thus lots of documentation!
*/
char *
os_getaddrinfo(dinfo_t *dip, char *host, int family, void **sa, socklen_t *salen)
{
char address_str[IPv6_STRSIZE];
char *ipv4 = NULL, *ipv6 = NULL;
struct addrinfo hints, *addrinfop, *aip;
int status;
memset(&hints, 0, sizeof hints);
memset(address_str, 0, sizeof(address_str));
if (sa && *sa) {
free(*sa);
*sa = NULL;
}
if (family) {
hints.ai_family = family;
} else {
hints.ai_family = AF_UNSPEC; /* IPv4 and IPv6 allowed. */
}
hints.ai_socktype = SOCK_STREAM;
/*
* int getaddrinfo(const char *nodename, const char *servname,
* const struct addrinfo *hints, struct addrinfo **res);
*/
if ((status = getaddrinfo(host, NULL, &hints, &addrinfop)) != SUCCESS) {
return(NULL);
}
/* Loop through all the results to find the desired address. */
for (aip = addrinfop; (aip != NULL); aip = aip->ai_next) {
if (aip->ai_family == AF_INET) {
/*
* struct sockaddr_in {
* short sin_family;
* u_short sin_port;
* struct in_addr sin_addr;
* char sin_zero[8];
* };
*/
struct sockaddr_in *sainp = (struct sockaddr_in *)(aip->ai_addr);
/*
* OS's NOT supporting these are in trouble! (not supported)
*
* const char *inet_ntop(int af, const void *src,
* char *dst, socklen_t size);
* int inet_pton(int af, const char *cp, void *addr);
*/
const char *p = inet_ntop(AF_INET, &sainp->sin_addr, address_str, IPv4_STRSIZE);
if (p) {
if (ipv4) free(ipv4);
ipv4 = strdup(p);
}
/* Optionally save the socket address for other API's! */
if (sa) {
*salen = (socklen_t)sizeof(*sainp);
if (*sa) free(*sa);
*sa = Malloc(dip, (size_t)*salen);
if (*sa) {
memcpy(*sa, sainp, *salen);
}
}
/* We prefer IPv4 address for now! */
/* We'll hope the last is an IPv4 address. */
//break;
} else if (aip->ai_family == AF_INET6) {
/*
* struct sockaddr_in6 {
* short sin6_family;
* u_short sin6_port;
* u_long sin6_flowinfo;
* struct in6_addr sin6_addr;
* u_long sin6_scope_id;
* };
*/
struct sockaddr_in6 *sain6p = (struct sockaddr_in6 *)(aip->ai_addr);
const char *p = inet_ntop(AF_INET6, &sain6p->sin6_addr, address_str, IPv6_STRSIZE);
if (p) {
/*
* Note: This is required for Windows, not sure about Unix but adding it here too!
*
* There can be multiple IP addresses returned, and "::1" is for the IPv6 local host.
* The ::1 address (or, rather, in any address, that has a double colon in it) double
* colon expands into the number of zero-bits, neccessary to pad the address to full
* length, so the expanded version looks like 0000:0000:0000:0000:0000:0000:0000:0001.
*/
if ( EQ(p, "::1") ) {
continue;
}
if (ipv6) free(ipv6);
ipv6 = strdup(address_str);
}
/* Optionally save the socket address for other API's! */
if (sa) {
*salen = (socklen_t)sizeof(*sain6p);
if (*sa) free(*sa);
*sa = Malloc(dip, (size_t)*salen);
if (*sa) {
memcpy(*sa, sain6p, *salen);
}
}
/* Don't break, instead try for IPv4 address! */
//break;
}
/* continue to the next entry... */
}
freeaddrinfo(addrinfop);
/* We'll favor IPv4 vs. IPv6 (for now)! */
if (ipv4 && ipv6) {
free(ipv6); ipv6 = NULL;
}
return( (ipv4) ? ipv4 : ipv6 );
}
/*
* Convert a network (socket) address to a host name.