]>
sourceware.org Git - lvm2.git/blob - libdm/libdm-file.c
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of the device-mapper userspace tools.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static int _create_dir_recursive(const char *dir
)
27 log_verbose("Creating directory \"%s\"", dir
);
28 /* Create parent directories */
29 orig
= s
= dm_strdup(dir
);
31 log_error("Failed to duplicate directory name.");
35 while ((s
= strchr(s
, '/')) != NULL
) {
38 rc
= mkdir(orig
, 0777);
39 if (rc
< 0 && errno
!= EEXIST
) {
41 log_sys_error("mkdir", orig
);
48 /* Create final directory */
49 rc
= mkdir(dir
, 0777);
50 if (rc
< 0 && errno
!= EEXIST
) {
52 log_sys_error("mkdir", orig
);
62 int dm_create_dir(const char *dir
)
69 if (stat(dir
, &info
) < 0)
70 return _create_dir_recursive(dir
);
72 if (S_ISDIR(info
.st_mode
))
75 log_error("Directory \"%s\" not found", dir
);
79 int dm_is_empty_dir(const char *dir
)
81 struct dirent
*dirent
;
84 if (!(d
= opendir(dir
))) {
85 log_sys_error("opendir", dir
);
89 while ((dirent
= readdir(d
)))
90 if (strcmp(dirent
->d_name
, ".") && strcmp(dirent
->d_name
, ".."))
94 log_sys_error("closedir", dir
);
96 return dirent
? 0 : 1;
99 int dm_fclose(FILE *stream
)
101 int prev_fail
= ferror(stream
);
102 int fclose_fail
= fclose(stream
);
104 /* If there was a previous failure, but fclose succeeded,
105 clear errno, since ferror does not set it, and its value
106 may be unrelated to the ferror-reported failure. */
107 if (prev_fail
&& !fclose_fail
)
110 return prev_fail
|| fclose_fail
? EOF
: 0;
113 int dm_create_lockfile(const char *lockfile
)
122 if((fd
= open(lockfile
, O_CREAT
| O_WRONLY
,
123 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
))) < 0) {
124 log_error("Cannot open lockfile [%s], error was [%s]",
125 lockfile
, strerror(errno
));
129 lock
.l_type
= F_WRLCK
;
131 lock
.l_whence
= SEEK_SET
;
134 if (fcntl(fd
, F_SETLK
, &lock
) < 0) {
141 log_error("Cannot lock lockfile [%s], error was [%s]",
142 lockfile
, strerror(errno
));
150 log_error("process is already running");
156 if (ftruncate(fd
, 0) < 0) {
157 log_error("Cannot truncate pidfile [%s], error was [%s]",
158 lockfile
, strerror(errno
));
160 goto fail_close_unlink
;
163 memset(buffer
, 0, sizeof(buffer
));
164 snprintf(buffer
, sizeof(buffer
)-1, "%u\n", getpid());
166 bufferlen
= strlen(buffer
);
167 write_out
= write(fd
, buffer
, bufferlen
);
169 if ((write_out
< 0) || (write_out
== 0 && errno
)) {
170 log_error("Cannot write pid to pidfile [%s], error was [%s]",
171 lockfile
, strerror(errno
));
173 goto fail_close_unlink
;
176 if ((write_out
== 0) || ((size_t)write_out
< bufferlen
)) {
177 log_error("Cannot write pid to pidfile [%s], shortwrite of"
178 "[%" PRIsize_t
"] bytes, expected [%" PRIsize_t
"]\n",
179 lockfile
, write_out
, bufferlen
);
181 goto fail_close_unlink
;
184 if ((value
= fcntl(fd
, F_GETFD
, 0)) < 0) {
185 log_error("Cannot get close-on-exec flag from pidfile [%s], "
186 "error was [%s]", lockfile
, strerror(errno
));
188 goto fail_close_unlink
;
191 if (fcntl(fd
, F_SETFD
, value
) < 0) {
192 log_error("Cannot set close-on-exec flag from pidfile [%s], "
193 "error was [%s]", lockfile
, strerror(errno
));
195 goto fail_close_unlink
;
201 if (unlink(lockfile
))
210 int dm_daemon_is_running(const char* lockfile
)
215 if((fd
= open(lockfile
, O_RDONLY
)) < 0)
218 lock
.l_type
= F_WRLCK
;
220 lock
.l_whence
= SEEK_SET
;
222 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
223 log_error("Cannot check lock status of lockfile [%s], error was [%s]",
224 lockfile
, strerror(errno
));
233 return (lock
.l_type
== F_UNLCK
) ? 0 : 1;
This page took 0.05124 seconds and 5 git commands to generate.