Whamcloud - gitweb
306c40e273e337f3238c1ef8020ea4138501e23f
[fs/lustre-release.git] / libsysio / tests / helper.pm
1 #!/usr/bin/perl -w
2
3 #
4 # Provides a set of helper routines for use in the Perl 
5 # test scripts
6 #
7
8 package helper;
9 use strict;
10 use POSIX;
11
12 BEGIN{}
13
14 # Print out a given error message, close the command file
15 # and exit
16 sub print_and_exit
17 {
18   my ($cmdfh, $outfh, $exit_num, $exit_str) = @_;
19
20   print STDOUT "$exit_str";
21
22   # Clean up
23   my $cmdstr =  'FREE $buf';
24   $cmdstr = $cmdstr."\n";
25
26   print $cmdfh $cmdstr;
27
28   my $res = <$outfh>;
29   chop($res);
30
31   print $cmdfh "exit\n";
32   close $outfh;
33
34   # Give test_driver time to finish
35   sleep 0.000001;
36
37   exit $exit_num;
38 }
39
40
41 # Output the given command and make sure that the exit
42 # code for the command was valid
43 sub send_cmd
44 {
45   my ($cmdfh, $outfh, $cmd, $cmdstr) = @_;
46
47   print $cmdfh $cmdstr;
48
49   my $res = <$outfh>;
50   chop($res);
51   if ($res ne "0000 ") {
52     print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n");
53   }
54 }
55
56 # Check the return value from the last libsysio call
57 sub verify_cmd
58 {
59
60   my ($cmdfh, $outfh, $cmd) = @_;
61
62   # Verify the system call's output
63   my $cmdstr = 'PRINT $$';
64   $cmdstr .= "\n";
65   send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);  
66
67   my $res = <$outfh>;
68   chop($res);
69
70   if ($res eq "0xffffffff") {
71      
72     # Get the errno
73     $cmdstr = 'PRINT $errno';
74     $cmdstr .= "\n";
75     send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
76     
77     my $err = <$outfh>;
78     chop($err);
79     print_and_exit($cmdfh, $outfh, 1, "ERROR!  $cmd returned $err\n");
80   }
81   return $res;
82 }
83
84 # Compares two numbers.  Output error message and exit if
85 # they differ
86 sub cmp_nums
87 {
88   my ($cmdfh, $outfh, $ionum, $pnum, $desc) = @_;
89
90   my $str;
91   if (!defined($ionum)) {
92       print_and_exit($cmdfh, $outfh, 1, "ERROR! ionum for $desc undefined");
93   } elsif (!defined($pnum)) {
94       print_and_exit($cmdfh, $outfh, 1, "ERROR! pnum for $desc undefined");
95   }
96   if ($ionum != $pnum) {
97     my $str = sprintf("ERROR!  Sysio's number %x does not match Perl's (%x)\n", 
98                       $ionum, $pnum);
99     $str = sprintf("%s Numbers were %s\n", $str, $desc);
100     print_and_exit($cmdfh, $outfh, 1, $str);
101   }
102 }
103
104 sub get_type
105 {
106   my $mode = $_[0];
107   my $t = '?';
108
109   if (S_ISDIR($mode)) {
110     $t = 'd';
111   } elsif (S_ISCHR($mode)) {
112     $t = 'c';
113   } elsif (S_ISBLK($mode)) {
114     $t = 'b';
115   } elsif (S_ISREG($mode)) {
116     $t = 'f';
117   } elsif (S_ISFIFO($mode)) {
118     $t = 'p';
119   } elsif (S_ISLNK($mode)) {
120     $t = 'S';
121   } elsif (S_ISSOCK($mode)) {
122     $t = 's';
123   }
124
125   return $t;
126 }
127
128 END{}
129
130 1;