File: common.c

package info (click to toggle)
ltspfs 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 396 kB
  • sloc: ansic: 2,104; sh: 276; python: 149; makefile: 34
file content (368 lines) | stat: -rw-r--r-- 8,606 bytes parent folder | download | duplicates (5)
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
/*
 * common.c: support routines for ltspfs.
 *

 * Copyright Scott Balneaves, sbalneav@ltsp.org, 2005, 2006, 2007

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.

 * 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.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, you can find it on the World Wide
 * Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.

 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <rpc/xdr.h>
#include "ltspfs.h"
#include "common.h"

/*
 * Open up a server socket.
 */

extern int mounted;

int bindsocket(int port)
{
    struct sockaddr_in serv_addr;
    int s;

    /*
     * Open up our socket
     */

    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0)
        error_die("Can't open socket");

    /*
     *  bind server port
     */

    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(port);

    if (bind(s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error_die("Can't bind port");

    /*
     * Listen for connections
     */

    if (listen(s, BACKLOG) < 0)
        error_die("Listen failed");

    return s;
}

/*
 * Open up a client socket.
 */

int opensocket(char *hostname, int port)
{
    struct sockaddr_in serv_addr;
    struct hostent *server;
    int s;

    /*
     * Open up our socket
     */

    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
        fprintf(stderr, "ERROR opening socket\n");
        exit(1);
    }

    server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr, "ERROR, no such host\n");
        exit(1);
    }

    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *) server->h_addr,
          (char *) &serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(port);
    if (connect(s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        fprintf(stderr, "ERROR connecting\n");
        exit(1);
    }

    return s;
}

/*
 * _readn: read n bytes from the socket
 * The select() function is used to handle timeouts.  On a timeout, the
 * timeout_function is called.
 */

int
_readn(register int fd, register char *ptr, register int nbytes,
       int timeout_secs, void (*timeout_function) (), int doselect)
{
    int nleft, nread;
    int r;
    fd_set set;                 /* For select */
    struct timeval ltspfs_timeout;      /* Timeout */
    struct timeval *timeout_ptr = &ltspfs_timeout;

    FD_ZERO(&set);
    FD_SET(fd, &set);

    if (!doselect)
        timeout_ptr = NULL;

    nleft = nbytes;

    while (nleft > 0) {
        ltspfs_timeout.tv_sec = timeout_secs;
        ltspfs_timeout.tv_usec = 0;
        r = select(FD_SETSIZE, &set, NULL, NULL, timeout_ptr);
        if (r < 0)
            return r;
        else if (r == 0)
            timeout_function();                  /* this should never return */
        else {
            nread = read(fd, ptr, nleft);
            if (nread < 0)
                return (nread);                  /* error, return < 0 */
            else if (nread == 0)
                break;                           /* EOF */

            nleft -= nread;
            ptr += nread;
        }
    }

    return (nbytes - nleft);                     /* return >= 0 */
}

/*
 * writen: write n bytes to the socket
 */

int
_writen(register int fd, register char *ptr, register int nbytes,
        int timeout_secs, void (*timeout_function) (), int doselect)
{
    int nleft, nwritten;
    int r;
    fd_set set;                                  /* For select */
    struct timeval ltspfs_timeout;               /* Timeout */
    struct timeval *timeout_ptr = &ltspfs_timeout;

    FD_ZERO(&set);
    FD_SET(fd, &set);

    if (!doselect)
        timeout_ptr = NULL;

    nleft = nbytes;

    while (nleft > 0) {
        ltspfs_timeout.tv_sec = timeout_secs;
        ltspfs_timeout.tv_usec = 0;
        r = select(FD_SETSIZE, NULL, &set, NULL, timeout_ptr);
        if (r < 0)
            return r;
        else if (r == 0)
            timeout_function();                  /* it's expected that this will never return */
        else {
            nwritten = write(fd, ptr, nleft);
            if (nwritten <= 0)
                return (nwritten);               /* error, return < 0 */

            nleft -= nwritten;
            ptr += nwritten;
        }
    }

    return (nbytes - nleft);                     /* return >= 0 */
}

/*
 * These are the user functions.  They handle some things with less parameters.
 */

int readn(register int fd, register char *ptr, register int maxlen)
{
    return _readn(fd, ptr, maxlen, LTSPFS_TIMEOUT, &timeout, TRUE);
}

int writen(register int fd, register char *ptr, register int nbytes)
{
    return _writen(fd, ptr, nbytes, LTSPFS_TIMEOUT, &timeout, TRUE);
}

#ifdef AUTOMOUNT
/*
 * Automounter.
 */

void am_mount(char *mountpoint)
{
    struct stat buf;
    char *path[] = { "/usr/sbin/ltspfs_mount", mountpoint, NULL };
    pid_t pid;
    int status;


    if (debug)
        info("am_mount called\n");
    if (mounted)                                 /* buh? */
        return;

    /*
     * Check if the mount script exists before calling
     */

    if (!stat(path[0], &buf)) {
        pid = fork();
        if (pid == 0) {
            execv(path[0], path);
            perror("execv");
            exit(1);
        } else if (pid < 0) {
            perror("fork");
            exit(1);
        }

        wait(&status);
    }
    mounted = 1;
}

void am_umount(char *mountpoint)
{
    struct stat buf;
    char *path[] = { "/usr/sbin/ltspfs_umount", mountpoint, NULL };
    pid_t pid;
    int status;


    if (debug)
        info("am_umount called\n");
    if (!mounted)                                /* buh? */
        return;

    /*
     * Check if the umount script exists before calling
     */

    if (!stat(path[0], &buf)) {
        pid = fork();
        if (pid == 0) {
            execv(path[0], path);
            perror("execv");
            exit(1);
        } else if (pid < 0) {
            perror("fork");
            exit(1);
        }

        wait(&status);
    }
    mounted = 0;
}

#endif

/*
 * status_return is used to simplify a bunch of functions that either
 * return a simple error, or an all clear error code.
 */

int status_return(int sockfd, int result)
{
    XDR out;
    char output[BUFSIZ];
    int i = 0;

    xdrmem_create(&out, output, BUFSIZ, XDR_ENCODE);
    xdr_int(&out, &i);                           /* bogus length */

    if (result == FAIL) {
        i = LTSP_STATUS_FAIL;
        xdr_int(&out, &i);
        if (debug)
            info("status_return STATUS_FAIL\n");
        xdr_int(&out, &errno);
    } else {
        i = LTSP_STATUS_OK;
        if (debug)
            info("status_return STATUS_OK\n");
        xdr_int(&out, &i);
    }

    i = xdr_getpos(&out);                        /* get current position */
    xdr_setpos(&out, 0);                         /* rewind to the beginning */
    xdr_int(&out, &i);                           /* Write the correct length */


    writen(sockfd, output, i);
    return 0;
}

/*
 * error_die:
 * Issue an error to syslog and die.
 */

void error_die(char *err)
{
    info("%s (errno = %d : %s): Shutting down\n", err, errno,
         strerror(errno));
    exit(ERROR);
}

/*
 * info: logs messages to syslog and/or stderr.
 *
 * Thanks to Petter Reinholdtsen for the heads up on the v... functions.
 */

void info(const char *format, ...)
{
    va_list ap;

    va_start(ap, format);

    if (syslogopen)
        vsyslog(LOG_INFO, format, ap);
    if (debug)
        vfprintf(stderr, format, ap);

    va_end(ap);
}