1#!/sw/bin/gawk -f
2
3# Usage: handle_res.awk <user> <machine> <time>
4#
5# Copyright 2001 Stephan Schulz, schulz@informatik.tu-muenchen.de
6#
7# Generate a file home/<user>/HOST_RESERVED_<machine> and delete it
8# again after <time> hours (unless it has been rewritten by some other
9# process)
10#
11
12function get_hostname(   pipe, tmp)
13{
14   pipe = "hostname";
15   pipe | getline tmp;
16   close(pipe);
17   if(!tmp)
18   {
19      print "handle_res.awk: Cannot get hostname?!?" > "/dev/stderr";
20      exit 1;
21   }
22    return tmp;
23}
24
25function get_pid(     tmp)
26{
27   getline tmp < "/dev/pid";
28   close("/dev/pid");
29   if(!tmp)
30   {
31      print "handle_res.awk: Cannot get PID ?!?" > "/dev/stderr";
32      exit 1;
33   }
34   return tmp;
35}
36
37function file_exists(file,    test, tmp)
38{
39  test = getline tmp < file;
40  close(file);
41  if(test == -1)
42    {
43      return 0;
44    }
45  return 1;
46}
47
48# round --- do normal rounding
49#
50# Arnold Robbins, arnold@gnu.org, August, 1996
51# Public Domain
52
53function round(x,   ival, aval, fraction)
54{
55   ival = int(x)    # integer part, int() truncates
56
57   # see if fractional part
58   if (ival == x)   # no fraction
59      return x
60
61   if (x < 0) {
62      aval = -x     # absolute value
63      ival = int(aval)
64      fraction = aval - ival
65      if (fraction >= .5)
66         return int(x) - 1   # -2.5 --> -3
67      else
68         return int(x)       # -2.3 --> -2
69   } else {
70      fraction = x - ival
71      if (fraction >= .5)
72         return ival + 1
73      else
74         return ival
75   }
76}
77
78# End of Arnold Robbins PD code
79
80
81BEGIN{
82   home = "/home"
83   global_procid = get_pid();
84   global_hostname = get_hostname();
85
86   if(ARGC!=4)
87   {
88      print "Usage: handle_res.awk <user> <machine> <time>" > "/dev/stderr";
89      exit 1;
90   }
91   user     = ARGV[1];
92   machine  = ARGV[2];
93   duration = ARGV[3];
94   sdur     = round(duration * 3600);
95   ARGV[1] = "";
96   ARGV[2] = "";
97   ARGV[3] = "";
98
99   res_file = home "/" user "/HOST_RESERVED_" machine;
100
101   print "\nhandle_res: PID is " global_procid " on host " global_hostname ".";
102   print "handle_res: Reserved host is " machine ".";
103   print "handle_res: Reservation file is " res_file ".";
104   print "handle_res: Reservation time is " duration " hours (" sdur " seconds).\n";
105
106   fingerprint = global_hostname "-+-" global_procid;
107   print fingerprint > res_file;
108   close(res_file);
109
110   system("sleep " sdur);
111
112   test = getline tmp < res_file;
113   close(res_file);
114   # print "\n";
115   if(test == -1)
116   {
117      # print "handle_res: Cannot find file anymore!" > "/dev/stderr";
118      exit 1;
119   }
120   if(tmp == fingerprint)
121   {
122      # print "handle_res: Deleting my reservation file!" > "/dev/stderr";
123      system("rm " res_file);
124      exit 0;
125   }
126   if(match(tmp, /sunjessen.*-\+-[0-9]*/))
127   {
128      # print "handle_res: Reservation file seems to belong to someone else" > "/dev/stderr";
129      exit 0;
130   }
131   # print "handle_res: Deleting legacy or malformed reservation file" > "/dev/stderr";
132   system("rm " res_file);
133}
134
135