* 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <qemu-common.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <signal.h>
+#include <libgen.h>
#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
#define NBD_BUFFER_SIZE (1024*1024)
-int verbose;
+static int verbose;
static void usage(const char *name)
{
static void version(const char *name)
{
printf(
-"qemu-nbd version 0.0.1\n"
+"%s version 0.0.1\n"
"Written by Anthony Liguori.\n"
"\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
- );
+ , name);
}
struct partition_record
char *device = NULL;
char *socket = NULL;
char sockpath[128];
- const char *sopt = "hVbo:p:rsnP:c:dvk:e:t";
+ const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
struct option lopt[] = {
- { "help", 0, 0, 'h' },
- { "version", 0, 0, 'V' },
- { "bind", 1, 0, 'b' },
- { "port", 1, 0, 'p' },
- { "socket", 1, 0, 'k' },
- { "offset", 1, 0, 'o' },
- { "read-only", 0, 0, 'r' },
- { "partition", 1, 0, 'P' },
- { "connect", 1, 0, 'c' },
- { "disconnect", 0, 0, 'd' },
- { "snapshot", 0, 0, 's' },
- { "nocache", 0, 0, 'n' },
- { "shared", 1, 0, 'e' },
- { "persistent", 0, 0, 't' },
- { "verbose", 0, 0, 'v' },
- { NULL, 0, 0, 0 }
+ { "help", 0, NULL, 'h' },
+ { "version", 0, NULL, 'V' },
+ { "bind", 1, NULL, 'b' },
+ { "port", 1, NULL, 'p' },
+ { "socket", 1, NULL, 'k' },
+ { "offset", 1, NULL, 'o' },
+ { "read-only", 0, NULL, 'r' },
+ { "partition", 1, NULL, 'P' },
+ { "connect", 1, NULL, 'c' },
+ { "disconnect", 0, NULL, 'd' },
+ { "snapshot", 0, NULL, 's' },
+ { "nocache", 0, NULL, 'n' },
+ { "shared", 1, NULL, 'e' },
+ { "persistent", 0, NULL, 't' },
+ { "verbose", 0, NULL, 'v' },
+ { NULL, 0, NULL, 0 }
};
int ch;
int opt_ind = 0;
flags |= BDRV_O_SNAPSHOT;
break;
case 'n':
- flags |= BDRV_O_DIRECT;
+ flags |= BDRV_O_NOCACHE;
break;
case 'b':
bindto = optarg;
pid_t pid;
int sock;
- if (!verbose)
- daemon(0, 0); /* detach client and server */
+ if (!verbose) {
+ /* detach client and server */
+ if (daemon(0, 0) == -1) {
+ errx(errno, "Failed to daemonize");
+ }
+ }
if (socket == NULL) {
- sprintf(sockpath, SOCKET_PATH, basename(device));
+ snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
+ basename(device));
socket = sockpath;
}
}
sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
- if (sharing_fds == NULL)
- errx(ENOMEM, "Cannot allocate sharing fds");
if (socket) {
sharing_fds[0] = unix_socket_incoming(socket);
(struct sockaddr *)&addr,
&addr_len);
if (sharing_fds[nb_fds] != -1 &&
- nbd_negotiate(bs, sharing_fds[nb_fds], fd_size) != -1) {
+ nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
if (sharing_fds[nb_fds] > max_fd)
max_fd = sharing_fds[nb_fds];
nb_fds++;