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
9fc7a50f
Unverified
Commit
9fc7a50f
authored
Mar 02, 2019
by
Stéphane Graber
Committed by
GitHub
Mar 02, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2887 from brauner/2019-03-01/coding_style_updates
CODING_STYLE: update
parents
e096ae3c
af32408a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
0 deletions
+120
-0
CODING_STYLE.md
CODING_STYLE.md
+120
-0
No files found.
CODING_STYLE.md
View file @
9fc7a50f
...
@@ -662,3 +662,123 @@ int lxc_attach_run_command(void *payload)
...
@@ -662,3 +662,123 @@ int lxc_attach_run_command(void *payload)
return ret;
return ret;
}
}
```
```
## 24) Never use `fgets()`
LXC does not allow the use of `fgets()`. Use `getline()` or other methods
instead.
## 25) Never allocate memory on the stack
This specifically forbids any usage of `alloca()` in the codebase.
## 26) Use cleanup macros supported by `gcc` and `clang`
LXC has switched from manually cleaning up resources to using cleanup macros
supported by `gcc` and `clang`:
```
c
__attribute__
((__cleanup__(
<my-cleanup-function-wrapper>
)))
```
We do not allow manually cleanups anymore if there are appropriate macros.
Currently the following macros are supported:
```
c
/
* close file descriptor *
/
__
do_close_prot_errno
/
* free allocated memory *
/
__do_free __
attribute__((__cleanup__(__auto_free__)))
/
* close FILEs *
/
__do_fclose __
attribute__((__cleanup__(__auto_fclose__)))
/
* close DIRs *
/
__do_closedir __
attribute__((__cleanup__(__auto_closedir__)))
```
For example:
```
c
void remount_all_slave(void)
{
__do_free char
*
line = NULL;
__do_fclose FILE
*
f = NULL;
__do_close_prot_errno int memfd = -EBADF, mntinfo_fd = -EBADF;
int ret;
ssize_t copied;
size_t len = 0;
mntinfo_fd = open("/proc/self/mountinfo", O_RDONLY | O_CLOEXEC);
if (mntinfo_fd < 0) {
SYSERROR("Failed to open \"/proc/self/mountinfo\"");
return;
}
memfd = memfd_create(".lxc_mountinfo", MFD_CLOEXEC);
if (memfd < 0) {
char template[] = P_tmpdir "/.lxc_mountinfo_XXXXXX";
if (errno != ENOSYS) {
SYSERROR("Failed to create temporary in-memory file");
return;
}
memfd = lxc_make_tmpfile(template, true);
if (memfd < 0) {
WARN("Failed to create temporary file");
return;
}
}
again:
copied = lxc_sendfile_nointr(memfd, mntinfo_fd, NULL, LXC_SENDFILE_MAX);
if (copied < 0) {
if (errno == EINTR)
goto again;
SYSERROR("Failed to copy \"/proc/self/mountinfo\"");
return;
}
ret = lseek(memfd, 0, SEEK_SET);
if (ret < 0) {
SYSERROR("Failed to reset file descriptor offset");
return;
}
f = fdopen(memfd, "r");
if (!f) {
SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing");
return;
}
/*
* After a successful fdopen() memfd will be closed when calling
* fclose(f). Calling close(memfd) afterwards is undefined.
*/
move_fd(memfd);
while (getline(&line, &len, f) != -1) {
char *opts, *target;
target = get_field(line, 4);
if (!target)
continue;
opts = get_field(target, 2);
if (!opts)
continue;
null_endofword(opts);
if (!strstr(opts, "shared"))
continue;
null_endofword(target);
ret = mount(NULL, target, NULL, MS_SLAVE, NULL);
if (ret < 0) {
SYSERROR("Failed to make \"%s\" MS_SLAVE", target);
ERROR("Continuing...");
continue;
}
TRACE("Remounted \"%s\" as MS_SLAVE", target);
}
TRACE("Remounted all mount table entries as MS_SLAVE");
}
```
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