0eafec0b74d6822f7305e453b69c498eac97f789
[debian/amanda] / common-src / util.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: util.c,v 1.17 2006/01/14 04:37:19 paddy_s Exp $
28  */
29
30 #include "amanda.h"
31 #include "util.h"
32
33 /*
34  * Keep calling read() until we've read buflen's worth of data, or EOF,
35  * or we get an error.
36  *
37  * Returns the number of bytes read, 0 on EOF, or negative on error.
38  */
39 ssize_t
40 fullread(fd, vbuf, buflen)
41     int fd;
42     void *vbuf;
43     size_t buflen;
44 {
45     ssize_t nread, tot = 0;
46     char *buf = vbuf;   /* cast to char so we can ++ it */
47
48     while (buflen > 0) {
49         nread = read(fd, buf, buflen);
50         if (nread < 0) {
51             if ((errno == EINTR) || (errno == EAGAIN))
52                 continue;
53             return ((tot > 0) ? tot : -1);
54         }
55
56         if (nread == 0)
57             break;
58
59         tot += nread;
60         buf += nread;
61         buflen -= nread;
62     }
63     return (tot);
64 }
65
66 /*
67  * Keep calling write() until we've written buflen's worth of data,
68  * or we get an error.
69  *
70  * Returns the number of bytes written, or negative on error.
71  */
72 ssize_t
73 fullwrite(fd, vbuf, buflen)
74     int fd;
75     const void *vbuf;
76     size_t buflen;
77 {
78     ssize_t nwritten, tot = 0;
79     const char *buf = vbuf;     /* cast to char so we can ++ it */
80
81     while (buflen > 0) {
82         nwritten = write(fd, buf, buflen);
83         if (nwritten < 0) {
84             if ((errno == EINTR) || (errno == EAGAIN))
85                 continue;
86             return ((tot > 0) ? tot : -1);
87         }
88         tot += nwritten;
89         buf += nwritten;
90         buflen -= nwritten;
91     }
92     return (tot);
93 }
94
95 /*
96  * Bind to a port in the given range.  Takes a begin,end pair of port numbers.
97  *
98  * Returns negative on error (EGAIN if all ports are in use).  Returns 0
99  * on success.
100  */
101 int
102 bind_portrange(s, addrp, first_port, last_port, proto)
103     int s;
104     struct sockaddr_in *addrp;
105     int first_port, last_port;
106     char *proto;
107 {
108     int port, cnt;
109     const int num_ports = last_port - first_port + 1;
110     int save_errno;
111     struct servent *servPort;
112
113     assert(first_port > 0 && first_port <= last_port && last_port < 65536);
114
115     /*
116      * We pick a different starting port based on our pid and the current
117      * time to avoid always picking the same reserved port twice.
118      */
119     port = ((getpid() + time(0)) % num_ports) + first_port;
120     /*
121      * Scan through the range, trying all available ports that are either 
122      * not taken in /etc/services or registered for *amanda*.  Wrap around
123      * if we don't happen to start at the beginning.
124      */
125     for (cnt = 0; cnt < num_ports; cnt++) {
126         servPort = getservbyport(htons(port), proto);
127         if((servPort == NULL) || strstr(servPort->s_name, "amanda")){
128             dbprintf(("%s: bind_portrange2: trying port=%d\n",
129                       debug_prefix_time(NULL), port));
130             addrp->sin_port = htons(port);
131             if (bind(s, (struct sockaddr *)addrp, sizeof(*addrp)) >= 0)
132                 return 0;
133             /*
134              * If the error was something other then port in use, stop.
135              */
136             if (errno != EADDRINUSE)
137                 break;
138         }
139         if (++port > last_port)
140             port = first_port;
141     }
142     if (cnt == num_ports) {
143         dbprintf(("%s: bind_portrange: all ports between %d and %d busy\n",
144                   debug_prefix_time(NULL),
145                   first_port,
146                   last_port));
147         errno = EAGAIN;
148     } else if (last_port < IPPORT_RESERVED
149                && getuid() != 0
150                && errno == EACCES) {
151         /*
152          * Do not bother with an error message in this case because it
153          * is expected.
154          */
155     } else {
156         save_errno = errno;
157         dbprintf(("%s: bind_portrange: port %d: %s\n",
158                   debug_prefix_time(NULL),
159                   port,
160                   strerror(errno)));
161         errno = save_errno;
162     }
163     return -1;
164 }
165
166 /*
167  * Construct a datestamp (YYYYMMDD) from a time_t.
168  */
169 char *
170 construct_datestamp(t)
171     time_t *t;
172 {
173     struct tm *tm;
174     char datestamp[3*NUM_STR_SIZE];
175     time_t when;
176
177     if(t == NULL) {
178         when = time((time_t *)NULL);
179     } else {
180         when = *t;
181     }
182     tm = localtime(&when);
183     snprintf(datestamp, sizeof(datestamp),
184              "%04d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
185     return stralloc(datestamp);
186 }
187
188 /*
189  * Construct a timestamp (YYYYMMDDHHMMSS) from a time_t.
190  */
191 char *
192 construct_timestamp(t)
193     time_t *t;
194 {
195     struct tm *tm;
196     char timestamp[6*NUM_STR_SIZE];
197     time_t when;
198
199     if(t == NULL) {
200         when = time((time_t *)NULL);
201     } else {
202         when = *t;
203     }
204     tm = localtime(&when);
205     snprintf(timestamp, sizeof(timestamp),
206              "%04d%02d%02d%02d%02d%02d",
207              tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
208              tm->tm_hour, tm->tm_min, tm->tm_sec);
209     return stralloc(timestamp);
210 }