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
83758a55
Unverified
Commit
83758a55
authored
Feb 11, 2021
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utils: convert to strnprintf()
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
3fcae2e4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
42 deletions
+41
-42
utils.c
src/lxc/utils.c
+41
-42
No files found.
src/lxc/utils.c
View file @
83758a55
...
@@ -84,8 +84,8 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
...
@@ -84,8 +84,8 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
!
strcmp
(
direntp
->
d_name
,
".."
))
!
strcmp
(
direntp
->
d_name
,
".."
))
continue
;
continue
;
rc
=
s
nprintf
(
pathname
,
PATH_MAX
,
"%s/%s"
,
dirname
,
direntp
->
d_name
);
rc
=
s
trnprintf
(
pathname
,
sizeof
(
pathname
)
,
"%s/%s"
,
dirname
,
direntp
->
d_name
);
if
(
rc
<
0
||
rc
>=
PATH_MAX
)
{
if
(
rc
<
0
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
failed
=
1
;
failed
=
1
;
continue
;
continue
;
...
@@ -267,8 +267,8 @@ char *get_rundir(void)
...
@@ -267,8 +267,8 @@ char *get_rundir(void)
if
(
!
rundir
)
if
(
!
rundir
)
return
NULL
;
return
NULL
;
ret
=
snprintf
(
rundir
,
len
,
"%s/.cache/lxc/run/"
,
homedir
);
ret
=
s
tr
nprintf
(
rundir
,
len
,
"%s/.cache/lxc/run/"
,
homedir
);
if
(
ret
<
0
||
(
size_t
)
ret
>=
len
)
if
(
ret
<
0
)
return
ret_set_errno
(
NULL
,
EIO
);
return
ret_set_errno
(
NULL
,
EIO
);
return
move_ptr
(
rundir
);
return
move_ptr
(
rundir
);
...
@@ -660,8 +660,8 @@ bool switch_to_ns(pid_t pid, const char *ns)
...
@@ -660,8 +660,8 @@ bool switch_to_ns(pid_t pid, const char *ns)
+
LXC_NAMESPACE_NAME_MAX
];
+
LXC_NAMESPACE_NAME_MAX
];
/* Switch to new ns */
/* Switch to new ns */
ret
=
snprintf
(
nspath
,
sizeof
(
nspath
),
"/proc/%d/ns/%s"
,
pid
,
ns
);
ret
=
s
tr
nprintf
(
nspath
,
sizeof
(
nspath
),
"/proc/%d/ns/%s"
,
pid
,
ns
);
if
(
ret
<
0
||
ret
>=
sizeof
(
nspath
)
)
if
(
ret
<
0
)
return
false
;
return
false
;
fd
=
open
(
nspath
,
O_RDONLY
|
O_CLOEXEC
);
fd
=
open
(
nspath
,
O_RDONLY
|
O_CLOEXEC
);
...
@@ -734,11 +734,10 @@ char *on_path(const char *cmd, const char *rootfs)
...
@@ -734,11 +734,10 @@ char *on_path(const char *cmd, const char *rootfs)
lxc_iterate_parts
(
entry
,
path
,
":"
)
{
lxc_iterate_parts
(
entry
,
path
,
":"
)
{
if
(
rootfs
)
if
(
rootfs
)
ret
=
snprintf
(
cmdpath
,
PATH_MAX
,
"%s/%s/%s"
,
rootfs
,
ret
=
strnprintf
(
cmdpath
,
sizeof
(
cmdpath
),
"%s/%s/%s"
,
rootfs
,
entry
,
cmd
);
entry
,
cmd
);
else
else
ret
=
s
nprintf
(
cmdpath
,
PATH_MAX
,
"%s/%s"
,
entry
,
cmd
);
ret
=
s
trnprintf
(
cmdpath
,
sizeof
(
cmdpath
)
,
"%s/%s"
,
entry
,
cmd
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
if
(
ret
<
0
)
continue
;
continue
;
if
(
access
(
cmdpath
,
X_OK
)
==
0
)
if
(
access
(
cmdpath
,
X_OK
)
==
0
)
...
@@ -788,8 +787,8 @@ char *choose_init(const char *rootfs)
...
@@ -788,8 +787,8 @@ char *choose_init(const char *rootfs)
else
else
tmp
=
empty
;
tmp
=
empty
;
ret
=
snprintf
(
retv
,
PATH_MAX
,
"%s/%s/%s"
,
tmp
,
SBINDIR
,
"/init.lxc"
);
ret
=
s
tr
nprintf
(
retv
,
PATH_MAX
,
"%s/%s/%s"
,
tmp
,
SBINDIR
,
"/init.lxc"
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
{
if
(
ret
<
0
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
goto
out1
;
goto
out1
;
}
}
...
@@ -797,8 +796,8 @@ char *choose_init(const char *rootfs)
...
@@ -797,8 +796,8 @@ char *choose_init(const char *rootfs)
if
(
access
(
retv
,
X_OK
)
==
0
)
if
(
access
(
retv
,
X_OK
)
==
0
)
return
retv
;
return
retv
;
ret
=
snprintf
(
retv
,
PATH_MAX
,
"%s/%s/%s"
,
tmp
,
LXCINITDIR
,
"/lxc/lxc-init"
);
ret
=
s
tr
nprintf
(
retv
,
PATH_MAX
,
"%s/%s/%s"
,
tmp
,
LXCINITDIR
,
"/lxc/lxc-init"
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
{
if
(
ret
<
0
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
goto
out1
;
goto
out1
;
}
}
...
@@ -806,8 +805,8 @@ char *choose_init(const char *rootfs)
...
@@ -806,8 +805,8 @@ char *choose_init(const char *rootfs)
if
(
access
(
retv
,
X_OK
)
==
0
)
if
(
access
(
retv
,
X_OK
)
==
0
)
return
retv
;
return
retv
;
ret
=
snprintf
(
retv
,
PATH_MAX
,
"%s/usr/lib/lxc/lxc-init"
,
tmp
);
ret
=
s
tr
nprintf
(
retv
,
PATH_MAX
,
"%s/usr/lib/lxc/lxc-init"
,
tmp
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
{
if
(
ret
<
0
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
goto
out1
;
goto
out1
;
}
}
...
@@ -815,8 +814,8 @@ char *choose_init(const char *rootfs)
...
@@ -815,8 +814,8 @@ char *choose_init(const char *rootfs)
if
(
access
(
retv
,
X_OK
)
==
0
)
if
(
access
(
retv
,
X_OK
)
==
0
)
return
retv
;
return
retv
;
ret
=
snprintf
(
retv
,
PATH_MAX
,
"%s/sbin/lxc-init"
,
tmp
);
ret
=
s
tr
nprintf
(
retv
,
PATH_MAX
,
"%s/sbin/lxc-init"
,
tmp
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
{
if
(
ret
<
0
)
{
ERROR
(
"The name of path is too long"
);
ERROR
(
"The name of path is too long"
);
goto
out1
;
goto
out1
;
}
}
...
@@ -834,8 +833,8 @@ char *choose_init(const char *rootfs)
...
@@ -834,8 +833,8 @@ char *choose_init(const char *rootfs)
if
(
rootfs
)
if
(
rootfs
)
goto
out1
;
goto
out1
;
ret
=
snprintf
(
retv
,
PATH_MAX
,
"/init.lxc.static"
);
ret
=
s
tr
nprintf
(
retv
,
PATH_MAX
,
"/init.lxc.static"
);
if
(
ret
<
0
||
ret
>=
PATH_MAX
)
{
if
(
ret
<
0
)
{
WARN
(
"Nonsense - name /lxc.init.static too long"
);
WARN
(
"Nonsense - name /lxc.init.static too long"
);
goto
out1
;
goto
out1
;
}
}
...
@@ -874,8 +873,8 @@ char *get_template_path(const char *t)
...
@@ -874,8 +873,8 @@ char *get_template_path(const char *t)
if
(
!
tpath
)
if
(
!
tpath
)
return
NULL
;
return
NULL
;
ret
=
snprintf
(
tpath
,
len
,
"%s/lxc-%s"
,
LXCTEMPLATEDIR
,
t
);
ret
=
s
tr
nprintf
(
tpath
,
len
,
"%s/lxc-%s"
,
LXCTEMPLATEDIR
,
t
);
if
(
ret
<
0
||
ret
>=
len
)
{
if
(
ret
<
0
)
{
free
(
tpath
);
free
(
tpath
);
return
NULL
;
return
NULL
;
}
}
...
@@ -1094,8 +1093,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
...
@@ -1094,8 +1093,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
source_fd
=
openat2
(
beneath_fd
,
src
,
&
how
,
sizeof
(
how
));
source_fd
=
openat2
(
beneath_fd
,
src
,
&
how
,
sizeof
(
how
));
if
(
source_fd
<
0
)
if
(
source_fd
<
0
)
return
-
errno
;
return
-
errno
;
ret
=
snprintf
(
src_buf
,
sizeof
(
src_buf
),
"/proc/self/fd/%d"
,
source_fd
);
ret
=
s
tr
nprintf
(
src_buf
,
sizeof
(
src_buf
),
"/proc/self/fd/%d"
,
source_fd
);
if
(
ret
<
0
||
ret
>=
sizeof
(
src_buf
)
)
if
(
ret
<
0
)
return
-
EIO
;
return
-
EIO
;
}
else
{
}
else
{
src_buf
[
0
]
=
'\0'
;
src_buf
[
0
]
=
'\0'
;
...
@@ -1104,8 +1103,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
...
@@ -1104,8 +1103,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
target_fd
=
openat2
(
beneath_fd
,
dst
,
&
how
,
sizeof
(
how
));
target_fd
=
openat2
(
beneath_fd
,
dst
,
&
how
,
sizeof
(
how
));
if
(
target_fd
<
0
)
if
(
target_fd
<
0
)
return
log_error_errno
(
-
errno
,
errno
,
"Failed to open %d(%s)"
,
beneath_fd
,
dst
);
return
log_error_errno
(
-
errno
,
errno
,
"Failed to open %d(%s)"
,
beneath_fd
,
dst
);
ret
=
snprintf
(
tgt_buf
,
sizeof
(
tgt_buf
),
"/proc/self/fd/%d"
,
target_fd
);
ret
=
s
tr
nprintf
(
tgt_buf
,
sizeof
(
tgt_buf
),
"/proc/self/fd/%d"
,
target_fd
);
if
(
ret
<
0
||
ret
>=
sizeof
(
tgt_buf
)
)
if
(
ret
<
0
)
return
-
EIO
;
return
-
EIO
;
if
(
!
is_empty_string
(
src_buf
))
if
(
!
is_empty_string
(
src_buf
))
...
@@ -1163,8 +1162,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
...
@@ -1163,8 +1162,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
if
(
srcfd
<
0
)
if
(
srcfd
<
0
)
return
srcfd
;
return
srcfd
;
ret
=
snprintf
(
srcbuf
,
sizeof
(
srcbuf
),
"/proc/self/fd/%d"
,
srcfd
);
ret
=
s
tr
nprintf
(
srcbuf
,
sizeof
(
srcbuf
),
"/proc/self/fd/%d"
,
srcfd
);
if
(
ret
<
0
||
ret
>=
(
int
)
sizeof
(
srcbuf
)
)
{
if
(
ret
<
0
)
{
close
(
srcfd
);
close
(
srcfd
);
ERROR
(
"Out of memory"
);
ERROR
(
"Out of memory"
);
return
-
EINVAL
;
return
-
EINVAL
;
...
@@ -1183,8 +1182,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
...
@@ -1183,8 +1182,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
return
destfd
;
return
destfd
;
}
}
ret
=
snprintf
(
destbuf
,
sizeof
(
destbuf
),
"/proc/self/fd/%d"
,
destfd
);
ret
=
s
tr
nprintf
(
destbuf
,
sizeof
(
destbuf
),
"/proc/self/fd/%d"
,
destfd
);
if
(
ret
<
0
||
ret
>=
(
int
)
sizeof
(
destbuf
)
)
{
if
(
ret
<
0
)
{
if
(
srcfd
!=
-
1
)
if
(
srcfd
!=
-
1
)
close
(
srcfd
);
close
(
srcfd
);
...
@@ -1266,8 +1265,8 @@ bool task_blocks_signal(pid_t pid, int signal)
...
@@ -1266,8 +1265,8 @@ bool task_blocks_signal(pid_t pid, int signal)
size_t
n
=
0
;
size_t
n
=
0
;
bool
bret
=
false
;
bool
bret
=
false
;
ret
=
s
nprintf
(
status
,
__PROC_STATUS_LEN
,
"/proc/%d/status"
,
pid
);
ret
=
s
trnprintf
(
status
,
sizeof
(
status
)
,
"/proc/%d/status"
,
pid
);
if
(
ret
<
0
||
ret
>=
__PROC_STATUS_LEN
)
if
(
ret
<
0
)
return
bret
;
return
bret
;
f
=
fopen
(
status
,
"re"
);
f
=
fopen
(
status
,
"re"
);
...
@@ -1305,10 +1304,10 @@ int lxc_preserve_ns(const int pid, const char *ns)
...
@@ -1305,10 +1304,10 @@ int lxc_preserve_ns(const int pid, const char *ns)
* are supported by the kernel by passing in the NULL or the empty
* are supported by the kernel by passing in the NULL or the empty
* string.
* string.
*/
*/
ret
=
s
nprintf
(
path
,
__NS_PATH_LEN
,
"/proc/%d/ns%s%s"
,
pid
,
ret
=
s
trnprintf
(
path
,
sizeof
(
path
)
,
"/proc/%d/ns%s%s"
,
pid
,
!
ns
||
strcmp
(
ns
,
""
)
==
0
?
""
:
"/"
,
!
ns
||
strcmp
(
ns
,
""
)
==
0
?
""
:
"/"
,
!
ns
||
strcmp
(
ns
,
""
)
==
0
?
""
:
ns
);
!
ns
||
strcmp
(
ns
,
""
)
==
0
?
""
:
ns
);
if
(
ret
<
0
||
(
size_t
)
ret
>=
__NS_PATH_LEN
)
if
(
ret
<
0
)
return
ret_errno
(
EIO
);
return
ret_errno
(
EIO
);
return
open
(
path
,
O_RDONLY
|
O_CLOEXEC
);
return
open
(
path
,
O_RDONLY
|
O_CLOEXEC
);
...
@@ -1404,8 +1403,8 @@ static int lxc_get_unused_loop_dev_legacy(char *loop_name)
...
@@ -1404,8 +1403,8 @@ static int lxc_get_unused_loop_dev_legacy(char *loop_name)
}
}
}
}
ret
=
snprintf
(
loop_name
,
LO_NAME_SIZE
,
"/dev/%s"
,
dp
->
d_name
);
ret
=
s
tr
nprintf
(
loop_name
,
LO_NAME_SIZE
,
"/dev/%s"
,
dp
->
d_name
);
if
(
ret
<
0
||
ret
>=
LO_NAME_SIZE
)
{
if
(
ret
<
0
)
{
close
(
fd
);
close
(
fd
);
fd
=
-
1
;
fd
=
-
1
;
continue
;
continue
;
...
@@ -1439,15 +1438,15 @@ static int lxc_get_unused_loop_dev(char *name_loop)
...
@@ -1439,15 +1438,15 @@ static int lxc_get_unused_loop_dev(char *name_loop)
goto
on_error
;
goto
on_error
;
}
}
ret
=
snprintf
(
name_loop
,
LO_NAME_SIZE
,
"/dev/loop%d"
,
loop_nr
);
ret
=
s
tr
nprintf
(
name_loop
,
LO_NAME_SIZE
,
"/dev/loop%d"
,
loop_nr
);
if
(
ret
<
0
||
ret
>=
LO_NAME_SIZE
)
if
(
ret
<
0
)
goto
on_error
;
goto
on_error
;
fd_tmp
=
open
(
name_loop
,
O_RDWR
|
O_CLOEXEC
);
fd_tmp
=
open
(
name_loop
,
O_RDWR
|
O_CLOEXEC
);
if
(
fd_tmp
<
0
)
{
if
(
fd_tmp
<
0
)
{
/* on Android loop devices are moved under /dev/block, give it a shot */
/* on Android loop devices are moved under /dev/block, give it a shot */
ret
=
snprintf
(
name_loop
,
LO_NAME_SIZE
,
"/dev/block/loop%d"
,
loop_nr
);
ret
=
s
tr
nprintf
(
name_loop
,
LO_NAME_SIZE
,
"/dev/block/loop%d"
,
loop_nr
);
if
(
ret
<
0
||
ret
>=
LO_NAME_SIZE
)
if
(
ret
<
0
)
goto
on_error
;
goto
on_error
;
fd_tmp
=
open
(
name_loop
,
O_RDWR
|
O_CLOEXEC
);
fd_tmp
=
open
(
name_loop
,
O_RDWR
|
O_CLOEXEC
);
...
@@ -1633,8 +1632,8 @@ bool lxc_nic_exists(char *nic)
...
@@ -1633,8 +1632,8 @@ bool lxc_nic_exists(char *nic)
if
(
!
strcmp
(
nic
,
"none"
))
if
(
!
strcmp
(
nic
,
"none"
))
return
true
;
return
true
;
ret
=
s
nprintf
(
path
,
__LXC_SYS_CLASS_NET_LEN
,
"/sys/class/net/%s"
,
nic
);
ret
=
s
trnprintf
(
path
,
sizeof
(
path
)
,
"/sys/class/net/%s"
,
nic
);
if
(
ret
<
0
||
(
size_t
)
ret
>=
__LXC_SYS_CLASS_NET_LEN
)
if
(
ret
<
0
)
return
false
;
return
false
;
ret
=
stat
(
path
,
&
sb
);
ret
=
stat
(
path
,
&
sb
);
...
...
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