2016-01-21 00:55:40 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
nsjail - useful procedures
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2016-02-28 09:34:43 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-08-19 00:59:06 +08:00
|
|
|
#include <stdio.h>
|
2016-01-21 00:55:40 +08:00
|
|
|
#include <stdlib.h>
|
2016-08-19 00:59:06 +08:00
|
|
|
#include <string.h>
|
2016-02-28 09:34:43 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-01-21 00:55:40 +08:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
2016-02-28 09:34:43 +08:00
|
|
|
void *utilMalloc(size_t sz)
|
2016-01-21 00:55:40 +08:00
|
|
|
{
|
|
|
|
void *ret = malloc(sz);
|
|
|
|
if (ret == NULL) {
|
|
|
|
LOG_F("malloc(sz=%zu) failed", sz);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-02-28 09:34:43 +08:00
|
|
|
|
|
|
|
ssize_t utilReadFromFd(int fd, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t *charbuf = (uint8_t *) buf;
|
|
|
|
|
|
|
|
size_t readSz = 0;
|
|
|
|
while (readSz < len) {
|
|
|
|
ssize_t sz = read(fd, &charbuf[readSz], len - readSz);
|
|
|
|
if (sz < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sz <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
readSz += sz;
|
|
|
|
}
|
|
|
|
return readSz;
|
|
|
|
}
|
|
|
|
|
2016-05-08 09:09:43 +08:00
|
|
|
ssize_t utilReadFromFile(const char *fname, void *buf, size_t len)
|
|
|
|
{
|
2016-05-09 21:16:26 +08:00
|
|
|
int fd;
|
|
|
|
TEMP_FAILURE_RETRY(fd = open(fname, O_RDONLY));
|
2016-05-08 09:09:43 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
LOG_E("open('%s', O_RDONLY)", fname);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-29 21:38:22 +08:00
|
|
|
ssize_t ret = utilReadFromFd(fd, buf, len);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2016-05-08 09:09:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 09:34:43 +08:00
|
|
|
ssize_t utilWriteToFd(int fd, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
const uint8_t *charbuf = (const uint8_t *)buf;
|
|
|
|
|
|
|
|
size_t writtenSz = 0;
|
|
|
|
while (writtenSz < len) {
|
|
|
|
ssize_t sz = write(fd, &charbuf[writtenSz], len - writtenSz);
|
|
|
|
if (sz < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sz < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
writtenSz += sz;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:59:06 +08:00
|
|
|
bool utilWriteBufToFile(const char *filename, const void *buf, size_t len, int open_flags)
|
2016-02-28 09:34:43 +08:00
|
|
|
{
|
2016-05-09 21:16:26 +08:00
|
|
|
int fd;
|
|
|
|
TEMP_FAILURE_RETRY(fd = open(filename, open_flags, 0644));
|
2016-02-28 09:34:43 +08:00
|
|
|
if (fd == -1) {
|
2016-06-19 19:54:36 +08:00
|
|
|
PLOG_E("Couldn't open '%s' for writing", filename);
|
2016-02-28 09:34:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (utilWriteToFd(fd, buf, len) == false) {
|
|
|
|
PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
|
2016-07-29 21:38:22 +08:00
|
|
|
close(fd);
|
2016-02-28 09:34:43 +08:00
|
|
|
unlink(filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_D("Written '%zu' bytes to '%s'", len, filename);
|
|
|
|
|
2016-07-29 21:38:22 +08:00
|
|
|
close(fd);
|
2016-02-28 09:34:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-19 00:59:06 +08:00
|
|
|
|
|
|
|
bool utilCreateDirRecursively(const char *dir)
|
|
|
|
{
|
|
|
|
int prev_dir_fd = AT_FDCWD;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "%s", dir);
|
|
|
|
|
|
|
|
char *curr = path;
|
|
|
|
if (*curr == '/') {
|
|
|
|
prev_dir_fd = open("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (prev_dir_fd == -1) {
|
|
|
|
PLOG_E("open('/', O_RDONLY | O_CLOEXEC)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while (*curr == '/') {
|
|
|
|
curr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *next = strchr(curr, '/');
|
|
|
|
if (next == NULL) {
|
|
|
|
close(prev_dir_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*next = '\0';
|
|
|
|
|
|
|
|
if (mkdirat(prev_dir_fd, curr, 0755) == -1 && errno != EEXIST) {
|
|
|
|
PLOG_E("mkdir('%s', 0755)", curr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dir_fd = openat(prev_dir_fd, curr, O_DIRECTORY | O_CLOEXEC);
|
|
|
|
if (dir_fd == -1) {
|
|
|
|
PLOG_E("openat('%d', '%s', O_DIRECTORY | O_CLOEXEC)", prev_dir_fd, curr);
|
|
|
|
close(prev_dir_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(prev_dir_fd);
|
|
|
|
prev_dir_fd = dir_fd;
|
|
|
|
curr = next + 1;
|
|
|
|
}
|
|
|
|
}
|