Blame libmultipath/file.c

Packit Service 0af388
/*
Packit Service 0af388
 * Copyright (c) 2005 Christophe Varoqui
Packit Service 0af388
 * Copyright (c) 2005 Benjamin Marzinski, Redhat
Packit Service 0af388
 */
Packit Service 0af388
#include <stdlib.h>
Packit Service 0af388
#include <sys/types.h>
Packit Service 0af388
#include <sys/stat.h>
Packit Service 0af388
#include <fcntl.h>
Packit Service 0af388
#include <errno.h>
Packit Service 0af388
#include <unistd.h>
Packit Service 0af388
#include <string.h>
Packit Service 0af388
#include <limits.h>
Packit Service 0af388
#include <stdio.h>
Packit Service 0af388
#include <signal.h>
Packit Service 0af388
Packit Service 0af388
#include "file.h"
Packit Service 0af388
#include "debug.h"
Packit Service 0af388
#include "uxsock.h"
Packit Service 0af388
Packit Service 0af388
Packit Service 0af388
/*
Packit Service 0af388
 * significant parts of this file were taken from iscsi-bindings.c of the
Packit Service 0af388
 * linux-iscsi project.
Packit Service 0af388
 * Copyright (C) 2002 Cisco Systems, Inc.
Packit Service 0af388
 *
Packit Service 0af388
 * This program is free software; you can redistribute it and/or modify
Packit Service 0af388
 * it under the terms of the GNU General Public License as published
Packit Service 0af388
 * by the Free Software Foundation; either version 2 of the License, or
Packit Service 0af388
 * (at your option) any later version.
Packit Service 0af388
 *
Packit Service 0af388
 * This program is distributed in the hope that it will be useful, but
Packit Service 0af388
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0af388
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit Service 0af388
 * General Public License for more details.
Packit Service 0af388
 *
Packit Service 0af388
 * See the file COPYING included with this distribution for more details.
Packit Service 0af388
 */
Packit Service 0af388
Packit Service 0af388
int
Packit Service 0af388
ensure_directories_exist(const char *str, mode_t dir_mode)
Packit Service 0af388
{
Packit Service 0af388
	char *pathname;
Packit Service 0af388
	char *end;
Packit Service 0af388
	int err;
Packit Service 0af388
Packit Service 0af388
	pathname = strdup(str);
Packit Service 0af388
	if (!pathname){
Packit Service 0af388
		condlog(0, "Cannot copy file pathname %s : %s",
Packit Service 0af388
			str, strerror(errno));
Packit Service 0af388
		return -1;
Packit Service 0af388
	}
Packit Service 0af388
	end = pathname;
Packit Service 0af388
	/* skip leading slashes */
Packit Service 0af388
	while (end && *end && (*end == '/'))
Packit Service 0af388
		end++;
Packit Service 0af388
Packit Service 0af388
	while ((end = strchr(end, '/'))) {
Packit Service 0af388
		/* if there is another slash, make the dir. */
Packit Service 0af388
		*end = '\0';
Packit Service 0af388
		err = mkdir(pathname, dir_mode);
Packit Service 0af388
		if (err && errno != EEXIST) {
Packit Service 0af388
			condlog(0, "Cannot make directory [%s] : %s",
Packit Service 0af388
				pathname, strerror(errno));
Packit Service 0af388
			free(pathname);
Packit Service 0af388
			return -1;
Packit Service 0af388
		}
Packit Service 0af388
		if (!err)
Packit Service 0af388
			condlog(3, "Created dir [%s]", pathname);
Packit Service 0af388
		*end = '/';
Packit Service 0af388
		end++;
Packit Service 0af388
	}
Packit Service 0af388
	free(pathname);
Packit Service 0af388
	return 0;
Packit Service 0af388
}
Packit Service 0af388
Packit Service 0af388
static void
Packit Service 0af388
sigalrm(__attribute__((unused)) int sig)
Packit Service 0af388
{
Packit Service 0af388
	/* do nothing */
Packit Service 0af388
}
Packit Service 0af388
Packit Service 0af388
static int
Packit Service 0af388
lock_file(int fd, const char *file_name)
Packit Service 0af388
{
Packit Service 0af388
	struct sigaction act, oldact;
Packit Service 0af388
	sigset_t set, oldset;
Packit Service 0af388
	struct flock lock;
Packit Service 0af388
	int err;
Packit Service 0af388
Packit Service 0af388
	memset(&lock, 0, sizeof(lock));
Packit Service 0af388
	lock.l_type = F_WRLCK;
Packit Service 0af388
	lock.l_whence = SEEK_SET;
Packit Service 0af388
Packit Service 0af388
	act.sa_handler = sigalrm;
Packit Service 0af388
	sigemptyset(&act.sa_mask);
Packit Service 0af388
	act.sa_flags = 0;
Packit Service 0af388
	sigemptyset(&set);
Packit Service 0af388
	sigaddset(&set, SIGALRM);
Packit Service 0af388
Packit Service 0af388
	sigaction(SIGALRM, &act, &oldact);
Packit Service 0af388
	pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
Packit Service 0af388
Packit Service 0af388
	alarm(FILE_TIMEOUT);
Packit Service 0af388
	err = fcntl(fd, F_SETLKW, &lock);
Packit Service 0af388
	alarm(0);
Packit Service 0af388
Packit Service 0af388
	if (err) {
Packit Service 0af388
		if (errno != EINTR)
Packit Service 0af388
			condlog(0, "Cannot lock %s : %s", file_name,
Packit Service 0af388
				strerror(errno));
Packit Service 0af388
		else
Packit Service 0af388
			condlog(0, "%s is locked. Giving up.", file_name);
Packit Service 0af388
	}
Packit Service 0af388
Packit Service 0af388
	pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Packit Service 0af388
	sigaction(SIGALRM, &oldact, NULL);
Packit Service 0af388
	return err;
Packit Service 0af388
}
Packit Service 0af388
Packit Service 0af388
int
Packit Service 0af388
open_file(const char *file, int *can_write, const char *header)
Packit Service 0af388
{
Packit Service 0af388
	int fd;
Packit Service 0af388
	struct stat s;
Packit Service 0af388
Packit Service 0af388
	if (ensure_directories_exist(file, 0700))
Packit Service 0af388
		return -1;
Packit Service 0af388
	*can_write = 1;
Packit Service 0af388
	fd = open(file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
Packit Service 0af388
	if (fd < 0) {
Packit Service 0af388
		if (errno == EROFS) {
Packit Service 0af388
			*can_write = 0;
Packit Service 0af388
			condlog(3, "Cannot open file [%s] read/write. "
Packit Service 0af388
				" trying readonly", file);
Packit Service 0af388
			fd = open(file, O_RDONLY);
Packit Service 0af388
			if (fd < 0) {
Packit Service 0af388
				condlog(0, "Cannot open file [%s] "
Packit Service 0af388
					"readonly : %s", file, strerror(errno));
Packit Service 0af388
				return -1;
Packit Service 0af388
			}
Packit Service 0af388
		}
Packit Service 0af388
		else {
Packit Service 0af388
			condlog(0, "Cannot open file [%s] : %s", file,
Packit Service 0af388
				strerror(errno));
Packit Service 0af388
			return -1;
Packit Service 0af388
		}
Packit Service 0af388
	}
Packit Service 0af388
	if (*can_write && lock_file(fd, file) < 0)
Packit Service 0af388
		goto fail;
Packit Service 0af388
Packit Service 0af388
	memset(&s, 0, sizeof(s));
Packit Service 0af388
	if (fstat(fd, &s) < 0){
Packit Service 0af388
		condlog(0, "Cannot stat file %s : %s", file, strerror(errno));
Packit Service 0af388
		goto fail;
Packit Service 0af388
	}
Packit Service 0af388
	if (s.st_size == 0) {
Packit Service 0af388
		if (*can_write == 0)
Packit Service 0af388
			goto fail;
Packit Service 0af388
		/* If file is empty, write the header */
Packit Service 0af388
		int len = strlen(header);
Packit Service 0af388
Packit Service 0af388
		if (write(fd, header, len) != len) {
Packit Service 0af388
			condlog(0,
Packit Service 0af388
				"Cannot write header to file %s : %s", file,
Packit Service 0af388
				strerror(errno));
Packit Service 0af388
			/* cleanup partially written header */
Packit Service 0af388
			if (ftruncate(fd, 0))
Packit Service 0af388
				condlog(0, "Cannot truncate header : %s",
Packit Service 0af388
					strerror(errno));
Packit Service 0af388
			goto fail;
Packit Service 0af388
		}
Packit Service 0af388
		fsync(fd);
Packit Service 0af388
		condlog(3, "Initialized new file [%s]", file);
Packit Service 0af388
	}
Packit Service 0af388
Packit Service 0af388
	return fd;
Packit Service 0af388
Packit Service 0af388
fail:
Packit Service 0af388
	close(fd);
Packit Service 0af388
	return -1;
Packit Service 0af388
}