--- /dev/null
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mount.h>
+
+#define DEFAULT_CGROUP "/sys/fs/cgroup"
+#define MAX_PATH_LENGTH 512
+
+static bool cgroup_is_exists(const char *cgroup_full_path)
+{
+ struct stat stat_buf;
+ return stat(cgroup_full_path, &stat_buf) == 0;
+}
+
+int cgroup_make_subdir(const char* parentdir, const char* new_name, const char* target_name)
+{
+ char buf[MAX_PATH_LENGTH];
+ char new_buf[MAX_PATH_LENGTH];
+ bool cgroup_exists;
+ bool cgroup_remount = false;
+ int ret = 0;
+
+ if (!parentdir || !new_name || !target_name)
+ return -1;
+
+ snprintf(buf, sizeof(buf), "%s/%s", parentdir, target_name);
+ snprintf(new_buf, sizeof(new_buf), "%s/%s", parentdir, new_name);
+
+ cgroup_exists = cgroup_is_exists(new_buf);
+ if (!cgroup_exists) {
+ if (!strncmp(parentdir, DEFAULT_CGROUP, sizeof(DEFAULT_CGROUP))) {
+ ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
+ MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, "mode=755");
+ if (ret < 0){
+ printf("rw remount failed\n");
+ return -1;
+ }
+ cgroup_remount = true;
+ }
+
+ ret = symlink(buf, new_buf);
+ if(ret < 0){
+ printf("symlink failed\n");
+ return -1;
+ }
+
+ if (cgroup_remount) {
+ ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
+ MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
+ if (ret < 0){
+ printf("ro remount failed\n");
+ return -1;
+ }
+ }
+ }
+ else
+ printf("the cgroup is already existed\n");
+
+ return 0;
+}
+
+int main()
+{
+ int ret;
+ ret = cgroup_make_subdir(DEFAULT_CGROUP, "memory", "resourced");
+ if (ret < 0)
+ printf("creation failed\n");
+ else
+ printf("creation success or already created\n");
+ return ret;
+}
+
mkdir -p %{buildroot}/%{_unitdir}
install -m 0755 nsjail %{buildroot}/%{_bindir}/
+install -m 0755 cgroup_remount %{buildroot}/%{_bindir}/
install -m 0755 nsjail.sh %{buildroot}/%{_bindir}/
install -m 0644 nsjail.service %{buildroot}/%{_unitdir}/
%install_service multi-user.target.wants nsjail.service
%manifest %{name}.manifest
%license LICENSE
%{_bindir}/nsjail*
+%{_bindir}/cgroup_remount*
%{_unitdir}/nsjail.service
%{_unitdir}/multi-user.target.wants/nsjail.service