static int pid_to_ns(int sock, pid_t tpid)
{
char v = '0';
- struct ucred cred;
+ struct ucred cred = {
+ .pid = -1,
+ .uid = -1,
+ .gid = -1,
+ };
while (recv_creds(sock, &cred, &v)) {
if (v == '1')
return 0;
- if (write(sock, &cred.pid, sizeof(pid_t)) != sizeof(pid_t))
+ if (write_nointr(sock, &cred.pid, sizeof(pid_t)) != sizeof(pid_t))
return 1;
}
bool recv_creds(int sock, struct ucred *cred, char *v)
{
- struct msghdr msg = { 0 };
+ struct msghdr msg = {};
struct iovec iov;
struct cmsghdr *cmsg;
- char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
- char buf[1];
- int ret;
+ ssize_t ret;
+ char cmsgbuf[CMSG_SPACE(sizeof(*cred))] = {};
+ char buf = '1';
int optval = 1;
- *v = '1';
-
- cred->pid = -1;
- cred->uid = -1;
- cred->gid = -1;
-
- if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1)
- return log_error(false, "Failed to set passcred: %s\n", strerror(errno));
-
- buf[0] = '1';
- if (write(sock, buf, 1) != 1)
- return log_error(false, "Failed to start write on scm fd: %s\n", strerror(errno));
-
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
- iov.iov_base = buf;
+ iov.iov_base = &buf;
iov.iov_len = sizeof(buf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
+ *v = buf;
+
+ ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
+ if (ret < 0)
+ return log_error(false, "Failed to set passcred: %s\n", strerror(errno));
+
+ ret = write_nointr(sock, &buf, sizeof(buf));
+ if (ret != sizeof(buf))
+ return log_error(false, "Failed to start write on scm fd: %s\n", strerror(errno));
+
if (!wait_for_sock(sock, 2))
return log_error(false, "Timed out waiting for scm_cred: %s\n", strerror(errno));
cmsg = CMSG_FIRSTHDR(&msg);
- if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) &&
- cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS) {
+ if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(*cred)) &&
+ cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_CREDENTIALS) {
memcpy(cred, CMSG_DATA(cmsg), sizeof(*cred));
}
- *v = buf[0];
+ *v = buf;
return true;
}