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
03faa621
Commit
03faa621
authored
Mar 29, 2016
by
Serge Hallyn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #926 from brauner/2016-03-27/mmap_file_to_str
add funs to mmap() files to \0-terminated strings
parents
bc41134f
25086a5f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
7 deletions
+38
-7
lxccontainer.c
src/lxc/lxccontainer.c
+2
-7
utils.c
src/lxc/utils.c
+28
-0
utils.h
src/lxc/utils.h
+8
-0
No files found.
src/lxc/lxccontainer.c
View file @
03faa621
...
@@ -2163,13 +2163,8 @@ static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc
...
@@ -2163,13 +2163,8 @@ static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc
}
}
if
(
fbuf
.
st_size
!=
0
)
{
if
(
fbuf
.
st_size
!=
0
)
{
/* write terminating \0-byte to file */
if
(
pwrite
(
fd
,
""
,
1
,
fbuf
.
st_size
)
<=
0
)
{
close
(
fd
);
goto
out
;
}
buf
=
mmap
(
NULL
,
fbuf
.
st_size
+
1
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
buf
=
lxc_strmmap
(
NULL
,
fbuf
.
st_size
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
if
(
buf
==
MAP_FAILED
)
{
if
(
buf
==
MAP_FAILED
)
{
SYSERROR
(
"Failed to create mapping %s"
,
path
);
SYSERROR
(
"Failed to create mapping %s"
,
path
);
close
(
fd
);
close
(
fd
);
...
@@ -2182,7 +2177,7 @@ static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc
...
@@ -2182,7 +2177,7 @@ static bool mod_rdep(struct lxc_container *c0, struct lxc_container *c, bool inc
bytes
+=
len
;
bytes
+=
len
;
}
}
munmap
(
buf
,
fbuf
.
st_size
+
1
);
lxc_strmunmap
(
buf
,
fbuf
.
st_size
);
if
(
ftruncate
(
fd
,
fbuf
.
st_size
-
bytes
)
<
0
)
{
if
(
ftruncate
(
fd
,
fbuf
.
st_size
-
bytes
)
<
0
)
{
SYSERROR
(
"Failed to truncate file %s"
,
path
);
SYSERROR
(
"Failed to truncate file %s"
,
path
);
close
(
fd
);
close
(
fd
);
...
...
src/lxc/utils.c
View file @
03faa621
...
@@ -1811,3 +1811,31 @@ int lxc_count_file_lines(const char *fn)
...
@@ -1811,3 +1811,31 @@ int lxc_count_file_lines(const char *fn)
fclose
(
f
);
fclose
(
f
);
return
n
;
return
n
;
}
}
void
*
lxc_strmmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd
,
off_t
offset
)
{
void
*
tmp
=
NULL
,
*
overlap
=
NULL
;
/* We establish an anonymous mapping that is one byte larger than the
* underlying file. The pages handed to us are zero filled. */
tmp
=
mmap
(
addr
,
length
+
1
,
PROT_READ
,
MAP_PRIVATE
|
MAP_ANONYMOUS
,
-
1
,
0
);
if
(
tmp
==
MAP_FAILED
)
goto
out
;
/* Now we establish a fixed-address mapping starting at the address we
* received from our anonymous mapping and replace all bytes excluding
* the additional \0-byte with the file. This allows us to use normal
* string-handling function. */
overlap
=
mmap
(
tmp
,
length
,
prot
,
MAP_FIXED
|
flags
,
fd
,
offset
);
if
(
overlap
==
MAP_FAILED
)
goto
out
;
out
:
return
overlap
;
}
int
lxc_strmunmap
(
void
*
addr
,
size_t
length
)
{
return
munmap
(
addr
,
length
+
1
);
}
src/lxc/utils.h
View file @
03faa621
...
@@ -252,6 +252,14 @@ extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
...
@@ -252,6 +252,14 @@ extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
extern
size_t
lxc_array_len
(
void
**
array
);
extern
size_t
lxc_array_len
(
void
**
array
);
extern
void
**
lxc_append_null_to_array
(
void
**
array
,
size_t
count
);
extern
void
**
lxc_append_null_to_array
(
void
**
array
,
size_t
count
);
/* mmap() wrapper. lxc_mmap() will take care to \0-terminate files so that
* normal string-handling functions can be used on the buffer. */
extern
void
*
lxc_strmmap
(
void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd
,
off_t
offset
);
/* munmap() wrapper. Use it to free memory mmap()ed with lxc_mmap(). */
extern
int
lxc_strmunmap
(
void
*
addr
,
size_t
length
);
//initialize rand with urandom
//initialize rand with urandom
extern
int
randseed
(
bool
);
extern
int
randseed
(
bool
);
...
...
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