Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
9268a9fb
Unverified
Commit
9268a9fb
authored
Mar 11, 2020
by
Stéphane Graber
Committed by
GitHub
Mar 11, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3291 from brauner/2020-03-11/fixes
bugfixes
parents
133d9608
f1258455
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
199 deletions
+116
-199
file_utils.c
src/lxc/file_utils.c
+50
-76
utils.c
src/lxc/utils.c
+66
-123
No files found.
src/lxc/file_utils.c
View file @
9268a9fb
...
@@ -73,7 +73,7 @@ int lxc_write_openat(const char *dir, const char *filename, const void *buf,
...
@@ -73,7 +73,7 @@ int lxc_write_openat(const char *dir, const char *filename, const void *buf,
int
lxc_write_to_file
(
const
char
*
filename
,
const
void
*
buf
,
size_t
count
,
int
lxc_write_to_file
(
const
char
*
filename
,
const
void
*
buf
,
size_t
count
,
bool
add_newline
,
mode_t
mode
)
bool
add_newline
,
mode_t
mode
)
{
{
int
fd
,
saved_errno
;
__do_close_prot_errno
int
fd
=
-
EBADF
;
ssize_t
ret
;
ssize_t
ret
;
fd
=
open
(
filename
,
O_WRONLY
|
O_TRUNC
|
O_CREAT
|
O_CLOEXEC
,
mode
);
fd
=
open
(
filename
,
O_WRONLY
|
O_TRUNC
|
O_CREAT
|
O_CLOEXEC
,
mode
);
...
@@ -82,30 +82,23 @@ int lxc_write_to_file(const char *filename, const void *buf, size_t count,
...
@@ -82,30 +82,23 @@ int lxc_write_to_file(const char *filename, const void *buf, size_t count,
ret
=
lxc_write_nointr
(
fd
,
buf
,
count
);
ret
=
lxc_write_nointr
(
fd
,
buf
,
count
);
if
(
ret
<
0
)
if
(
ret
<
0
)
goto
out_error
;
return
-
1
;
if
((
size_t
)
ret
!=
count
)
if
((
size_t
)
ret
!=
count
)
goto
out_error
;
return
-
1
;
if
(
add_newline
)
{
if
(
add_newline
)
{
ret
=
lxc_write_nointr
(
fd
,
"
\n
"
,
1
);
ret
=
lxc_write_nointr
(
fd
,
"
\n
"
,
1
);
if
(
ret
!=
1
)
if
(
ret
!=
1
)
goto
out_error
;
return
-
1
;
}
}
close
(
fd
);
return
0
;
return
0
;
out_error:
saved_errno
=
errno
;
close
(
fd
);
errno
=
saved_errno
;
return
-
1
;
}
}
int
lxc_read_from_file
(
const
char
*
filename
,
void
*
buf
,
size_t
count
)
int
lxc_read_from_file
(
const
char
*
filename
,
void
*
buf
,
size_t
count
)
{
{
int
fd
=
-
1
,
saved_errno
;
__do_close_prot_errno
int
fd
=
-
EBADF
;
ssize_t
ret
;
ssize_t
ret
;
fd
=
open
(
filename
,
O_RDONLY
|
O_CLOEXEC
);
fd
=
open
(
filename
,
O_RDONLY
|
O_CLOEXEC
);
...
@@ -126,19 +119,16 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count)
...
@@ -126,19 +119,16 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count)
ret
=
lxc_read_nointr
(
fd
,
buf
,
count
);
ret
=
lxc_read_nointr
(
fd
,
buf
,
count
);
}
}
saved_errno
=
errno
;
close
(
fd
);
errno
=
saved_errno
;
return
ret
;
return
ret
;
}
}
ssize_t
lxc_write_nointr
(
int
fd
,
const
void
*
buf
,
size_t
count
)
ssize_t
lxc_write_nointr
(
int
fd
,
const
void
*
buf
,
size_t
count
)
{
{
ssize_t
ret
;
ssize_t
ret
;
again:
ret
=
write
(
fd
,
buf
,
count
);
do
{
if
(
ret
<
0
&&
errno
==
EINTR
)
ret
=
write
(
fd
,
buf
,
count
);
goto
again
;
}
while
(
ret
<
0
&&
errno
==
EINTR
)
;
return
ret
;
return
ret
;
}
}
...
@@ -146,10 +136,10 @@ again:
...
@@ -146,10 +136,10 @@ again:
ssize_t
lxc_send_nointr
(
int
sockfd
,
void
*
buf
,
size_t
len
,
int
flags
)
ssize_t
lxc_send_nointr
(
int
sockfd
,
void
*
buf
,
size_t
len
,
int
flags
)
{
{
ssize_t
ret
;
ssize_t
ret
;
again:
ret
=
send
(
sockfd
,
buf
,
len
,
flags
);
do
{
if
(
ret
<
0
&&
errno
==
EINTR
)
ret
=
send
(
sockfd
,
buf
,
len
,
flags
);
goto
again
;
}
while
(
ret
<
0
&&
errno
==
EINTR
)
;
return
ret
;
return
ret
;
}
}
...
@@ -157,10 +147,10 @@ again:
...
@@ -157,10 +147,10 @@ again:
ssize_t
lxc_read_nointr
(
int
fd
,
void
*
buf
,
size_t
count
)
ssize_t
lxc_read_nointr
(
int
fd
,
void
*
buf
,
size_t
count
)
{
{
ssize_t
ret
;
ssize_t
ret
;
again:
ret
=
read
(
fd
,
buf
,
count
);
do
{
if
(
ret
<
0
&&
errno
==
EINTR
)
ret
=
read
(
fd
,
buf
,
count
);
goto
again
;
}
while
(
ret
<
0
&&
errno
==
EINTR
)
;
return
ret
;
return
ret
;
}
}
...
@@ -168,10 +158,10 @@ again:
...
@@ -168,10 +158,10 @@ again:
ssize_t
lxc_recv_nointr
(
int
sockfd
,
void
*
buf
,
size_t
len
,
int
flags
)
ssize_t
lxc_recv_nointr
(
int
sockfd
,
void
*
buf
,
size_t
len
,
int
flags
)
{
{
ssize_t
ret
;
ssize_t
ret
;
again:
ret
=
recv
(
sockfd
,
buf
,
len
,
flags
);
do
{
if
(
ret
<
0
&&
errno
==
EINTR
)
ret
=
recv
(
sockfd
,
buf
,
len
,
flags
);
goto
again
;
}
while
(
ret
<
0
&&
errno
==
EINTR
)
;
return
ret
;
return
ret
;
}
}
...
@@ -180,21 +170,20 @@ ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
...
@@ -180,21 +170,20 @@ ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
int
flags
)
int
flags
)
{
{
ssize_t
ret
;
ssize_t
ret
;
struct
msghdr
msg
;
struct
msghdr
msg
=
{
.
msg_iov
=
iov
,
.
msg_iovlen
=
iovlen
,
};
memset
(
&
msg
,
0
,
sizeof
(
msg
));
do
{
msg
.
msg_iov
=
iov
;
ret
=
recvmsg
(
sockfd
,
&
msg
,
flags
);
msg
.
msg_iovlen
=
iovlen
;
}
while
(
ret
<
0
&&
errno
==
EINTR
);
again:
ret
=
recvmsg
(
sockfd
,
&
msg
,
flags
);
if
(
ret
<
0
&&
errno
==
EINTR
)
goto
again
;
return
ret
;
return
ret
;
}
}
ssize_t
lxc_read_nointr_expect
(
int
fd
,
void
*
buf
,
size_t
count
,
const
void
*
expected_buf
)
ssize_t
lxc_read_nointr_expect
(
int
fd
,
void
*
buf
,
size_t
count
,
const
void
*
expected_buf
)
{
{
ssize_t
ret
;
ssize_t
ret
;
...
@@ -205,15 +194,14 @@ ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expe
...
@@ -205,15 +194,14 @@ ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expe
if
((
size_t
)
ret
!=
count
)
if
((
size_t
)
ret
!=
count
)
return
-
1
;
return
-
1
;
if
(
expected_buf
&&
memcmp
(
buf
,
expected_buf
,
count
)
!=
0
)
{
if
(
expected_buf
&&
memcmp
(
buf
,
expected_buf
,
count
)
!=
0
)
errno
=
EINVAL
;
return
ret_set_errno
(
-
1
,
EINVAL
);
return
-
1
;
}
return
0
;
return
0
;
}
}
ssize_t
lxc_read_file_expect
(
const
char
*
path
,
void
*
buf
,
size_t
count
,
const
void
*
expected_buf
)
ssize_t
lxc_read_file_expect
(
const
char
*
path
,
void
*
buf
,
size_t
count
,
const
void
*
expected_buf
)
{
{
__do_close_prot_errno
int
fd
=
-
EBADF
;
__do_close_prot_errno
int
fd
=
-
EBADF
;
...
@@ -233,7 +221,7 @@ bool file_exists(const char *f)
...
@@ -233,7 +221,7 @@ bool file_exists(const char *f)
int
print_to_file
(
const
char
*
file
,
const
char
*
content
)
int
print_to_file
(
const
char
*
file
,
const
char
*
content
)
{
{
FILE
*
f
;
__do_fclose
FILE
*
f
=
NULL
;
int
ret
=
0
;
int
ret
=
0
;
f
=
fopen
(
file
,
"we"
);
f
=
fopen
(
file
,
"we"
);
...
@@ -243,14 +231,13 @@ int print_to_file(const char *file, const char *content)
...
@@ -243,14 +231,13 @@ int print_to_file(const char *file, const char *content)
if
(
fprintf
(
f
,
"%s"
,
content
)
!=
strlen
(
content
))
if
(
fprintf
(
f
,
"%s"
,
content
)
!=
strlen
(
content
))
ret
=
-
1
;
ret
=
-
1
;
fclose
(
f
);
return
ret
;
return
ret
;
}
}
int
is_dir
(
const
char
*
path
)
int
is_dir
(
const
char
*
path
)
{
{
struct
stat
statbuf
;
int
ret
;
int
ret
;
struct
stat
statbuf
;
ret
=
stat
(
path
,
&
statbuf
);
ret
=
stat
(
path
,
&
statbuf
);
if
(
ret
==
0
&&
S_ISDIR
(
statbuf
.
st_mode
))
if
(
ret
==
0
&&
S_ISDIR
(
statbuf
.
st_mode
))
...
@@ -264,8 +251,8 @@ int is_dir(const char *path)
...
@@ -264,8 +251,8 @@ int is_dir(const char *path)
*/
*/
int
lxc_count_file_lines
(
const
char
*
fn
)
int
lxc_count_file_lines
(
const
char
*
fn
)
{
{
FILE
*
f
;
__do_free
char
*
line
=
NULL
;
char
*
line
=
NULL
;
__do_fclose
FILE
*
f
=
NULL
;
size_t
sz
=
0
;
size_t
sz
=
0
;
int
n
=
0
;
int
n
=
0
;
...
@@ -273,12 +260,9 @@ int lxc_count_file_lines(const char *fn)
...
@@ -273,12 +260,9 @@ int lxc_count_file_lines(const char *fn)
if
(
!
f
)
if
(
!
f
)
return
-
1
;
return
-
1
;
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
{
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
n
++
;
n
++
;
}
free
(
line
);
fclose
(
f
);
return
n
;
return
n
;
}
}
...
@@ -338,11 +322,9 @@ bool fhas_fs_type(int fd, fs_type_magic magic_val)
...
@@ -338,11 +322,9 @@ bool fhas_fs_type(int fd, fs_type_magic magic_val)
FILE
*
fopen_cloexec
(
const
char
*
path
,
const
char
*
mode
)
FILE
*
fopen_cloexec
(
const
char
*
path
,
const
char
*
mode
)
{
{
int
open_mode
=
0
;
__do_close_prot_errno
int
fd
=
-
EBADF
;
int
step
=
0
;
int
open_mode
=
0
,
step
=
0
;
int
fd
;
FILE
*
f
;
int
saved_errno
=
0
;
FILE
*
ret
;
if
(
!
strncmp
(
mode
,
"r+"
,
2
))
{
if
(
!
strncmp
(
mode
,
"r+"
,
2
))
{
open_mode
=
O_RDWR
;
open_mode
=
O_RDWR
;
...
@@ -366,32 +348,24 @@ FILE *fopen_cloexec(const char *path, const char *mode)
...
@@ -366,32 +348,24 @@ FILE *fopen_cloexec(const char *path, const char *mode)
for
(;
mode
[
step
];
step
++
)
for
(;
mode
[
step
];
step
++
)
if
(
mode
[
step
]
==
'x'
)
if
(
mode
[
step
]
==
'x'
)
open_mode
|=
O_EXCL
;
open_mode
|=
O_EXCL
;
open_mode
|=
O_CLOEXEC
;
fd
=
open
(
path
,
open_mode
,
0660
);
fd
=
open
(
path
,
open_mode
|
O_CLOEXEC
,
0660
);
if
(
fd
<
0
)
if
(
fd
<
0
)
return
NULL
;
return
NULL
;
ret
=
fdopen
(
fd
,
mode
);
f
=
fdopen
(
fd
,
mode
);
saved_errno
=
errno
;
if
(
f
)
if
(
!
ret
)
move_fd
(
fd
);
close
(
fd
);
return
f
;
errno
=
saved_errno
;
return
ret
;
}
}
ssize_t
lxc_sendfile_nointr
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
)
ssize_t
lxc_sendfile_nointr
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
)
{
{
ssize_t
ret
;
ssize_t
ret
;
again:
do
{
ret
=
sendfile
(
out_fd
,
in_fd
,
offset
,
count
);
ret
=
sendfile
(
out_fd
,
in_fd
,
offset
,
count
);
if
(
ret
<
0
)
{
}
while
(
ret
<
0
&&
errno
==
EINTR
);
if
(
errno
==
EINTR
)
goto
again
;
return
-
1
;
}
return
ret
;
return
ret
;
}
}
...
...
src/lxc/utils.c
View file @
9268a9fb
...
@@ -63,21 +63,20 @@ extern bool btrfs_try_remove_subvol(const char *path);
...
@@ -63,21 +63,20 @@ extern bool btrfs_try_remove_subvol(const char *path);
static
int
_recursive_rmdir
(
const
char
*
dirname
,
dev_t
pdev
,
static
int
_recursive_rmdir
(
const
char
*
dirname
,
dev_t
pdev
,
const
char
*
exclude
,
int
level
,
bool
onedev
)
const
char
*
exclude
,
int
level
,
bool
onedev
)
{
{
__do_closedir
DIR
*
dir
=
NULL
;
int
failed
=
0
;
bool
hadexclude
=
false
;
int
ret
;
struct
dirent
*
direntp
;
struct
dirent
*
direntp
;
DIR
*
dir
;
int
ret
,
failed
=
0
;
char
pathname
[
PATH_MAX
];
char
pathname
[
PATH_MAX
];
bool
hadexclude
=
false
;
dir
=
opendir
(
dirname
);
dir
=
opendir
(
dirname
);
if
(
!
dir
)
{
if
(
!
dir
)
ERROR
(
"Failed to open
\"
%s
\"
"
,
dirname
);
return
log_error
(
-
1
,
"Failed to open
\"
%s
\"
"
,
dirname
);
return
-
1
;
}
while
((
direntp
=
readdir
(
dir
)))
{
while
((
direntp
=
readdir
(
dir
)))
{
struct
stat
mystat
;
int
rc
;
int
rc
;
struct
stat
mystat
;
if
(
!
strcmp
(
direntp
->
d_name
,
"."
)
||
if
(
!
strcmp
(
direntp
->
d_name
,
"."
)
||
!
strcmp
(
direntp
->
d_name
,
".."
))
!
strcmp
(
direntp
->
d_name
,
".."
))
...
@@ -86,14 +85,14 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
...
@@ -86,14 +85,14 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
rc
=
snprintf
(
pathname
,
PATH_MAX
,
"%s/%s"
,
dirname
,
direntp
->
d_name
);
rc
=
snprintf
(
pathname
,
PATH_MAX
,
"%s/%s"
,
dirname
,
direntp
->
d_name
);
if
(
rc
<
0
||
rc
>=
PATH_MAX
)
{
if
(
rc
<
0
||
rc
>=
PATH_MAX
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
failed
=
1
;
failed
=
1
;
continue
;
continue
;
}
}
if
(
!
level
&&
exclude
&&
!
strcmp
(
direntp
->
d_name
,
exclude
))
{
if
(
!
level
&&
exclude
&&
!
strcmp
(
direntp
->
d_name
,
exclude
))
{
ret
=
rmdir
(
pathname
);
ret
=
rmdir
(
pathname
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
switch
(
errno
)
{
switch
(
errno
)
{
case
ENOTEMPTY
:
case
ENOTEMPTY
:
INFO
(
"Not deleting snapshot
\"
%s
\"
"
,
pathname
);
INFO
(
"Not deleting snapshot
\"
%s
\"
"
,
pathname
);
hadexclude
=
true
;
hadexclude
=
true
;
...
@@ -121,48 +120,38 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
...
@@ -121,48 +120,38 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
}
}
if
(
onedev
&&
mystat
.
st_dev
!=
pdev
)
{
if
(
onedev
&&
mystat
.
st_dev
!=
pdev
)
{
/* TODO should we be checking /proc/self/mountinfo for
* pathname and not doing this if found? */
if
(
btrfs_try_remove_subvol
(
pathname
))
if
(
btrfs_try_remove_subvol
(
pathname
))
INFO
(
"Removed btrfs subvolume at
\"
%s
\"
"
,
pathname
);
INFO
(
"Removed btrfs subvolume at
\"
%s
\"
"
,
pathname
);
continue
;
continue
;
}
}
if
(
S_ISDIR
(
mystat
.
st_mode
))
{
if
(
S_ISDIR
(
mystat
.
st_mode
))
{
if
(
_recursive_rmdir
(
pathname
,
pdev
,
exclude
,
level
+
1
,
onedev
)
<
0
)
if
(
_recursive_rmdir
(
pathname
,
pdev
,
exclude
,
level
+
1
,
onedev
)
<
0
)
failed
=
1
;
failed
=
1
;
}
else
{
}
else
{
if
(
unlink
(
pathname
)
<
0
)
{
if
(
unlink
(
pathname
)
<
0
)
{
SYSERROR
(
"Failed to delete
\"
%s
\"
"
,
pathname
);
SYSERROR
(
"Failed to delete
\"
%s
\"
"
,
pathname
);
failed
=
1
;
failed
=
1
;
}
}
}
}
}
}
if
(
rmdir
(
dirname
)
<
0
&&
!
btrfs_try_remove_subvol
(
dirname
)
&&
!
hadexclude
)
{
if
(
rmdir
(
dirname
)
<
0
&&
!
btrfs_try_remove_subvol
(
dirname
)
&&
!
hadexclude
)
{
SYSERROR
(
"Failed to delete
\"
%s
\"
"
,
dirname
);
SYSERROR
(
"Failed to delete
\"
%s
\"
"
,
dirname
);
failed
=
1
;
failed
=
1
;
}
ret
=
closedir
(
dir
);
if
(
ret
)
{
SYSERROR
(
"Failed to close directory
\"
%s
\"
"
,
dirname
);
failed
=
1
;
}
}
return
failed
?
-
1
:
0
;
return
failed
?
-
1
:
0
;
}
}
/* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
/*
* lxc_rmdir_onedev()
* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
* lxc_rmdir_onedev().
*/
*/
static
bool
is_native_overlayfs
(
const
char
*
path
)
static
inline
bool
is_native_overlayfs
(
const
char
*
path
)
{
{
if
(
has_fs_type
(
path
,
OVERLAY_SUPER_MAGIC
)
||
return
has_fs_type
(
path
,
OVERLAY_SUPER_MAGIC
)
||
has_fs_type
(
path
,
OVERLAYFS_SUPER_MAGIC
))
has_fs_type
(
path
,
OVERLAYFS_SUPER_MAGIC
);
return
true
;
return
false
;
}
}
/* returns 0 on success, -1 if there were any failures */
/* returns 0 on success, -1 if there were any failures */
...
@@ -178,8 +167,7 @@ extern int lxc_rmdir_onedev(const char *path, const char *exclude)
...
@@ -178,8 +167,7 @@ extern int lxc_rmdir_onedev(const char *path, const char *exclude)
if
(
errno
==
ENOENT
)
if
(
errno
==
ENOENT
)
return
0
;
return
0
;
SYSERROR
(
"Failed to stat
\"
%s
\"
"
,
path
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to stat
\"
%s
\"
"
,
path
);
return
-
1
;
}
}
return
_recursive_rmdir
(
path
,
mystat
.
st_dev
,
exclude
,
0
,
onedev
);
return
_recursive_rmdir
(
path
,
mystat
.
st_dev
,
exclude
,
0
,
onedev
);
...
@@ -210,25 +198,20 @@ int mkdir_p(const char *dir, mode_t mode)
...
@@ -210,25 +198,20 @@ int mkdir_p(const char *dir, mode_t mode)
const
char
*
orig
=
dir
;
const
char
*
orig
=
dir
;
do
{
do
{
__do_free
char
*
makeme
=
NULL
;
int
ret
;
int
ret
;
char
*
makeme
;
dir
=
tmp
+
strspn
(
tmp
,
"/"
);
dir
=
tmp
+
strspn
(
tmp
,
"/"
);
tmp
=
dir
+
strcspn
(
dir
,
"/"
);
tmp
=
dir
+
strcspn
(
dir
,
"/"
);
errno
=
ENOMEM
;
makeme
=
strndup
(
orig
,
dir
-
orig
);
makeme
=
strndup
(
orig
,
dir
-
orig
);
if
(
!
makeme
)
if
(
!
makeme
)
return
-
1
;
return
ret_set_errno
(
-
1
,
ENOMEM
)
;
ret
=
mkdir
(
makeme
,
mode
);
ret
=
mkdir
(
makeme
,
mode
);
if
(
ret
<
0
&&
errno
!=
EEXIST
)
{
if
(
ret
<
0
&&
errno
!=
EEXIST
)
SYSERROR
(
"Failed to create directory
\"
%s
\"
"
,
makeme
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to create directory
\"
%s
\"
"
,
makeme
);
free
(
makeme
);
return
-
1
;
}
free
(
makeme
);
}
while
(
tmp
!=
dir
);
}
while
(
tmp
!=
dir
);
return
0
;
return
0
;
...
@@ -237,36 +220,31 @@ int mkdir_p(const char *dir, mode_t mode)
...
@@ -237,36 +220,31 @@ int mkdir_p(const char *dir, mode_t mode)
char
*
get_rundir
()
char
*
get_rundir
()
{
{
char
*
rundir
;
char
*
rundir
;
size_t
len
;
const
char
*
homedir
;
const
char
*
homedir
;
struct
stat
sb
;
struct
stat
sb
;
if
(
stat
(
RUNTIME_PATH
,
&
sb
)
<
0
)
if
(
stat
(
RUNTIME_PATH
,
&
sb
)
<
0
)
return
NULL
;
return
NULL
;
if
(
geteuid
()
==
sb
.
st_uid
||
getegid
()
==
sb
.
st_gid
)
{
if
(
geteuid
()
==
sb
.
st_uid
||
getegid
()
==
sb
.
st_gid
)
rundir
=
strdup
(
RUNTIME_PATH
);
return
strdup
(
RUNTIME_PATH
);
return
rundir
;
}
rundir
=
getenv
(
"XDG_RUNTIME_DIR"
);
rundir
=
getenv
(
"XDG_RUNTIME_DIR"
);
if
(
rundir
)
{
if
(
rundir
)
rundir
=
strdup
(
rundir
);
return
strdup
(
rundir
);
return
rundir
;
}
INFO
(
"XDG_RUNTIME_DIR isn't set in the environment"
);
INFO
(
"XDG_RUNTIME_DIR isn't set in the environment"
);
homedir
=
getenv
(
"HOME"
);
homedir
=
getenv
(
"HOME"
);
if
(
!
homedir
)
{
if
(
!
homedir
)
ERROR
(
"HOME isn't set in the environment"
);
return
log_error
(
NULL
,
"HOME isn't set in the environment"
);
return
NULL
;
}
rundir
=
malloc
(
sizeof
(
char
)
*
(
17
+
strlen
(
homedir
)));
len
=
strlen
(
homedir
)
+
17
;
rundir
=
malloc
(
sizeof
(
char
)
*
len
);
if
(
!
rundir
)
if
(
!
rundir
)
return
NULL
;
return
NULL
;
sprintf
(
rundir
,
"%s/.cache/lxc/run/"
,
homedir
);
snprintf
(
rundir
,
len
,
"%s/.cache/lxc/run/"
,
homedir
);
return
rundir
;
return
rundir
;
}
}
...
@@ -328,16 +306,15 @@ again:
...
@@ -328,16 +306,15 @@ again:
#ifdef HAVE_OPENSSL
#ifdef HAVE_OPENSSL
#include <openssl/evp.h>
#include <openssl/evp.h>
static
int
do_sha1_hash
(
const
char
*
buf
,
int
buflen
,
unsigned
char
*
md_value
,
unsigned
int
*
md_len
)
static
int
do_sha1_hash
(
const
char
*
buf
,
int
buflen
,
unsigned
char
*
md_value
,
unsigned
int
*
md_len
)
{
{
EVP_MD_CTX
*
mdctx
;
EVP_MD_CTX
*
mdctx
;
const
EVP_MD
*
md
;
const
EVP_MD
*
md
;
md
=
EVP_get_digestbyname
(
"sha1"
);
md
=
EVP_get_digestbyname
(
"sha1"
);
if
(
!
md
)
{
if
(
!
md
)
printf
(
"Unknown message digest: sha1
\n
"
);
return
log_error
(
-
1
,
"Unknown message digest: sha1
\n
"
);
return
-
1
;
}
mdctx
=
EVP_MD_CTX_create
();
mdctx
=
EVP_MD_CTX_create
();
EVP_DigestInit_ex
(
mdctx
,
md
,
NULL
);
EVP_DigestInit_ex
(
mdctx
,
md
,
NULL
);
...
@@ -350,60 +327,37 @@ static int do_sha1_hash(const char *buf, int buflen, unsigned char *md_value, un
...
@@ -350,60 +327,37 @@ static int do_sha1_hash(const char *buf, int buflen, unsigned char *md_value, un
int
sha1sum_file
(
char
*
fnam
,
unsigned
char
*
digest
,
unsigned
int
*
md_len
)
int
sha1sum_file
(
char
*
fnam
,
unsigned
char
*
digest
,
unsigned
int
*
md_len
)
{
{
char
*
buf
;
__do_free
char
*
buf
=
NULL
;
__do_fclose
FILE
*
f
=
NULL
;
int
ret
;
int
ret
;
FILE
*
f
;
long
flen
;
long
flen
;
if
(
!
fnam
)
if
(
!
fnam
)
return
-
1
;
return
-
1
;
f
=
fopen_cloexec
(
fnam
,
"r"
);
f
=
fopen_cloexec
(
fnam
,
"r"
);
if
(
!
f
)
{
if
(
!
f
)
SYSERROR
(
"Failed to open template
\"
%s
\"
"
,
fnam
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to open template
\"
%s
\"
"
,
fnam
);
return
-
1
;
}
if
(
fseek
(
f
,
0
,
SEEK_END
)
<
0
)
{
if
(
fseek
(
f
,
0
,
SEEK_END
)
<
0
)
SYSERROR
(
"Failed to seek to end of template"
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to seek to end of template"
);
fclose
(
f
);
return
-
1
;
}
if
((
flen
=
ftell
(
f
))
<
0
)
{
flen
=
ftell
(
f
);
SYSERROR
(
"Failed to tell size of template"
);
if
(
flen
<
0
)
fclose
(
f
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to tell size of template"
);
return
-
1
;
}
if
(
fseek
(
f
,
0
,
SEEK_SET
)
<
0
)
{
if
(
fseek
(
f
,
0
,
SEEK_SET
)
<
0
)
SYSERROR
(
"Failed to seek to start of template"
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to seek to start of template"
);
fclose
(
f
);
return
-
1
;
}
if
((
buf
=
malloc
(
flen
+
1
))
==
NULL
)
{
buf
=
malloc
(
flen
+
1
);
SYSERROR
(
"Out of memory"
);
if
(
!
buf
)
fclose
(
f
);
return
log_error_errno
(
-
1
,
ENOMEM
,
"Out of memory"
);
return
-
1
;
}
if
(
fread
(
buf
,
1
,
flen
,
f
)
!=
flen
)
{
if
(
fread
(
buf
,
1
,
flen
,
f
)
!=
flen
)
SYSERROR
(
"Failed to read template"
);
return
log_error_errno
(
-
1
,
errno
,
"Failed to read template"
);
free
(
buf
);
fclose
(
f
);
return
-
1
;
}
if
(
fclose
(
f
)
<
0
)
{
SYSERROR
(
"Failed to close template"
);
free
(
buf
);
return
-
1
;
}
buf
[
flen
]
=
'\0'
;
buf
[
flen
]
=
'\0'
;
ret
=
do_sha1_hash
(
buf
,
flen
,
(
void
*
)
digest
,
md_len
);
ret
=
do_sha1_hash
(
buf
,
flen
,
(
void
*
)
digest
,
md_len
);
free
(
buf
);
return
ret
;
return
ret
;
}
}
#endif
#endif
...
@@ -556,10 +510,8 @@ uid_t get_ns_uid(uid_t orig)
...
@@ -556,10 +510,8 @@ uid_t get_ns_uid(uid_t orig)
uid_t
nsid
,
hostid
,
range
;
uid_t
nsid
,
hostid
,
range
;
f
=
fopen
(
"/proc/self/uid_map"
,
"re"
);
f
=
fopen
(
"/proc/self/uid_map"
,
"re"
);
if
(
!
f
)
{
if
(
!
f
)
SYSERROR
(
"Failed to open uid_map"
);
return
log_error_errno
(
0
,
errno
,
"Failed to open uid_map"
);
return
0
;
}
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
{
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
{
if
(
sscanf
(
line
,
"%u %u %u"
,
&
nsid
,
&
hostid
,
&
range
)
!=
3
)
if
(
sscanf
(
line
,
"%u %u %u"
,
&
nsid
,
&
hostid
,
&
range
)
!=
3
)
...
@@ -580,10 +532,8 @@ gid_t get_ns_gid(gid_t orig)
...
@@ -580,10 +532,8 @@ gid_t get_ns_gid(gid_t orig)
gid_t
nsid
,
hostid
,
range
;
gid_t
nsid
,
hostid
,
range
;
f
=
fopen
(
"/proc/self/gid_map"
,
"re"
);
f
=
fopen
(
"/proc/self/gid_map"
,
"re"
);
if
(
!
f
)
{
if
(
!
f
)
SYSERROR
(
"Failed to open gid_map"
);
return
log_error_errno
(
0
,
errno
,
"Failed to open gid_map"
);
return
0
;
}
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
{
while
(
getline
(
&
line
,
&
sz
,
f
)
!=
-
1
)
{
if
(
sscanf
(
line
,
"%u %u %u"
,
&
nsid
,
&
hostid
,
&
range
)
!=
3
)
if
(
sscanf
(
line
,
"%u %u %u"
,
&
nsid
,
&
hostid
,
&
range
)
!=
3
)
...
@@ -697,17 +647,12 @@ bool switch_to_ns(pid_t pid, const char *ns)
...
@@ -697,17 +647,12 @@ bool switch_to_ns(pid_t pid, const char *ns)
return
false
;
return
false
;
fd
=
open
(
nspath
,
O_RDONLY
|
O_CLOEXEC
);
fd
=
open
(
nspath
,
O_RDONLY
|
O_CLOEXEC
);
if
(
fd
<
0
)
{
if
(
fd
<
0
)
SYSERROR
(
"Failed to open
\"
%s
\"
"
,
nspath
);
return
log_error_errno
(
false
,
errno
,
"Failed to open
\"
%s
\"
"
,
nspath
);
return
false
;
}
ret
=
setns
(
fd
,
0
);
ret
=
setns
(
fd
,
0
);
if
(
ret
)
{
if
(
ret
)
SYSERROR
(
"Failed to set process %d to
\"
%s
\"
of %d."
,
pid
,
ns
,
return
log_error_errno
(
false
,
errno
,
"Failed to set process %d to
\"
%s
\"
of %d"
,
pid
,
ns
,
fd
);
fd
);
return
false
;
}
return
true
;
return
true
;
}
}
...
@@ -756,7 +701,8 @@ bool detect_ramfs_rootfs(void)
...
@@ -756,7 +701,8 @@ bool detect_ramfs_rootfs(void)
char
*
on_path
(
const
char
*
cmd
,
const
char
*
rootfs
)
char
*
on_path
(
const
char
*
cmd
,
const
char
*
rootfs
)
{
{
char
*
entry
=
NULL
,
*
path
=
NULL
;
__do_free
char
*
path
=
NULL
;
char
*
entry
=
NULL
;
char
cmdpath
[
PATH_MAX
];
char
cmdpath
[
PATH_MAX
];
int
ret
;
int
ret
;
...
@@ -768,7 +714,7 @@ char *on_path(const char *cmd, const char *rootfs)
...
@@ -768,7 +714,7 @@ char *on_path(const char *cmd, const char *rootfs)
if
(
!
path
)
if
(
!
path
)
return
NULL
;
return
NULL
;
lxc_iterate_parts
(
entry
,
path
,
":"
)
{
lxc_iterate_parts
(
entry
,
path
,
":"
)
{
if
(
rootfs
)
if
(
rootfs
)
ret
=
snprintf
(
cmdpath
,
PATH_MAX
,
"%s/%s/%s"
,
rootfs
,
ret
=
snprintf
(
cmdpath
,
PATH_MAX
,
"%s/%s/%s"
,
rootfs
,
entry
,
cmd
);
entry
,
cmd
);
...
@@ -777,13 +723,10 @@ char *on_path(const char *cmd, const char *rootfs)
...
@@ -777,13 +723,10 @@ char *on_path(const char *cmd, const char *rootfs)
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
continue
;
continue
;
if
(
access
(
cmdpath
,
X_OK
)
==
0
)
{
if
(
access
(
cmdpath
,
X_OK
)
==
0
)
free
(
path
);
return
strdup
(
cmdpath
);
return
strdup
(
cmdpath
);
}
}
}
free
(
path
);
return
NULL
;
return
NULL
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment