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
549d0ad2
Unverified
Commit
549d0ad2
authored
Oct 30, 2018
by
Christian Brauner
Committed by
GitHub
Oct 30, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2709 from 2xsec/bugfix
storage_utils: add error handling
parents
f8f1069a
f6665080
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
42 deletions
+72
-42
parse.c
src/lxc/parse.c
+2
-3
storage_utils.c
src/lxc/storage/storage_utils.c
+70
-39
No files found.
src/lxc/parse.c
View file @
549d0ad2
...
@@ -101,7 +101,6 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
...
@@ -101,7 +101,6 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
return
-
1
;
return
-
1
;
}
}
/* sendfile() handles up to 2GB. No config file should be that big. */
/* sendfile() handles up to 2GB. No config file should be that big. */
bytes_sent
=
lxc_sendfile_nointr
(
memfd
,
fd
,
NULL
,
LXC_SENDFILE_MAX
);
bytes_sent
=
lxc_sendfile_nointr
(
memfd
,
fd
,
NULL
,
LXC_SENDFILE_MAX
);
if
(
bytes_sent
<
0
)
{
if
(
bytes_sent
<
0
)
{
...
@@ -170,7 +169,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
...
@@ -170,7 +169,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
f
=
fopen
(
file
,
"r"
);
f
=
fopen
(
file
,
"r"
);
if
(
!
f
)
{
if
(
!
f
)
{
SYSERROR
(
"
failed to open %s
"
,
file
);
SYSERROR
(
"
Failed to open
\"
%s
\"
"
,
file
);
return
-
1
;
return
-
1
;
}
}
...
@@ -181,7 +180,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
...
@@ -181,7 +180,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
* error.
* error.
*/
*/
if
(
err
<
0
)
if
(
err
<
0
)
ERROR
(
"Failed to parse config:
%s
"
,
line
);
ERROR
(
"Failed to parse config:
\"
%s
\"
"
,
line
);
break
;
break
;
}
}
}
}
...
...
src/lxc/storage/storage_utils.c
View file @
549d0ad2
...
@@ -77,6 +77,7 @@ char *dir_new_path(char *src, const char *oldname, const char *name,
...
@@ -77,6 +77,7 @@ char *dir_new_path(char *src, const char *oldname, const char *name,
p
+=
l1
;
p
+=
l1
;
nlen
+=
(
strlen
(
lxcpath
)
-
l1
);
nlen
+=
(
strlen
(
lxcpath
)
-
l1
);
}
}
l2
=
strlen
(
oldname
);
l2
=
strlen
(
oldname
);
while
((
p
=
strstr
(
p
,
oldname
))
!=
NULL
)
{
while
((
p
=
strstr
(
p
,
oldname
))
!=
NULL
)
{
p
+=
l2
;
p
+=
l2
;
...
@@ -105,14 +106,17 @@ char *dir_new_path(char *src, const char *oldname, const char *name,
...
@@ -105,14 +106,17 @@ char *dir_new_path(char *src, const char *oldname, const char *name,
/* move target pointer (p) */
/* move target pointer (p) */
p
+=
p2
-
src
;
p
+=
p2
-
src
;
/* print new name in place of oldname */
/* print new name in place of oldname */
p
+=
sprintf
(
p
,
"%s"
,
name
);
p
+=
sprintf
(
p
,
"%s"
,
name
);
/* move src to end of oldname */
/* move src to end of oldname */
src
=
p2
+
l2
;
src
=
p2
+
l2
;
}
}
/* copy the rest of src */
/* copy the rest of src */
sprintf
(
p
,
"%s"
,
src
);
sprintf
(
p
,
"%s"
,
src
);
return
ret
;
return
ret
;
}
}
...
@@ -153,12 +157,18 @@ int blk_getsize(struct lxc_storage *bdev, uint64_t *size)
...
@@ -153,12 +157,18 @@ int blk_getsize(struct lxc_storage *bdev, uint64_t *size)
const
char
*
src
;
const
char
*
src
;
src
=
lxc_storage_get_path
(
bdev
->
src
,
bdev
->
type
);
src
=
lxc_storage_get_path
(
bdev
->
src
,
bdev
->
type
);
fd
=
open
(
src
,
O_RDONLY
);
if
(
fd
<
0
)
fd
=
open
(
src
,
O_RDONLY
|
O_CLOEXEC
);
if
(
fd
<
0
)
{
SYSERROR
(
"Failed to open
\"
%s
\"
"
,
src
);
return
-
1
;
return
-
1
;
}
/* size of device in bytes */
/* size of device in bytes */
ret
=
ioctl
(
fd
,
BLKGETSIZE64
,
size
);
ret
=
ioctl
(
fd
,
BLKGETSIZE64
,
size
);
if
(
ret
<
0
)
SYSERROR
(
"Failed to get block size of dev-src"
);
close
(
fd
);
close
(
fd
);
return
ret
;
return
ret
;
}
}
...
@@ -195,80 +205,94 @@ int detect_fs(struct lxc_storage *bdev, char *type, int len)
...
@@ -195,80 +205,94 @@ int detect_fs(struct lxc_storage *bdev, char *type, int len)
srcdev
=
lxc_storage_get_path
(
bdev
->
src
,
bdev
->
type
);
srcdev
=
lxc_storage_get_path
(
bdev
->
src
,
bdev
->
type
);
ret
=
pipe
(
p
);
ret
=
pipe
(
p
);
if
(
ret
<
0
)
if
(
ret
<
0
)
{
SYSERROR
(
"Failed to create pipe"
);
return
-
1
;
return
-
1
;
}
if
((
pid
=
fork
())
<
0
)
pid
=
fork
();
if
(
pid
<
0
)
{
SYSERROR
(
"Failed to fork process"
);
return
-
1
;
return
-
1
;
}
if
(
pid
>
0
)
{
if
(
pid
>
0
)
{
int
status
;
int
status
;
close
(
p
[
1
]);
close
(
p
[
1
]);
memset
(
type
,
0
,
len
);
memset
(
type
,
0
,
len
);
ret
=
read
(
p
[
0
],
type
,
len
-
1
);
ret
=
read
(
p
[
0
],
type
,
len
-
1
);
close
(
p
[
0
]);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
SYSERROR
(
"error reading from pipe"
);
SYSERROR
(
"Failed to read FSType from pipe"
);
wait
(
&
status
);
return
-
1
;
}
else
if
(
ret
==
0
)
{
}
else
if
(
ret
==
0
)
{
ERROR
(
"child exited early - fstype not found"
);
ERROR
(
"FSType not found - child exited early"
);
wait
(
&
status
);
ret
=
-
1
;
return
-
1
;
}
}
close
(
p
[
0
]);
wait
(
&
status
);
wait
(
&
status
);
if
(
ret
<
0
)
return
ret
;
type
[
len
-
1
]
=
'\0'
;
type
[
len
-
1
]
=
'\0'
;
INFO
(
"detected fstype %s for %s"
,
type
,
srcdev
);
INFO
(
"Detected FSType
\"
%s
\"
for
\"
%s
\"
"
,
type
,
srcdev
);
return
ret
;
return
ret
;
}
}
if
(
unshare
(
CLONE_NEWNS
)
<
0
)
if
(
unshare
(
CLONE_NEWNS
)
<
0
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
if
(
detect_shared_rootfs
())
{
if
(
detect_shared_rootfs
())
if
(
mount
(
NULL
,
"/"
,
NULL
,
MS_SLAVE
|
MS_REC
,
NULL
))
{
if
(
mount
(
NULL
,
"/"
,
NULL
,
MS_SLAVE
|
MS_REC
,
NULL
))
{
SYSERROR
(
"Failed to make / rslave"
);
SYSERROR
(
"Failed to make / rslave"
);
ERROR
(
"Continuing..."
);
ERROR
(
"Continuing..."
);
}
}
}
ret
=
mount_unknown_fs
(
srcdev
,
bdev
->
dest
,
bdev
->
mntopts
);
ret
=
mount_unknown_fs
(
srcdev
,
bdev
->
dest
,
bdev
->
mntopts
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
ERROR
(
"
failed mounting %s onto %s to detect fst
ype"
,
srcdev
,
ERROR
(
"
Failed to mount
\"
%s
\"
onto
\"
%s
\"
to detect FST
ype"
,
srcdev
,
bdev
->
dest
);
bdev
->
dest
);
exit
(
1
);
_exit
(
EXIT_FAILURE
);
}
}
l
=
linkderef
(
srcdev
,
devpath
);
l
=
linkderef
(
srcdev
,
devpath
);
if
(
!
l
)
if
(
!
l
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
f
=
fopen
(
"/proc/self/mounts"
,
"r"
);
f
=
fopen
(
"/proc/self/mounts"
,
"r"
);
if
(
!
f
)
if
(
!
f
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
while
(
getline
(
&
line
,
&
linelen
,
f
)
!=
-
1
)
{
while
(
getline
(
&
line
,
&
linelen
,
f
)
!=
-
1
)
{
sp1
=
strchr
(
line
,
' '
);
sp1
=
strchr
(
line
,
' '
);
if
(
!
sp1
)
if
(
!
sp1
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
*
sp1
=
'\0'
;
*
sp1
=
'\0'
;
if
(
strcmp
(
line
,
l
))
if
(
strcmp
(
line
,
l
))
continue
;
continue
;
sp2
=
strchr
(
sp1
+
1
,
' '
);
sp2
=
strchr
(
sp1
+
1
,
' '
);
if
(
!
sp2
)
if
(
!
sp2
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
*
sp2
=
'\0'
;
*
sp2
=
'\0'
;
sp3
=
strchr
(
sp2
+
1
,
' '
);
sp3
=
strchr
(
sp2
+
1
,
' '
);
if
(
!
sp3
)
if
(
!
sp3
)
exit
(
1
);
_exit
(
EXIT_FAILURE
);
*
sp3
=
'\0'
;
*
sp3
=
'\0'
;
sp2
++
;
sp2
++
;
if
(
write
(
p
[
1
],
sp2
,
strlen
(
sp2
))
!=
strlen
(
sp2
))
if
(
write
(
p
[
1
],
sp2
,
strlen
(
sp2
))
!=
strlen
(
sp2
))
exit
(
1
);
_exit
(
EXIT_FAILURE
);
exit
(
0
);
_exit
(
EXIT_SUCCESS
);
}
}
exit
(
1
);
_exit
(
EXIT_FAILURE
);
}
}
int
do_mkfs_exec_wrapper
(
void
*
args
)
int
do_mkfs_exec_wrapper
(
void
*
args
)
...
@@ -294,10 +318,12 @@ int do_mkfs_exec_wrapper(void *args)
...
@@ -294,10 +318,12 @@ int do_mkfs_exec_wrapper(void *args)
return
-
1
;
return
-
1
;
}
}
TRACE
(
"
e
xecuting
\"
%s %s
\"
"
,
mkfs
,
data
[
1
]);
TRACE
(
"
E
xecuting
\"
%s %s
\"
"
,
mkfs
,
data
[
1
]);
execlp
(
mkfs
,
mkfs
,
data
[
1
],
(
char
*
)
NULL
);
execlp
(
mkfs
,
mkfs
,
data
[
1
],
(
char
*
)
NULL
);
SYSERROR
(
"failed to run
\"
%s %s
\"
"
,
mkfs
,
data
[
1
]);
SYSERROR
(
"Failed to run
\"
%s %s
\"
"
,
mkfs
,
data
[
1
]);
free
(
mkfs
);
free
(
mkfs
);
return
-
1
;
return
-
1
;
}
}
...
@@ -344,7 +370,7 @@ int mount_unknown_fs(const char *rootfs, const char *target,
...
@@ -344,7 +370,7 @@ int mount_unknown_fs(const char *rootfs, const char *target,
ret
=
lxc_file_for_each_line
(
fsfile
[
i
],
find_fstype_cb
,
&
cbarg
);
ret
=
lxc_file_for_each_line
(
fsfile
[
i
],
find_fstype_cb
,
&
cbarg
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
ERROR
(
"
failed to parse '%s'
"
,
fsfile
[
i
]);
ERROR
(
"
Failed to parse
\"
%s
\"
"
,
fsfile
[
i
]);
return
-
1
;
return
-
1
;
}
}
...
@@ -352,7 +378,8 @@ int mount_unknown_fs(const char *rootfs, const char *target,
...
@@ -352,7 +378,8 @@ int mount_unknown_fs(const char *rootfs, const char *target,
return
0
;
return
0
;
}
}
ERROR
(
"failed to determine fs type for '%s'"
,
rootfs
);
ERROR
(
"Failed to determine FSType for
\"
%s
\"
"
,
rootfs
);
return
-
1
;
return
-
1
;
}
}
...
@@ -381,7 +408,7 @@ int find_fstype_cb(char *buffer, void *data)
...
@@ -381,7 +408,7 @@ int find_fstype_cb(char *buffer, void *data)
fstype
+=
lxc_char_left_gc
(
fstype
,
strlen
(
fstype
));
fstype
+=
lxc_char_left_gc
(
fstype
,
strlen
(
fstype
));
fstype
[
lxc_char_right_gc
(
fstype
,
strlen
(
fstype
))]
=
'\0'
;
fstype
[
lxc_char_right_gc
(
fstype
,
strlen
(
fstype
))]
=
'\0'
;
DEBUG
(
"
trying to mount '%s'->'%s' with fstype '%s'
"
,
cbarg
->
rootfs
,
DEBUG
(
"
Trying to mount
\"
%s
\"
->
\"
%s
\"
with FSType
\"
%s
\"
"
,
cbarg
->
rootfs
,
cbarg
->
target
,
fstype
);
cbarg
->
target
,
fstype
);
if
(
parse_mntopts
(
cbarg
->
options
,
&
mntflags
,
&
mntdata
)
<
0
)
{
if
(
parse_mntopts
(
cbarg
->
options
,
&
mntflags
,
&
mntdata
)
<
0
)
{
...
@@ -390,14 +417,14 @@ int find_fstype_cb(char *buffer, void *data)
...
@@ -390,14 +417,14 @@ int find_fstype_cb(char *buffer, void *data)
}
}
if
(
mount
(
cbarg
->
rootfs
,
cbarg
->
target
,
fstype
,
mntflags
,
mntdata
))
{
if
(
mount
(
cbarg
->
rootfs
,
cbarg
->
target
,
fstype
,
mntflags
,
mntdata
))
{
SYSDEBUG
(
"
mount failed with error
"
);
SYSDEBUG
(
"
Failed to mount
"
);
free
(
mntdata
);
free
(
mntdata
);
return
0
;
return
0
;
}
}
free
(
mntdata
);
free
(
mntdata
);
INFO
(
"
mounted '%s' on '%s', with fstype '%s'
"
,
cbarg
->
rootfs
,
INFO
(
"
Mounted
\"
%s
\"
on
\"
%s
\"
, with FSType
\"
%s
\"
"
,
cbarg
->
rootfs
,
cbarg
->
target
,
fstype
);
cbarg
->
target
,
fstype
);
return
1
;
return
1
;
...
@@ -409,18 +436,20 @@ const char *linkderef(const char *path, char *dest)
...
@@ -409,18 +436,20 @@ const char *linkderef(const char *path, char *dest)
ssize_t
ret
;
ssize_t
ret
;
ret
=
stat
(
path
,
&
sbuf
);
ret
=
stat
(
path
,
&
sbuf
);
if
(
ret
<
0
)
if
(
ret
<
0
)
{
SYSERROR
(
"Failed to get status of file -
\"
%s
\"
"
,
path
);
return
NULL
;
return
NULL
;
}
if
(
!
S_ISLNK
(
sbuf
.
st_mode
))
if
(
!
S_ISLNK
(
sbuf
.
st_mode
))
return
path
;
return
path
;
ret
=
readlink
(
path
,
dest
,
PATH_MAX
);
ret
=
readlink
(
path
,
dest
,
PATH_MAX
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
SYSERROR
(
"
error reading link %s
"
,
path
);
SYSERROR
(
"
Failed to read link of
\"
%s
\"
"
,
path
);
return
NULL
;
return
NULL
;
}
else
if
(
ret
>=
PATH_MAX
)
{
}
else
if
(
ret
>=
PATH_MAX
)
{
ERROR
(
"
link in %
s too long"
,
path
);
ERROR
(
"
The name of link of
\"
%s
\"
i
s too long"
,
path
);
return
NULL
;
return
NULL
;
}
}
dest
[
ret
]
=
'\0'
;
dest
[
ret
]
=
'\0'
;
...
@@ -517,20 +546,22 @@ int storage_destroy_wrapper(void *data)
...
@@ -517,20 +546,22 @@ int storage_destroy_wrapper(void *data)
struct
lxc_conf
*
conf
=
data
;
struct
lxc_conf
*
conf
=
data
;
if
(
setgid
(
0
)
<
0
)
{
if
(
setgid
(
0
)
<
0
)
{
ERROR
(
"Failed to setgid to 0"
);
SYS
ERROR
(
"Failed to setgid to 0"
);
return
-
1
;
return
-
1
;
}
}
if
(
setgroups
(
0
,
NULL
)
<
0
)
if
(
setgroups
(
0
,
NULL
)
<
0
)
WARN
(
"Failed to clear groups"
);
SYS
WARN
(
"Failed to clear groups"
);
if
(
setuid
(
0
)
<
0
)
{
if
(
setuid
(
0
)
<
0
)
{
ERROR
(
"Failed to setuid to 0"
);
SYS
ERROR
(
"Failed to setuid to 0"
);
return
-
1
;
return
-
1
;
}
}
if
(
!
storage_destroy
(
conf
))
if
(
!
storage_destroy
(
conf
))
{
ERROR
(
"Failed to destroy storage"
);
return
-
1
;
return
-
1
;
}
return
0
;
return
0
;
}
}
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