2 * Network Block Device - server
4 * Copyright 1996-1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
6 * Copyright 2001-2004 Wouter Verhelst <wouter@debian.org>
7 * Copyright 2002 Anton Altaparmakov <aia21@cam.ac.uk>
9 * Version 1.0 - hopefully 64-bit-clean
10 * Version 1.1 - merging enhancements from Josh Parsons, <josh@coombs.anu.edu.au>
11 * Version 1.2 - autodetect size of block devices, thanx to Peter T. Breuer" <ptb@it.uc3m.es>
12 * Version 1.5 - can compile on Unix systems that don't have 64 bit integer
13 * type, or don't have 64 bit file offsets by defining FS_32BIT
14 * in compile options for nbd-server *only*. This can be done
15 * with make FSCHOICE=-DFS_32BIT nbd-server. (I don't have the
16 * original autoconf input file, or I would make it a configure
17 * option.) Ken Yap <ken@nlc.net.au>.
18 * Version 1.6 - fix autodetection of block device size and really make 64 bit
19 * clean on 32 bit machines. Anton Altaparmakov <aia21@cam.ac.uk>
20 * Version 2.0 - Version synchronised with client
21 * Version 2.1 - Reap zombie client processes when they exit. Removed
22 * (uncommented) the _IO magic, it's no longer necessary. Wouter
23 * Verhelst <wouter@debian.org>
24 * Version 2.2 - Auto switch to read-only mode (usefull for floppies).
25 * Version 2.3 - Fixed code so that Large File Support works. This
26 * removes the FS_32BIT compile-time directive; define
27 * _FILE_OFFSET_BITS=64 and _LARGEFILE_SOURCE if you used to be
28 * using FS_32BIT. This will allow you to use files >2GB instead of
29 * having to use the -m option. Wouter Verhelst <wouter@debian.org>
30 * Version 2.4 - Added code to keep track of children, so that we can
31 * properly kill them from initscripts. Add a call to daemon(),
32 * so that processes don't think they have to wait for us, which is
33 * interesting for initscripts as well. Wouter Verhelst
35 * Version 2.5 - Bugfix release: forgot to reset child_arraysize to
36 * zero after fork()ing, resulting in nbd-server going berserk
37 * when it receives a signal with at least one child open. Wouter
38 * Verhelst <wouter@debian.org>
39 * 10/10/2003 - Added socket option SO_KEEPALIVE (sf.net bug 819235);
40 * rectified type of mainloop::size_host (sf.net bugs 814435 and
41 * 817385); close the PID file after writing to it, so that the
42 * daemon can actually be found. Wouter Verhelst
44 * 10/10/2003 - Size of the data "size_host" was wrong and so was not
45 * correctly put in network endianness. Many types were corrected
46 * (size_t and off_t instead of int). <vspaceg@sourceforge.net>
47 * Version 2.6 - Some code cleanup.
48 * Version 2.7 - Better build system.
49 * 11/02/2004 - Doxygenified the source, modularized it a bit. Needs a
50 * lot more work, but this is a start. Wouter Verhelst
54 /* Includes LFS defines, which defines behaviours of some of the following
55 * headers, so must come before those */
58 #include <sys/types.h>
59 #include <sys/socket.h>
61 #include <sys/wait.h> /* wait */
62 #ifdef HAVE_SYS_IOCTL_H
63 #include <sys/ioctl.h>
65 #include <sys/param.h>
66 #ifdef HAVE_SYS_MOUNT_H
67 #include <sys/mount.h> /* For BLKGETSIZE */
69 #include <signal.h> /* sigaction */
70 #include <netinet/tcp.h>
71 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
72 #include <netdb.h> /* hostent, gethostby*, getservby* */
79 #include <arpa/inet.h>
87 /* used in cliserv.h, so must come first */
88 #define MY_NAME "nbd_server"
91 /** how much space for child PIDs we have by default. Dynamically
92 allocated, and will be realloc()ed if out of space, so this should
93 probably be fair for most situations. */
94 #define DEFAULT_CHILD_ARRAY 256
96 /** Logging macros, now nothing goes to syslog unless you say ISSERVER */
98 #define msg2(a,b) syslog(a,b)
99 #define msg3(a,b,c) syslog(a,b,c)
100 #define msg4(a,b,c,d) syslog(a,b,c,d)
102 #define msg2(a,b) g_message(b)
103 #define msg3(a,b,c) g_message(b,c)
104 #define msg4(a,b,c,d) g_message(b,c,d)
107 /* Debugging macros */
110 #define DEBUG( a ) printf( a )
111 #define DEBUG2( a,b ) printf( a,b )
112 #define DEBUG3( a,b,c ) printf( a,b,c )
115 #define DEBUG2( a,b )
116 #define DEBUG3( a,b,c )
118 #ifndef PACKAGE_VERSION
119 #define PACKAGE_VERSION ""
122 * The highest value a variable of type off_t can reach.
124 /* This is starting to get ugly. If someone knows a better way to find
125 * the maximum value of a signed type *without* relying on overflow
126 * (doing so breaks on 64bit architectures), that would be nice.
128 * Actually, do we need this at all? Can't we just say '0 is autodetect', and
129 * live with it? Or better yet, use an extra flag, or so?
130 * Answer: yes, we need it, as the hunksize is defined to this when the
131 * multiple file thingy isn't used.
133 #define OFFT_MAX (((((off_t)1)<<((sizeof(off_t)-1)*8))-1)<<7)+127
134 #define LINELEN 256 /**< Size of static buffer used to read the
135 authorization file (yuck) */
136 #define BUFSIZE (1024*1024) /**< Size of buffer that can hold requests */
137 #define GIGA (1*1024*1024*1024) /**< 1 Gigabyte. Used as hunksize when doing
138 the multiple file thingy. @todo: make this a
139 configuration option. */
140 #define DIFFPAGESIZE 4096 /**< diff file uses those chunks */
141 #define F_READONLY 1 /**< flag to tell us a file is readonly */
142 #define F_MULTIFILE 2 /**< flag to tell us a file is exported using -m */
143 #define F_COPYONWRITE 4 /**< flag to tell us a file is exported using
145 #define F_AUTOREADONLY 8 /**< flag to tell us a file is set to autoreadonly */
146 GHashTable *children;
147 char pidfname[256]; /**< name of our PID file */
148 char default_authname[] = "/etc/nbd_server.allow"; /**< default name of allow file */
151 * Variables associated with a server.
154 char* exportname; /**< (unprocessed) filename of the file we're exporting */
155 off_t hunksize; /**< size of a hunk of an exported file */
156 off_t expected_size; /**< size of the exported file as it was told to
157 us through configuration */
158 unsigned int port; /**< port we're exporting this file at */
159 char* authname; /**< filename of the authorization file */
160 int flags; /**< flags associated with this exported file */
161 unsigned int timeout;/**< how long a connection may be idle
163 int socket; /**< The socket of this server. */
167 * Variables associated with a client socket.
170 off_t exportsize; /**< size of the file we're exporting */
171 char *clientname; /**< peer */
172 char *exportname; /**< (processed) filename of the file we're exporting */
173 GArray *export; /**< array of filedescriptors of exported files;
174 only the first is actually used unless we're
175 doing the multiple file option */
176 int net; /**< The actual client socket */
177 SERVER *server; /**< The server this client is getting data from */
178 char* difffilename; /**< filename of the copy-on-write file, if any */
179 int difffile; /**< filedescriptor of copyonwrite file. @todo
180 shouldn't this be an array too? (cfr export) Or
181 make -m and -c mutually exclusive */
182 u32 difffilelen; /**< number of pages in difffile */
183 u32 *difmap; /**< see comment on the global difmap for this one */
187 * Check whether a client is allowed to connect. Works with an authorization
188 * file which contains one line per machine, no wildcards.
190 * @param name IP address of client trying to connect (in human-readable form)
191 * @return 0 - authorization refused, 1 - OK
193 int authorized_client(CLIENT *opts) {
198 if ((f=fopen(opts->server->authname,"r"))==NULL) {
199 msg4(LOG_INFO,"Can't open authorization file %s (%s).",
200 opts->server->authname,strerror(errno)) ;
204 while (fgets(line,LINELEN,f)!=NULL) {
205 if (strncmp(line,opts->clientname,strlen(opts->clientname))==0) {
215 * Read data from a file descriptor into a buffer
217 * @param f a file descriptor
218 * @param buf a buffer
219 * @param len the number of bytes to be read
221 inline void readit(int f, void *buf, size_t len) {
225 if ((res = read(f, buf, len)) <= 0)
226 err("Read failed: %m");
233 * Write data from a buffer into a filedescriptor
235 * @param f a file descriptor
236 * @param buf a buffer containing data
237 * @param len the number of bytes to be written
239 inline void writeit(int f, void *buf, size_t len) {
243 if ((res = write(f, buf, len)) <= 0)
244 err("Send failed: %m");
251 * Print out a message about how to use nbd-server. Split out to a separate
252 * function so that we can call it from multiple places
255 printf("This is nbd-server version " VERSION "\n");
256 printf("Usage: port file_to_export [size][kKmM] [-l authorize_file] [-r] [-m] [-c] [-a timeout_sec]\n"
257 "\t-r|--read-only\t\tread only\n"
258 "\t-m|--multi-file\t\tmultiple file\n"
259 "\t-c|--copy-on-write\tcopy on write\n"
260 "\t-l|--authorize-file\tfile with list of hosts that are allowed to\n\t\t\t\tconnect.\n"
261 "\t-a|--idle-time\t\tmaximum idle seconds; server terminates when\n\t\t\t\tidle time exceeded\n\n"
262 "\tif port is set to 0, stdin is used (for running from inetd)\n"
263 "\tif file_to_export contains '%%s', it is substituted with the IP\n"
264 "\t\taddress of the machine trying to connect\n" );
268 * Parse the command line.
270 * @todo getopt() is a great thing, and easy to use. Also, we want to
271 * create a configuration file which nbd-server will read. Maybe do (as in,
274 * @param argc the argc argument to main()
275 * @param argv the argv argument to main()
277 SERVER* cmdline(int argc, char *argv[]) {
280 struct option long_options[] = {
281 {"read-only", no_argument, NULL, 'r'},
282 {"multi-file", no_argument, NULL, 'm'},
283 {"copy-on-write", no_argument, NULL, 'c'},
284 {"authorize-file", required_argument, NULL, 'l'},
285 {"idle-time", required_argument, NULL, 'a'},
290 serve=g_malloc(sizeof(SERVER));
291 serve->hunksize=OFFT_MAX;
292 while((c=getopt_long(argc, argv, "a:cl:mr", long_options, &i))>=0) {
295 serve->flags |= F_READONLY;
298 serve->flags |= F_MULTIFILE;
299 serve->hunksize = 1*GIGA;
300 serve->authname = default_authname;
303 serve->flags |=F_COPYONWRITE;
306 serve->authname=optarg;
309 serve->timeout=strtol(optarg, NULL, 0);
317 /* What's left: the port to export, the name of the to be exported
318 * file, and, optionally, the size of the file, in that order. */
323 serve->port=strtol(argv[i], NULL, 0);
328 serve->exportname = argv[i];
329 if(serve->exportname[0] != '/') {
330 fprintf(stderr, "E: The to be exported file needs to be an absolute filename!\n");
335 size_t last = strlen(argv[i])-1;
336 char suffix = argv[i][last];
337 if (suffix == 'k' || suffix == 'K' ||
338 suffix == 'm' || suffix == 'M')
339 argv[i][last] = '\0';
340 es = (off_t)atol(argv[i]);
348 serve->expected_size = es;
354 * Signal handler for SIGCHLD
355 * @param s the signal we're handling (must be SIGCHLD, or something
358 void sigchld_handler(int s) {
363 while((pid=wait(status)) > 0) {
364 if(WIFEXITED(status)) {
365 msg3(LOG_INFO, "Child exited with %d", WEXITSTATUS(status));
367 i=g_hash_table_lookup(children, &pid);
369 msg3(LOG_INFO, "SIGCHLD received for an unknown child with PID %ld", (long)pid);
371 DEBUG2("Removing %d from the list of children", pid);
372 g_hash_table_remove(children, &pid);
378 * Kill a child. Called from sigterm_handler::g_hash_table_foreach.
381 * @param value the value corresponding to the above key
382 * @param user_data a pointer which we always set to 1, so that we know what
385 void killchild(gpointer key, gpointer value, gpointer user_data) {
387 int *parent=user_data;
394 * Handle SIGTERM and dispatch it to our children
395 * @param s the signal we're handling (must be SIGTERM, or something
396 * is severely wrong).
398 void sigterm_handler(int s) {
401 g_hash_table_foreach(children, killchild, &parent);
411 * Detect the size of a file.
413 * @param export An open filedescriptor
414 * @return the size of the file, or OFFT_MAX if detection was
417 off_t size_autodetect(int export) {
420 struct stat stat_buf;
423 #ifdef HAVE_SYS_MOUNT_H
424 #ifdef HAVE_SYS_IOCTL_H
426 DEBUG("looking for export size with ioctl BLKGETSIZE\n");
427 if (!ioctl(export, BLKGETSIZE, &es32) && es32) {
428 es = (off_t)es32 * (off_t)512;
431 #endif /* BLKGETSIZE */
432 #endif /* HAVE_SYS_IOCTL_H */
433 #endif /* HAVE_SYS_MOUNT_H */
435 DEBUG("looking for export size with fstat\n");
436 stat_buf.st_size = 0;
437 error = fstat(export, &stat_buf);
439 if(stat_buf.st_size > 0)
440 return (off_t)stat_buf.st_size;
442 err("fstat failed: %m");
445 DEBUG("looking for export size with lseek SEEK_END\n");
446 es = lseek(export, (off_t)0, SEEK_END);
447 if (es > ((off_t)0)) {
450 DEBUG2("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
453 err("Could not find size of exported block device: %m");
458 * seek to a position in a file, with error handling.
459 * @param handle a filedescriptor
460 * @param a position to seek to
461 * @todo get rid of this; lastpoint is a global variable right now, but it
462 * shouldn't be. If we pass it on as a parameter, that makes things a *lot*
465 void myseek(int handle,off_t a) {
466 if (lseek(handle, a, SEEK_SET) < 0) {
467 err("Can not seek locally!\n");
472 * Write an amount of bytes at a given offset to the right file. This
473 * abstracts the write-side of the multiple file option.
475 * @param a The offset where the write should start
476 * @param buf The buffer to write from
477 * @param len The length of buf
478 * @return The number of bytes actually written, or -1 in case of an error
480 int rawexpwrite(off_t a, char *buf, size_t len, CLIENT *client) {
483 myseek(g_array_index(client->export, int, (int)(a/client->server->hunksize)), a%client->server->hunksize);
485 res = write(g_array_index(client->export, int, (int)((off_t)a/(off_t)(client->server->hunksize))), buf, len);
486 return (res < 0 || (size_t)res != len);
490 * Read an amount of bytes at a given offset from the right file. This
491 * abstracts the read-side of the multiple files option.
493 * @param a The offset where the read should start
494 * @param buf A buffer to read into
495 * @param len The size of buf
496 * @return The number of bytes actually read, or -1 in case of an
499 int rawexpread(off_t a, char *buf, size_t len, CLIENT *client) {
502 myseek(g_array_index(client->export,int,(int)a/client->server->hunksize),
503 a%client->server->hunksize);
504 res = read(g_array_index(client->export,int,(int)a/client->server->hunksize), buf, len);
505 return (res < 0 || (size_t)res != len);
509 * Read an amount of bytes at a given offset from the right file. This
510 * abstracts the read-side of the copyonwrite stuff, and calls
511 * rawexpread() with the right parameters to do the actual work.
512 * @param a The offset where the read should start
513 * @param buf A buffer to read into
514 * @param len The size of buf
515 * @return The number of bytes actually read, or -1 in case of an error
517 int expread(off_t a, char *buf, size_t len, CLIENT *client) {
519 off_t mapcnt, mapl, maph, pagestart;
521 if (!(client->server->flags & F_COPYONWRITE))
522 return rawexpread(a, buf, len, client);
523 DEBUG3("Asked to read %d bytes at %Lu.\n", len, (unsigned long long)a);
525 mapl=a/DIFFPAGESIZE; maph=(a+len-1)/DIFFPAGESIZE;
527 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
528 pagestart=mapcnt*DIFFPAGESIZE;
530 rdlen=(0<DIFFPAGESIZE-offset && len<(size_t)(DIFFPAGESIZE-offset)) ?
531 len : (size_t)DIFFPAGESIZE-offset;
532 if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
533 DEBUG3("Page %Lu is at %lu\n", (unsigned long long)mapcnt,
534 (unsigned long)(client->difmap[mapcnt]));
535 myseek(client->difffile, client->difmap[mapcnt]*DIFFPAGESIZE+offset);
536 if (read(client->difffile, buf, rdlen) != rdlen) return -1;
537 } else { /* the block is not there */
538 DEBUG2("Page %Lu is not here, we read the original one\n",
539 (unsigned long long)mapcnt);
540 if(rawexpread(a, buf, rdlen, client)) return -1;
542 len-=rdlen; a+=rdlen; buf+=rdlen;
548 * Write an amount of bytes at a given offset to the right file. This
549 * abstracts the write-side of the copyonwrite option, and calls
550 * rawexpwrite() with the right parameters to do the actual work.
552 * @param a The offset where the write should start
553 * @param buf The buffer to write from
554 * @param len The length of buf
555 * @return The number of bytes actually written, or -1 in case of an error
557 int expwrite(off_t a, char *buf, size_t len, CLIENT *client) {
558 char pagebuf[DIFFPAGESIZE];
559 off_t mapcnt,mapl,maph;
564 if (!(client->server->flags & F_COPYONWRITE))
565 return(rawexpwrite(a,buf,len, client));
566 DEBUG3("Asked to write %d bytes at %Lu.\n", len, (unsigned long long)a);
568 mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
570 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
571 pagestart=mapcnt*DIFFPAGESIZE ;
573 wrlen=(0<DIFFPAGESIZE-offset && len<(size_t)(DIFFPAGESIZE-offset)) ?
574 len : (size_t)DIFFPAGESIZE-offset;
576 if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
577 DEBUG3("Page %Lu is at %lu\n", (unsigned long long)mapcnt,
578 (unsigned long)(client->difmap[mapcnt])) ;
579 myseek(client->difffile,
580 client->difmap[mapcnt]*DIFFPAGESIZE+offset);
581 if (write(client->difffile, buf, wrlen) != wrlen) return -1 ;
582 } else { /* the block is not there */
583 myseek(client->difffile,client->difffilelen*DIFFPAGESIZE) ;
584 client->difmap[mapcnt]=client->difffilelen++ ;
585 DEBUG3("Page %Lu is not here, we put it at %lu\n",
586 (unsigned long long)mapcnt,
587 (unsigned long)(client->difmap[mapcnt]));
589 if (rdlen+pagestart%(client->server->hunksize) >
590 (client->server->hunksize))
591 rdlen=client->server->hunksize -
592 (pagestart%client->server->hunksize);
593 if (rawexpread(pagestart, pagebuf, rdlen, client))
595 memcpy(pagebuf+offset,buf,wrlen) ;
596 if (write(client->difffile, pagebuf, DIFFPAGESIZE) !=
600 len-=wrlen ; a+=wrlen ; buf+=wrlen ;
606 * Do the initial negotiation.
608 * @param net A socket to do the negotiation over
610 void negotiate(CLIENT *client) {
614 memset(zeros, 0, 290);
615 if (write(client->net, INIT_PASSWD, 8) < 0)
616 err("Negotiation failed: %m");
617 cliserv_magic = htonll(cliserv_magic);
618 if (write(client->net, &cliserv_magic, sizeof(cliserv_magic)) < 0)
619 err("Negotiation failed: %m");
620 size_host = htonll((u64)(client->exportsize));
621 if (write(client->net, &size_host, 8) < 0)
622 err("Negotiation failed: %m");
623 if (write(client->net, zeros, 128) < 0)
624 err("Negotiation failed: %m");
627 /** sending macro. */
628 #define SEND(net,reply) writeit( net, &reply, sizeof( reply ));
630 #define ERROR(client,reply) { reply.error = htonl(-1); SEND(client->net,reply); reply.error = 0; }
632 * Serve a file to a single client.
634 * @todo This beast needs to be split up in many tiny little manageable
635 * pieces. Preferably with a chainsaw.
637 * @param net A network socket, connected to an nbd client
640 int mainloop(CLIENT *client) {
641 struct nbd_request request;
642 struct nbd_reply reply;
648 DEBUG("Entering request loop!\n");
649 reply.magic = htonl(NBD_REPLY_MAGIC);
658 if (client->server->timeout)
659 alarm(client->server->timeout);
660 readit(client->net, &request, sizeof(request));
661 request.from = ntohll(request.from);
662 request.type = ntohl(request.type);
664 if (request.type==NBD_CMD_DISC) {
665 msg2(LOG_INFO, "Disconnect request received.");
666 if (client->difmap) g_free(client->difmap) ;
667 if (client->difffile>=0) {
668 close(client->difffile);
669 unlink(client->difffilename);
670 free(client->difffilename);
676 len = ntohl(request.len);
678 if (request.magic != htonl(NBD_REQUEST_MAGIC))
679 err("Not enough magic.");
681 err("Request too big!");
683 printf("%s from %Lu (%Lu) len %d, ", request.type ? "WRITE" :
684 "READ", (unsigned long long)request.from,
685 (unsigned long long)request.from / 512, len);
687 memcpy(reply.handle, request.handle, sizeof(reply.handle));
688 if ((request.from + len) > (OFFT_MAX)) {
689 DEBUG("[Number too large!]");
690 ERROR(client, reply);
694 if (((ssize_t)((off_t)request.from + len) > client->exportsize) ||
695 ((client->server->flags & F_READONLY) && request.type)) {
697 ERROR(client, reply);
701 if (request.type==NBD_CMD_WRITE) {
702 DEBUG("wr: net->buf, ");
703 readit(client->net, buf, len);
705 if ((client->server->flags & F_AUTOREADONLY) ||
706 expwrite(request.from, buf, len,
708 DEBUG("Write failed: %m" );
709 ERROR(client, reply);
712 SEND(client->net, reply);
719 if (expread(request.from, buf + sizeof(struct nbd_reply), len, client)) {
720 DEBUG("Read failed: %m");
721 ERROR(client, reply);
726 memcpy(buf, &reply, sizeof(struct nbd_reply));
727 writeit(client->net, buf, len + sizeof(struct nbd_reply));
734 * Split a single exportfile into multiple ones, if that was asked.
735 * @return 0 on success, -1 on failure
736 * @param client information on the client which we want to split
738 int splitexport(CLIENT* client) {
742 client->export = g_array_new(TRUE, TRUE, sizeof(int));
743 for (i=0; i<client->exportsize; i+=client->server->hunksize) {
746 if(client->server->flags & F_MULTIFILE) {
747 tmpname=g_strdup_printf("%s.%d", client->exportname,
748 (int)(i/client->server->hunksize));
750 tmpname=g_strdup(client->exportname);
752 DEBUG2( "Opening %s\n", tmpname );
753 if((fhandle = open(tmpname, (client->server->flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1) {
754 /* Read WRITE ACCESS was requested by media is only read only */
755 client->server->flags |= F_AUTOREADONLY;
756 client->server->flags |= F_READONLY;
757 if((fhandle = open(tmpname, O_RDONLY)) == -1)
758 err("Could not open exported file: %m");
760 g_array_insert_val(client->export,i/client->server->hunksize,fhandle);
765 int copyonwrite_prepare(CLIENT* client)
768 if ((client->difffilename = malloc(1024))==NULL)
769 err("Failed to allocate string for diff file name");
770 snprintf(client->difffilename, 1024, "%s-%s-%d.diff",client->exportname,client->clientname,
772 client->difffilename[1023]='\0';
773 msg3(LOG_INFO,"About to create map and diff file %s",client->difffilename) ;
774 client->difffile=open(client->difffilename,O_RDWR | O_CREAT | O_TRUNC,0600) ;
775 if (client->difffile<0) err("Could not create diff file (%m)") ;
776 if ((client->difmap=calloc(client->exportsize/DIFFPAGESIZE,sizeof(u32)))==NULL)
777 err("Could not allocate memory") ;
778 for (i=0;i<client->exportsize/DIFFPAGESIZE;i++) client->difmap[i]=(u32)-1 ;
784 * Serve a connection.
786 * @todo allow for multithreading, perhaps use libevent. Not just yet, though;
787 * follow the road map.
789 * @param net A network socket connected to an nbd client
791 void serveconnection(CLIENT *client) {
794 if (!client->server->expected_size) {
795 client->exportsize = size_autodetect(g_array_index(client->export,int,0));
797 /* Perhaps we should check first. Not now. */
798 client->exportsize = client->server->expected_size;
800 if (client->exportsize > OFFT_MAX) {
801 /* uhm, well... In a parallel universe, this *might* be
803 err("Size of exported file is too big\n");
806 msg3(LOG_INFO, "size of exported file/device is %Lu", (unsigned long long)client->exportsize);
809 if (client->server->flags & F_COPYONWRITE) {
810 copyonwrite_prepare(client);
813 setmysockopt(client->net);
819 * Find the name of the file we have to serve. This will use g_strdup_printf
820 * to put the IP address of the client inside a filename containing
821 * "%s". That name is then written to client->exportname.
823 * @param net A socket connected to an nbd client
824 * @param client information about the client. The IP address in human-readable
825 * format will be written to a new char* buffer, the address of which will be
826 * stored in client->clientname.
828 void set_peername(int net, CLIENT *client) {
829 struct sockaddr_in addrin;
830 int addrinlen = sizeof( addrin );
833 if (getpeername(net, (struct sockaddr *) &addrin, (socklen_t *)&addrinlen) < 0)
834 err("getsockname failed: %m");
835 peername = inet_ntoa(addrin.sin_addr);
836 client->exportname=g_strdup_printf(client->server->exportname, peername);
838 msg4(LOG_INFO, "connect from %s, assigned file is %s",
839 peername, client->exportname);
840 client->clientname=g_strdup(peername);
845 * @param data a pointer to pid_t which should be freed
847 void destroy_pid_t(gpointer data) {
852 * Go daemon (unless we specified at compile time that we didn't want this)
853 * @param serve the first server of our configuration. If its port is zero,
854 * then do not daemonize, because we're doing inetd then.
856 #if !defined(NODAEMON) && !defined(NOFORK)
857 void daemonize(SERVER* serve) {
864 snprintf(pidfname, sizeof(char)*255, "/var/run/nbd-server.%d.pid", serve->port);
865 pidf=fopen(pidfname, "w");
867 fprintf(pidf,"%d", (int)getpid());
871 fprintf(stderr, "Not fatal; continuing");
876 #define daemonize(serve)
877 #endif /* !defined(NODAEMON) && !defined(NOFORK) */
880 * Connect a server's socket.
882 * @todo modularize this giant beast. Preferably with a chainsaw. Also,
883 * it has no business starting mainloop(), through serveconnection(); it
884 * should connect, and be done with it.
886 * @param serve the server we want to connect.
888 void setup_serve(SERVER* serve) {
889 struct sockaddr_in addrin;
891 int addrinlen = sizeof(addrin);
898 if ((serve->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
901 /* lose the pesky "Address already in use" error message */
902 if (setsockopt(serve->socket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
903 err("setsockopt SO_REUSEADDR");
905 if (setsockopt(serve->socket,SOL_SOCKET,SO_KEEPALIVE,&yes,sizeof(int)) == -1) {
906 err("setsockopt SO_KEEPALIVE");
909 DEBUG("Waiting for connections... bind, ");
910 addrin.sin_family = AF_INET;
911 addrin.sin_port = htons(serve->port);
912 addrin.sin_addr.s_addr = 0;
913 if (bind(serve->socket, (struct sockaddr *) &addrin, addrinlen) < 0)
916 if (listen(serve->socket, 1) < 0)
918 sa.sa_handler = sigchld_handler;
919 sigemptyset(&sa.sa_mask);
920 sa.sa_flags = SA_RESTART;
921 if(sigaction(SIGCHLD, &sa, NULL) == -1)
922 err("sigaction: %m");
923 sa.sa_handler = sigterm_handler;
924 sigemptyset(&sa.sa_mask);
925 sa.sa_flags = SA_RESTART;
926 if(sigaction(SIGTERM, &sa, NULL) == -1)
927 err("sigaction: %m");
928 children=g_hash_table_new_full(g_int_hash, g_int_equal, NULL, destroy_pid_t);
932 * Loop through the available servers, and serve them.
934 * Actually, right now we only handle one server. Will change that for
937 int serveloop(SERVER* serve) {
938 struct sockaddr_in addrin;
939 socklen_t addrinlen=sizeof(addrin);
946 if ((net = accept(serve->socket, (struct sockaddr *) &addrin, &addrinlen)) < 0)
949 client = g_malloc(sizeof(CLIENT));
950 client->server=serve;
951 client->exportsize=OFFT_MAX;
953 set_peername(net, client);
954 if (!authorized_client(client)) {
955 msg2(LOG_INFO,"Unauthorized client") ;
959 msg2(LOG_INFO,"Authorized client") ;
960 pid=g_malloc(sizeof(pid_t));
962 if ((*pid=fork())<0) {
963 msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
967 if (*pid>0) { /* parent */
969 g_hash_table_insert(children, pid, pid);
973 g_hash_table_destroy(children);
974 close(serve->socket) ;
976 msg2(LOG_INFO,"Starting to serve") ;
977 serveconnection(client);
982 * Main entry point...
984 int main(int argc, char *argv[]) {
988 if (sizeof( struct nbd_request )!=28) {
989 fprintf(stderr,"Bad size of structure. Alignment problems?\n");
994 serve=cmdline(argc, argv);
995 servers=g_array_new(TRUE, FALSE, sizeof(SERVER*));
997 if (!(serve->port)) {
1000 /* You really should define ISSERVER if you're going to use
1001 * inetd mode, but if you don't, closing stdout and stderr
1002 * (which inetd had connected to the client socket) will let it
1006 open("/dev/null", O_WRONLY);
1007 open("/dev/null", O_WRONLY);
1009 client=g_malloc(sizeof(CLIENT));
1010 client->server=serve;
1012 client->exportsize=OFFT_MAX;
1013 set_peername(0,client);
1014 serveconnection(client);