#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <errno.h>
-#include <limits.h>
-#include <sys/stat.h>
-
-#include <sched.h>
-#include <sys/mount.h>
-
-#include <tzplatform_config.h>
-
-#include <sys/types.h>
-#include <grp.h>
#include <string.h>
-#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
-#define PIDFILE_PATH ".systemd.pid"
-// For compatibility, Using hard-coded path
-#define LEGACY_CONTENTS_DIR "/opt/usr/media"
-#define LEGACY_APPS_DIR "/opt/usr/apps"
-
-#define LAZYMOUNT_LIB LIBDIR"/liblazymount.so.0"
#define CONTAINER_LIB LIBDIR"/security/pam_krate.so"
#define LOAD_SYMBOL(handle, sym, name) \
} \
} while (0);
-#define MOUNT_SIZE "10000k"
-#define MAX_GRP_BUF_SIZE (1024 * 4)
-#define GRP_NAME_SYSTEM_SHARE "system_share"
-
-static void *container_handle = NULL;
-
-static const char *systemd_arg[] = {
- "/usr/lib/systemd/systemd",
- "--user",
- NULL
-};
-
-static int stop_process(char *username)
-{
- int r;
-
- r = umount2(tzplatform_getenv(TZ_USER_CONTENT), MNT_DETACH);
- if (r < 0) {
- fprintf(stderr, "Warning : Failed to umount user content\n");
- }
-
- r = umount2(tzplatform_getenv(TZ_USER_APP), MNT_DETACH);
- if (r < 0) {
- fprintf(stderr, "Warning : Failed to umount application content\n");
- }
- return 0;
-}
-static int normal_user_postprocess(char *username)
+static int container_postprocess(char *username)
{
int r;
- r = mount(tzplatform_getenv(TZ_USER_CONTENT),
- LEGACY_CONTENTS_DIR, NULL, MS_BIND, NULL);
- if (r < 0) {
- fprintf(stderr, "user content bind mount failed - %d\n", errno);
- return r;
- }
-
- r = mount(tzplatform_getenv(TZ_USER_APP),
- LEGACY_APPS_DIR, NULL, MS_BIND, NULL);
- if (r < 0) {
- fprintf(stderr, "user app bind mount failed - %d\n", errno);
- return r;
- }
-
- return 0;
-}
+ static void *container_handle;
+ int (*handle_postprocess)(char *);
-static int container_open(void)
-{
- if (container_handle)
+ /* not support container */
+ if (access(CONTAINER_LIB, F_OK))
return 0;
container_handle = dlopen(CONTAINER_LIB, RTLD_LAZY);
fprintf(stderr, "container module dlopen error\n");
return -1;
}
- return 0;
-}
-
-static int container_preprocess(char *username)
-{
- int r;
- int (*handle_preprocess)(char *);
-
- r = container_open();
- if (r < 0)
- return r;
-
- LOAD_SYMBOL(container_handle, handle_preprocess, "container_preprocess");
-
- r = handle_preprocess(username);
- if (r < 0) {
- fprintf(stderr, "container module preprocess error\n");
- return r;
- }
-
- return 0;
-}
-
-static int container_postprocess(char *username)
-{
- int r;
- int (*handle_postprocess)(char *);
-
- /* not support container */
- if (access(CONTAINER_LIB, F_OK))
- return 0;
-
- r = container_open();
- if (r < 0)
- return r;
LOAD_SYMBOL(container_handle, handle_postprocess, "container_postprocess");
return 0;
}
-static int make_pid_file(int pid, char* user_id)
-{
- FILE *fp;
- char pidpath[PATH_MAX];
- int r = 0;
-
- snprintf(pidpath, PATH_MAX, "/run/user/%s/%s", user_id, PIDFILE_PATH);
-
- fp = fopen(pidpath, "w+");
- if (fp != NULL) {
- fprintf(fp, "%d", pid);
- fclose(fp);
- } else
- r = -1;
-
- return r;
-}
-
-static int change_smack_for_user_session()
-{
- FILE *fp;
- int r = 0;
-
- fp = fopen("/proc/self/attr/current", "w");
-
- if (fp == NULL) {
- r = -errno;
- return r;
- }
- r = fputs("User", fp);
- if (r == EOF) {
- fclose(fp);
- r = -errno;
- return r;
- }
- fclose(fp);
-
- return 0;
-}
-
-int run_child(int argc, const char *argv[], char* user_id)
-{
- pid_t pid;
- int r = 0;
- int i;
-
- if (!argv)
- return -EINVAL;
-
- pid = fork();
-
- if (pid < 0) {
- fprintf(stderr, "failed to fork");
- r = -errno;
- } else if (pid == 0) {
- r = change_smack_for_user_session();
- if (r != 0) {
- fprintf(stderr, "failed to change smack\n");
- return r;
- }
-
- for (i = 0; i < _NSIG; ++i)
- signal(i, SIG_DFL);
-
- r = execv(argv[0], (char **)argv);
- /* NOT REACH */
- } else{
- make_pid_file(pid, user_id);
- r = pid;
- }
-
- return r;
-}
-
int main(int argc, char *argv[])
{
- int r = 0;
- int support_container = 0;
char *operation;
char *username;
operation = argv[1];
username = argv[2];
- if (strcmp(operation, "stop") == 0)
- return stop_process(username);
- else if (strcmp(operation, "start") == 0)
- ; /* do main code */
- else if (strcmp(operation, "bind-user-contents") == 0)
- return normal_user_postprocess(username);
- else if (strcmp(operation, "container_postprocess") == 0)
+ if (strcmp(operation, "container_postprocess") == 0)
return container_postprocess(username);
else {
fprintf(stderr, "option is invalid(%s)\n", operation);
return -2;
}
- /* If container supports below funcs, below line should be enabled. */
- support_container = (access(CONTAINER_LIB, F_OK) == 0) ? 1 : 0;
- if (support_container) {
- r = container_preprocess(username);
- if (r < 0) {
- fprintf(stderr, "container preprocess failed\n");
- return r;
- }
- }
-
- r = run_child(ARRAY_SIZE(systemd_arg), systemd_arg, username);
- if (r < 0) {
- fprintf(stderr, "systemd user execution failed\n");
- return r;
- } else{
- fprintf(stderr, "success = pid = %d\n", r);
- }
-
return 0;
}
-
-