LCOV - code coverage report
Current view: top level - arch/um/os-Linux - umid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 79 164 48.2 %
Date: 2023-03-27 20:00:47 Functions: 9 12 75.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
       4             :  */
       5             : 
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : #include <dirent.h>
       9             : #include <errno.h>
      10             : #include <fcntl.h>
      11             : #include <signal.h>
      12             : #include <string.h>
      13             : #include <unistd.h>
      14             : #include <sys/stat.h>
      15             : #include <init.h>
      16             : #include <os.h>
      17             : 
      18             : #define UML_DIR "~/.uml/"
      19             : 
      20             : #define UMID_LEN 64
      21             : 
      22             : /* Changed by set_umid, which is run early in boot */
      23             : static char umid[UMID_LEN] = { 0 };
      24             : 
      25             : /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
      26             : static char *uml_dir = UML_DIR;
      27             : 
      28           1 : static int __init make_uml_dir(void)
      29             : {
      30           1 :         char dir[512] = { '\0' };
      31             :         int len, err;
      32             : 
      33           1 :         if (*uml_dir == '~') {
      34           1 :                 char *home = getenv("HOME");
      35             : 
      36           1 :                 err = -ENOENT;
      37           1 :                 if (home == NULL) {
      38           0 :                         printk(UM_KERN_ERR
      39             :                                 "%s: no value in environment for $HOME\n",
      40             :                                 __func__);
      41           0 :                         goto err;
      42             :                 }
      43           1 :                 strlcpy(dir, home, sizeof(dir));
      44           1 :                 uml_dir++;
      45             :         }
      46           1 :         strlcat(dir, uml_dir, sizeof(dir));
      47           1 :         len = strlen(dir);
      48           1 :         if (len > 0 && dir[len - 1] != '/')
      49           0 :                 strlcat(dir, "/", sizeof(dir));
      50             : 
      51           1 :         err = -ENOMEM;
      52           1 :         uml_dir = malloc(strlen(dir) + 1);
      53           1 :         if (uml_dir == NULL) {
      54           0 :                 printk(UM_KERN_ERR "%s : malloc failed, errno = %d\n",
      55             :                         __func__, errno);
      56           0 :                 goto err;
      57             :         }
      58           1 :         strcpy(uml_dir, dir);
      59             : 
      60           1 :         if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
      61           0 :                 printk(UM_KERN_ERR "Failed to mkdir '%s': %s\n",
      62             :                         uml_dir, strerror(errno));
      63           0 :                 err = -errno;
      64             :                 goto err_free;
      65             :         }
      66             :         return 0;
      67             : 
      68             : err_free:
      69           0 :         free(uml_dir);
      70             : err:
      71           0 :         uml_dir = NULL;
      72           0 :         return err;
      73             : }
      74             : 
      75             : /*
      76             :  * Unlinks the files contained in @dir and then removes @dir.
      77             :  * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
      78             :  * ignore ENOENT errors for anything (they happen, strangely enough - possibly
      79             :  * due to races between multiple dying UML threads).
      80             :  */
      81           1 : static int remove_files_and_dir(char *dir)
      82             : {
      83             :         DIR *directory;
      84             :         struct dirent *ent;
      85             :         int len;
      86             :         char file[256];
      87             :         int ret;
      88             : 
      89           1 :         directory = opendir(dir);
      90           1 :         if (directory == NULL) {
      91           0 :                 if (errno != ENOENT)
      92           0 :                         return -errno;
      93             :                 else
      94             :                         return 0;
      95             :         }
      96             : 
      97           4 :         while ((ent = readdir(directory)) != NULL) {
      98           3 :                 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
      99           2 :                         continue;
     100           1 :                 len = strlen(dir) + strlen("/") + strlen(ent->d_name) + 1;
     101           1 :                 if (len > sizeof(file)) {
     102             :                         ret = -E2BIG;
     103             :                         goto out;
     104             :                 }
     105             : 
     106           1 :                 sprintf(file, "%s/%s", dir, ent->d_name);
     107           1 :                 if (unlink(file) < 0 && errno != ENOENT) {
     108           0 :                         ret = -errno;
     109           0 :                         goto out;
     110             :                 }
     111             :         }
     112             : 
     113           1 :         if (rmdir(dir) < 0 && errno != ENOENT) {
     114           0 :                 ret = -errno;
     115           0 :                 goto out;
     116             :         }
     117             : 
     118             :         ret = 0;
     119             : out:
     120           1 :         closedir(directory);
     121           1 :         return ret;
     122             : }
     123             : 
     124             : /*
     125             :  * This says that there isn't already a user of the specified directory even if
     126             :  * there are errors during the checking.  This is because if these errors
     127             :  * happen, the directory is unusable by the pre-existing UML, so we might as
     128             :  * well take it over.  This could happen either by
     129             :  *      the existing UML somehow corrupting its umid directory
     130             :  *      something other than UML sticking stuff in the directory
     131             :  *      this boot racing with a shutdown of the other UML
     132             :  * In any of these cases, the directory isn't useful for anything else.
     133             :  *
     134             :  * Boolean return: 1 if in use, 0 otherwise.
     135             :  */
     136           0 : static inline int is_umdir_used(char *dir)
     137             : {
     138             :         char pid[sizeof("nnnnnnnnn")], *end, *file;
     139             :         int fd, p, n, err;
     140           0 :         size_t filelen = strlen(dir) + sizeof("/pid") + 1;
     141             : 
     142           0 :         file = malloc(filelen);
     143           0 :         if (!file)
     144             :                 return -ENOMEM;
     145             : 
     146           0 :         snprintf(file, filelen, "%s/pid", dir);
     147             : 
     148           0 :         fd = open(file, O_RDONLY);
     149           0 :         if (fd < 0) {
     150           0 :                 fd = -errno;
     151           0 :                 if (fd != -ENOENT) {
     152           0 :                         printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
     153             :                                "file '%s', err = %d\n", file, -fd);
     154             :                 }
     155             :                 goto out;
     156             :         }
     157             : 
     158           0 :         err = 0;
     159           0 :         n = read(fd, pid, sizeof(pid));
     160           0 :         if (n < 0) {
     161           0 :                 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
     162             :                        "'%s', err = %d\n", file, errno);
     163           0 :                 goto out_close;
     164           0 :         } else if (n == 0) {
     165           0 :                 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
     166             :                        "'%s', 0-byte read\n", file);
     167           0 :                 goto out_close;
     168             :         }
     169             : 
     170           0 :         p = strtoul(pid, &end, 0);
     171           0 :         if (end == pid) {
     172           0 :                 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
     173             :                        "'%s', errno = %d\n", file, errno);
     174           0 :                 goto out_close;
     175             :         }
     176             : 
     177           0 :         if ((kill(p, 0) == 0) || (errno != ESRCH)) {
     178           0 :                 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
     179             :                        umid, p);
     180           0 :                 return 1;
     181             :         }
     182             : 
     183             : out_close:
     184           0 :         close(fd);
     185             : out:
     186           0 :         free(file);
     187           0 :         return 0;
     188             : }
     189             : 
     190             : /*
     191             :  * Try to remove the directory @dir unless it's in use.
     192             :  * Precondition: @dir exists.
     193             :  * Returns 0 for success, < 0 for failure in removal or if the directory is in
     194             :  * use.
     195             :  */
     196           0 : static int umdir_take_if_dead(char *dir)
     197             : {
     198             :         int ret;
     199           0 :         if (is_umdir_used(dir))
     200             :                 return -EEXIST;
     201             : 
     202           0 :         ret = remove_files_and_dir(dir);
     203           0 :         if (ret) {
     204           0 :                 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
     205             :                        "failed with err = %d\n", ret);
     206             :         }
     207             :         return ret;
     208             : }
     209             : 
     210           1 : static void __init create_pid_file(void)
     211             : {
     212             :         char pid[sizeof("nnnnnnnnn")], *file;
     213             :         int fd, n;
     214             : 
     215           1 :         n = strlen(uml_dir) + UMID_LEN + sizeof("/pid");
     216           1 :         file = malloc(n);
     217           1 :         if (!file)
     218           0 :                 return;
     219             : 
     220           1 :         if (umid_file_name("pid", file, n))
     221             :                 goto out;
     222             : 
     223           1 :         fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
     224           1 :         if (fd < 0) {
     225           0 :                 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
     226             :                        "%s\n", file, strerror(errno));
     227           0 :                 goto out;
     228             :         }
     229             : 
     230           1 :         snprintf(pid, sizeof(pid), "%d\n", getpid());
     231           1 :         n = write(fd, pid, strlen(pid));
     232           1 :         if (n != strlen(pid))
     233           0 :                 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
     234             :                        errno);
     235             : 
     236           1 :         close(fd);
     237             : out:
     238           1 :         free(file);
     239             : }
     240             : 
     241           1 : int __init set_umid(char *name)
     242             : {
     243           1 :         if (strlen(name) > UMID_LEN - 1)
     244             :                 return -E2BIG;
     245             : 
     246           1 :         strlcpy(umid, name, sizeof(umid));
     247             : 
     248           1 :         return 0;
     249             : }
     250             : 
     251             : /* Changed in make_umid, which is called during early boot */
     252             : static int umid_setup = 0;
     253             : 
     254           3 : static int __init make_umid(void)
     255             : {
     256             :         int fd, err;
     257             :         char tmp[256];
     258             : 
     259           3 :         if (umid_setup)
     260             :                 return 0;
     261             : 
     262           1 :         make_uml_dir();
     263             : 
     264           1 :         if (*umid == '\0') {
     265           1 :                 strlcpy(tmp, uml_dir, sizeof(tmp));
     266           1 :                 strlcat(tmp, "XXXXXX", sizeof(tmp));
     267           1 :                 fd = mkstemp(tmp);
     268           1 :                 if (fd < 0) {
     269           0 :                         printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
     270             :                                "%s\n", tmp, strerror(errno));
     271           0 :                         err = -errno;
     272           0 :                         goto err;
     273             :                 }
     274             : 
     275           1 :                 close(fd);
     276             : 
     277           1 :                 set_umid(&tmp[strlen(uml_dir)]);
     278             : 
     279             :                 /*
     280             :                  * There's a nice tiny little race between this unlink and
     281             :                  * the mkdir below.  It'd be nice if there were a mkstemp
     282             :                  * for directories.
     283             :                  */
     284           1 :                 if (unlink(tmp)) {
     285           0 :                         err = -errno;
     286           0 :                         goto err;
     287             :                 }
     288             :         }
     289             : 
     290           1 :         snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
     291           1 :         err = mkdir(tmp, 0777);
     292           1 :         if (err < 0) {
     293           0 :                 err = -errno;
     294           0 :                 if (err != -EEXIST)
     295             :                         goto err;
     296             : 
     297           0 :                 if (umdir_take_if_dead(tmp) < 0)
     298             :                         goto err;
     299             : 
     300           0 :                 err = mkdir(tmp, 0777);
     301             :         }
     302           1 :         if (err) {
     303           0 :                 err = -errno;
     304           0 :                 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
     305             :                        errno);
     306           0 :                 goto err;
     307             :         }
     308             : 
     309           1 :         umid_setup = 1;
     310             : 
     311           1 :         create_pid_file();
     312             : 
     313           1 :         err = 0;
     314             :  err:
     315             :         return err;
     316             : }
     317             : 
     318           1 : static int __init make_umid_init(void)
     319             : {
     320           1 :         if (!make_umid())
     321             :                 return 0;
     322             : 
     323             :         /*
     324             :          * If initializing with the given umid failed, then try again with
     325             :          * a random one.
     326             :          */
     327           0 :         printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
     328             :                "random umid\n", umid);
     329           0 :         *umid = '\0';
     330           0 :         make_umid();
     331             : 
     332           0 :         return 0;
     333             : }
     334             : 
     335             : __initcall(make_umid_init);
     336             : 
     337           2 : int __init umid_file_name(char *name, char *buf, int len)
     338             : {
     339             :         int n, err;
     340             : 
     341           2 :         err = make_umid();
     342           2 :         if (err)
     343             :                 return err;
     344             : 
     345           2 :         n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
     346           2 :         if (n >= len) {
     347           0 :                 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
     348           0 :                 return -E2BIG;
     349             :         }
     350             : 
     351             :         return 0;
     352             : }
     353             : 
     354           1 : char *get_umid(void)
     355             : {
     356           1 :         return umid;
     357             : }
     358             : 
     359           0 : static int __init set_uml_dir(char *name, int *add)
     360             : {
     361           0 :         if (*name == '\0') {
     362           0 :                 os_warn("uml_dir can't be an empty string\n");
     363           0 :                 return 0;
     364             :         }
     365             : 
     366           0 :         if (name[strlen(name) - 1] == '/') {
     367           0 :                 uml_dir = name;
     368           0 :                 return 0;
     369             :         }
     370             : 
     371           0 :         uml_dir = malloc(strlen(name) + 2);
     372           0 :         if (uml_dir == NULL) {
     373           0 :                 os_warn("Failed to malloc uml_dir - error = %d\n", errno);
     374             : 
     375             :                 /*
     376             :                  * Return 0 here because do_initcalls doesn't look at
     377             :                  * the return value.
     378             :                  */
     379           0 :                 return 0;
     380             :         }
     381           0 :         sprintf(uml_dir, "%s/", name);
     382             : 
     383           0 :         return 0;
     384             : }
     385             : 
     386             : __uml_setup("uml_dir=", set_uml_dir,
     387             : "uml_dir=<directory>\n"
     388             : "    The location to place the pid and umid files.\n\n"
     389             : );
     390             : 
     391           1 : static void remove_umid_dir(void)
     392             : {
     393             :         char *dir, err;
     394             : 
     395           1 :         dir = malloc(strlen(uml_dir) + UMID_LEN + 1);
     396           1 :         if (!dir)
     397             :                 return;
     398             : 
     399           1 :         sprintf(dir, "%s%s", uml_dir, umid);
     400           1 :         err = remove_files_and_dir(dir);
     401           1 :         if (err)
     402           0 :                 os_warn("%s - remove_files_and_dir failed with err = %d\n",
     403             :                         __func__, err);
     404             : 
     405           1 :         free(dir);
     406             : }
     407             : 
     408             : __uml_exitcall(remove_umid_dir);

Generated by: LCOV version 1.14