You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by al...@apache.org on 2017/12/22 18:46:59 UTC
[1/3] mesos git commit: Improved the comment around the union in
ns::clone().
Repository: mesos
Updated Branches:
refs/heads/master 0685b3ce3 -> 223aad37e
Improved the comment around the union in ns::clone().
Review: https://reviews.apache.org/r/64685/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/b44e3628
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b44e3628
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b44e3628
Branch: refs/heads/master
Commit: b44e36289320ac2cf9e8fa80bd7905bc44cd4a87
Parents: 0685b3c
Author: Alexander Rukletsov <ru...@gmail.com>
Authored: Fri Dec 22 19:41:47 2017 +0100
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Fri Dec 22 19:46:24 2017 +0100
----------------------------------------------------------------------
src/linux/ns.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/b44e3628/src/linux/ns.cpp
----------------------------------------------------------------------
diff --git a/src/linux/ns.cpp b/src/linux/ns.cpp
index 5e2df1e..8b59ba7 100644
--- a/src/linux/ns.cpp
+++ b/src/linux/ns.cpp
@@ -317,10 +317,9 @@ Try<pid_t> clone(
iov.iov_base = base;
iov.iov_len = sizeof(base);
- // Need to allocate a char array large enough to hold "control"
- // data. However, since this buffer is in reality a 'struct cmsghdr'
- // we use a union to ensure that it is aligned as required for that
- // structure.
+ // We need to allocate a char array large enough to hold "control" data. However,
+ // since this buffer is in reality a 'struct cmsghdr' with the payload, we use
+ // a union to ensure that it is aligned as required for that structure.
union {
struct cmsghdr cmessage;
char control[CMSG_SPACE(sizeof(struct ucred))];
[2/3] mesos git commit: Fixed the type-punned pointer and strict
aliasing issue.
Posted by al...@apache.org.
Fixed the type-punned pointer and strict aliasing issue.
Dereferencing a pointer cast from a different type of pointer
violates the so-called "strict aliasing" rule, which is undefined
behaviour and might lead to bugs when compiler optimizations are
enabled.
For more information on this topic, see
https://blog.regehr.org/archives/959
http://alas.matf.bg.ac.rs/manuals/lspe/snode=153.html
Review: https://reviews.apache.org/r/64686/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/5517db06
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/5517db06
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/5517db06
Branch: refs/heads/master
Commit: 5517db067054327aeb333a8cc2678da218a4cc93
Parents: b44e362
Author: Alexander Rukletsov <ru...@gmail.com>
Authored: Fri Dec 22 19:41:55 2017 +0100
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Fri Dec 22 19:46:25 2017 +0100
----------------------------------------------------------------------
src/linux/ns.cpp | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/5517db06/src/linux/ns.cpp
----------------------------------------------------------------------
diff --git a/src/linux/ns.cpp b/src/linux/ns.cpp
index 8b59ba7..be59d9c 100644
--- a/src/linux/ns.cpp
+++ b/src/linux/ns.cpp
@@ -18,8 +18,10 @@
#include <unistd.h>
+#include <sys/socket.h>
#include <sys/wait.h>
+#include <cstring>
#include <vector>
#include <process/collect.hpp>
@@ -394,7 +396,11 @@ Try<pid_t> clone(
return Error("Bad control data received");
}
- pid_t pid = ((struct ucred*) CMSG_DATA(CMSG_FIRSTHDR(&message)))->pid;
+ struct ucred cred;
+ std::memcpy(
+ &cred, CMSG_DATA(CMSG_FIRSTHDR(&message)), sizeof(struct ucred));
+
+ const pid_t pid = cred.pid;
// Need to `waitpid` on child process to avoid a zombie. Note that
// it's expected that the child will terminate quickly hence
@@ -452,17 +458,20 @@ Try<pid_t> clone(
stack.get(),
flags,
[=]() {
- struct ucred* cred = reinterpret_cast<struct ucred*>(
- CMSG_DATA(CMSG_FIRSTHDR(&message)));
+ struct ucred cred;
+ cred.pid = ::getpid();
+ cred.uid = ::getuid();
+ cred.gid = ::getgid();
// Now send back the pid and have it be translated appropriately
// by the kernel to the enclosing pid namespace.
//
// NOTE: sending back the pid is best effort because we're going
// to exit no matter what.
- cred->pid = ::getpid();
- cred->uid = ::getuid();
- cred->gid = ::getgid();
+ std::memcpy(
+ CMSG_DATA(CMSG_FIRSTHDR(&message)),
+ &cred,
+ sizeof(struct ucred));
if (sendmsg(sockets[1], &message, 0) == -1) {
// Failed to send the pid back to the parent!
[3/3] mesos git commit: Removed redundant `struct` prefix in
"linux/ns.cpp".
Posted by al...@apache.org.
Removed redundant `struct` prefix in "linux/ns.cpp".
Review: https://reviews.apache.org/r/64807/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/223aad37
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/223aad37
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/223aad37
Branch: refs/heads/master
Commit: 223aad37ee034283c82afa9b33a7db9d4959646c
Parents: 5517db0
Author: Alexander Rukletsov <ru...@gmail.com>
Authored: Fri Dec 22 19:42:01 2017 +0100
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Fri Dec 22 19:46:25 2017 +0100
----------------------------------------------------------------------
src/linux/ns.cpp | 27 ++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/223aad37/src/linux/ns.cpp
----------------------------------------------------------------------
diff --git a/src/linux/ns.cpp b/src/linux/ns.cpp
index be59d9c..4466407 100644
--- a/src/linux/ns.cpp
+++ b/src/linux/ns.cpp
@@ -319,15 +319,15 @@ Try<pid_t> clone(
iov.iov_base = base;
iov.iov_len = sizeof(base);
- // We need to allocate a char array large enough to hold "control" data. However,
- // since this buffer is in reality a 'struct cmsghdr' with the payload, we use
- // a union to ensure that it is aligned as required for that structure.
+ // We need to allocate a char array large enough to hold "control" data.
+ // However, since this buffer is in reality a 'cmsghdr' with the payload, we
+ // use a union to ensure that it is aligned as required for that structure.
union {
- struct cmsghdr cmessage;
- char control[CMSG_SPACE(sizeof(struct ucred))];
+ cmsghdr cmessage;
+ char control[CMSG_SPACE(sizeof(ucred))];
};
- cmessage.cmsg_len = CMSG_LEN(sizeof(struct ucred));
+ cmessage.cmsg_len = CMSG_LEN(sizeof(ucred));
cmessage.cmsg_level = SOL_SOCKET;
cmessage.cmsg_type = SCM_CREDENTIALS;
@@ -337,7 +337,7 @@ Try<pid_t> clone(
message.msg_iov = &iov;
message.msg_iovlen = 1;
message.msg_control = control;
- message.msg_controllen = sizeof(control); // CMSG_LEN(sizeof(struct ucred));
+ message.msg_controllen = sizeof(control); // CMSG_LEN(sizeof(ucred));
// Finally, the stack we'll use in the call to os::clone below (we
// allocate the stack here in order to keep the call to os::clone
@@ -389,16 +389,15 @@ Try<pid_t> clone(
// Extract pid.
if (CMSG_FIRSTHDR(&message) == nullptr ||
- CMSG_FIRSTHDR(&message)->cmsg_len != CMSG_LEN(sizeof(struct ucred)) ||
+ CMSG_FIRSTHDR(&message)->cmsg_len != CMSG_LEN(sizeof(ucred)) ||
CMSG_FIRSTHDR(&message)->cmsg_level != SOL_SOCKET ||
CMSG_FIRSTHDR(&message)->cmsg_type != SCM_CREDENTIALS) {
kill(child, SIGKILL);
return Error("Bad control data received");
}
- struct ucred cred;
- std::memcpy(
- &cred, CMSG_DATA(CMSG_FIRSTHDR(&message)), sizeof(struct ucred));
+ ucred cred;
+ std::memcpy(&cred, CMSG_DATA(CMSG_FIRSTHDR(&message)), sizeof(ucred));
const pid_t pid = cred.pid;
@@ -458,7 +457,7 @@ Try<pid_t> clone(
stack.get(),
flags,
[=]() {
- struct ucred cred;
+ ucred cred;
cred.pid = ::getpid();
cred.uid = ::getuid();
cred.gid = ::getgid();
@@ -469,9 +468,7 @@ Try<pid_t> clone(
// NOTE: sending back the pid is best effort because we're going
// to exit no matter what.
std::memcpy(
- CMSG_DATA(CMSG_FIRSTHDR(&message)),
- &cred,
- sizeof(struct ucred));
+ CMSG_DATA(CMSG_FIRSTHDR(&message)), &cred, sizeof(ucred));
if (sendmsg(sockets[1], &message, 0) == -1) {
// Failed to send the pid back to the parent!