Commit 7eebe8a9 authored by Jeff Dike's avatar Jeff Dike Committed by Linus Torvalds

[PATCH] uml: umid cleanup

This patch cleans up the umid code:

- The only_if_set argument to get_umid is gone.

- get_umid returns an empty string rather than NULL if there is no umid.

- umid_is_random is gone since its users went away.

- Some printfs were turned into printks because the code runs late enough
  that printk is working.

- Error paths were cleaned up.

- Some functions now return an error and let the caller print the error
  message rather than printing it themselves.  This eliminates the practice of
  passing a pointer to printf or printk in, depending on where in the boot
  process we are.

- Major tidying of not_dead_yet - mostly error path cleanup, plus a comment
  explaining why it doesn't react to errors the way you might expect.

- Calls to os_* interfaces that were moved under os are changed back to
  their native libc forms.

- snprintf, strlcpy, and their bounds-checking friends are used more often,
  replacing by-hand bounds checking in some places.
Signed-off-by: default avatarJeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 2264c475
...@@ -831,8 +831,8 @@ char *add_xterm_umid(char *base) ...@@ -831,8 +831,8 @@ char *add_xterm_umid(char *base)
char *umid, *title; char *umid, *title;
int len; int len;
umid = get_umid(1); umid = get_umid();
if(umid == NULL) if(*umid == '\0')
return base; return base;
len = strlen(base) + strlen(" ()") + strlen(umid) + 1; len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
......
...@@ -216,7 +216,7 @@ extern int helper_wait(int pid); ...@@ -216,7 +216,7 @@ extern int helper_wait(int pid);
/* umid.c */ /* umid.c */
extern int umid_file_name(char *name, char *buf, int len); extern int umid_file_name(char *name, char *buf, int len);
extern int set_umid(char *name, int (*printer)(const char *fmt, ...)); extern int set_umid(char *name);
extern char *get_umid(int only_if_set); extern char *get_umid(void);
#endif #endif
...@@ -64,7 +64,6 @@ extern void setup_machinename(char *machine_out); ...@@ -64,7 +64,6 @@ extern void setup_machinename(char *machine_out);
extern void setup_hostinfo(void); extern void setup_hostinfo(void);
extern void do_exec(int old_pid, int new_pid); extern void do_exec(int old_pid, int new_pid);
extern void tracer_panic(char *msg, ...); extern void tracer_panic(char *msg, ...);
extern char *get_umid(int only_if_set);
extern void do_longjmp(void *p, int val); extern void do_longjmp(void *p, int val);
extern int detach(int pid, int sig); extern int detach(int pid, int sig);
extern int attach(int pid); extern int attach(int pid);
......
...@@ -146,8 +146,8 @@ void set_cmdline(char *cmd) ...@@ -146,8 +146,8 @@ void set_cmdline(char *cmd)
if(CHOOSE_MODE(honeypot, 0)) return; if(CHOOSE_MODE(honeypot, 0)) return;
umid = get_umid(1); umid = get_umid();
if(umid != NULL){ if(*umid != '\0'){
snprintf(argv1_begin, snprintf(argv1_begin,
(argv1_end - argv1_begin) * sizeof(*ptr), (argv1_end - argv1_begin) * sizeof(*ptr),
"(%s) ", umid); "(%s) ", umid);
......
...@@ -3,15 +3,13 @@ ...@@ -3,15 +3,13 @@
* Licensed under the GPL * Licensed under the GPL
*/ */
#include "linux/stddef.h"
#include "linux/kernel.h"
#include "asm/errno.h" #include "asm/errno.h"
#include "init.h" #include "init.h"
#include "os.h" #include "os.h"
#include "kern.h" #include "kern.h"
#include "linux/kernel.h"
/* Changed by set_umid_arg and umid_file_name */ /* Changed by set_umid_arg */
int umid_is_random = 0;
static int umid_inited = 0; static int umid_inited = 0;
static int __init set_umid_arg(char *name, int *add) static int __init set_umid_arg(char *name, int *add)
...@@ -22,11 +20,9 @@ static int __init set_umid_arg(char *name, int *add) ...@@ -22,11 +20,9 @@ static int __init set_umid_arg(char *name, int *add)
return 0; return 0;
*add = 0; *add = 0;
err = set_umid(name, printf); err = set_umid(name);
if(err == -EEXIST){ if(err == -EEXIST)
printf("umid '%s' already in use\n", name); printf("umid '%s' already in use\n", name);
umid_is_random = 1;
}
else if(!err) else if(!err)
umid_inited = 1; umid_inited = 1;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <dirent.h> #include <dirent.h>
#include <sys/fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/param.h> #include <sys/param.h>
#include "init.h" #include "init.h"
...@@ -25,15 +26,16 @@ static char *uml_dir = UML_DIR; ...@@ -25,15 +26,16 @@ static char *uml_dir = UML_DIR;
static int __init make_uml_dir(void) static int __init make_uml_dir(void)
{ {
char dir[512] = { '\0' }; char dir[512] = { '\0' };
int len; int len, err;
if(*uml_dir == '~'){ if(*uml_dir == '~'){
char *home = getenv("HOME"); char *home = getenv("HOME");
err = -ENOENT;
if(home == NULL){ if(home == NULL){
printf("make_uml_dir : no value in environment for " printk("make_uml_dir : no value in environment for "
"$HOME\n"); "$HOME\n");
exit(1); goto err;
} }
strlcpy(dir, home, sizeof(dir)); strlcpy(dir, home, sizeof(dir));
uml_dir++; uml_dir++;
...@@ -43,18 +45,26 @@ static int __init make_uml_dir(void) ...@@ -43,18 +45,26 @@ static int __init make_uml_dir(void)
if (len > 0 && dir[len - 1] != '/') if (len > 0 && dir[len - 1] != '/')
strlcat(dir, "/", sizeof(dir)); strlcat(dir, "/", sizeof(dir));
err = -ENOMEM;
uml_dir = malloc(strlen(dir) + 1); uml_dir = malloc(strlen(dir) + 1);
if (uml_dir == NULL) { if (uml_dir == NULL) {
printf("make_uml_dir : malloc failed, errno = %d\n", errno); printf("make_uml_dir : malloc failed, errno = %d\n", errno);
exit(1); goto err;
} }
strcpy(uml_dir, dir); strcpy(uml_dir, dir);
if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){ if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno)); printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
return(-1); err = -errno;
goto err_free;
} }
return 0; return 0;
err_free:
free(uml_dir);
err:
uml_dir = NULL;
return err;
} }
static int actually_do_remove(char *dir) static int actually_do_remove(char *dir)
...@@ -65,75 +75,88 @@ static int actually_do_remove(char *dir) ...@@ -65,75 +75,88 @@ static int actually_do_remove(char *dir)
char file[256]; char file[256];
directory = opendir(dir); directory = opendir(dir);
if(directory == NULL){ if(directory == NULL)
printk("actually_do_remove : couldn't open directory '%s', " return -errno;
"errno = %d\n", dir, errno);
return(1);
}
while((ent = readdir(directory)) != NULL){ while((ent = readdir(directory)) != NULL){
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue; continue;
len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
if(len > sizeof(file)){ if(len > sizeof(file))
printk("Not deleting '%s' from '%s' - name too long\n", return -E2BIG;
ent->d_name, dir);
continue;
}
sprintf(file, "%s/%s", dir, ent->d_name); sprintf(file, "%s/%s", dir, ent->d_name);
if(unlink(file) < 0){ if(unlink(file) < 0)
printk("actually_do_remove : couldn't remove '%s' " return -errno;
"from '%s', errno = %d\n", ent->d_name, dir,
errno);
return(1);
}
}
if(rmdir(dir) < 0){
printk("actually_do_remove : couldn't rmdir '%s', "
"errno = %d\n", dir, errno);
return(1);
} }
return(0); if(rmdir(dir) < 0)
return -errno;
return 0;
} }
extern int tracing_pid; /* This says that there isn't already a user of the specified directory even if
* there are errors during the checking. This is because if these errors
* happen, the directory is unusable by the pre-existing UML, so we might as
* well take it over. This could happen either by
* the existing UML somehow corrupting its umid directory
* something other than UML sticking stuff in the directory
* this boot racing with a shutdown of the other UML
* In any of these cases, the directory isn't useful for anything else.
*/
static int not_dead_yet(char *dir) static int not_dead_yet(char *dir)
{ {
char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
char pid[sizeof("nnnnn\0")], *end; char pid[sizeof("nnnnn\0")], *end;
int dead, fd, p, n; int dead, fd, p, n, err;
n = snprintf(file, sizeof(file), "%s/pid", dir);
if(n >= sizeof(file)){
printk("not_dead_yet - pid filename too long\n");
err = -E2BIG;
goto out;
}
sprintf(file, "%s/pid", dir);
dead = 0; dead = 0;
fd = os_open_file(file, of_read(OPENFLAGS()), 0); fd = open(file, O_RDONLY);
if(fd < 0){ if(fd < 0){
if(fd != -ENOENT){ if(fd != -ENOENT){
printk("not_dead_yet : couldn't open pid file '%s', " printk("not_dead_yet : couldn't open pid file '%s', "
"err = %d\n", file, -fd); "err = %d\n", file, -fd);
return(1);
} }
dead = 1; goto out;
} }
if(fd > 0){
n = os_read_file(fd, pid, sizeof(pid)); err = 0;
if(n < 0){ n = read(fd, pid, sizeof(pid));
printk("not_dead_yet : couldn't read pid file '%s', " if(n <= 0){
"err = %d\n", file, -n); printk("not_dead_yet : couldn't read pid file '%s', "
return(1); "err = %d\n", file, -n);
} goto out_close;
p = strtoul(pid, &end, 0); }
if(end == pid){
printk("not_dead_yet : couldn't parse pid file '%s', " p = strtoul(pid, &end, 0);
"errno = %d\n", file, errno); if(end == pid){
dead = 1; printk("not_dead_yet : couldn't parse pid file '%s', "
} "errno = %d\n", file, errno);
if(((kill(p, 0) < 0) && (errno == ESRCH)) || goto out_close;
(p == CHOOSE_MODE(tracing_pid, os_getpid())))
dead = 1;
} }
if(!dead)
return(1); if((kill(p, 0) == 0) || (errno != ESRCH))
return(actually_do_remove(dir)); return 1;
err = actually_do_remove(dir);
if(err)
printk("not_dead_yet - actually_do_remove failed with "
"err = %d\n", err);
return err;
out_close:
close(fd);
out:
return 0;
} }
static void __init create_pid_file(void) static void __init create_pid_file(void)
...@@ -145,26 +168,26 @@ static void __init create_pid_file(void) ...@@ -145,26 +168,26 @@ static void __init create_pid_file(void)
if(umid_file_name("pid", file, sizeof(file))) if(umid_file_name("pid", file, sizeof(file)))
return; return;
fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))), fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
0644);
if(fd < 0){ if(fd < 0){
printf("Open of machine pid file \"%s\" failed: %s\n", printk("Open of machine pid file \"%s\" failed: %s\n",
file, strerror(-fd)); file, strerror(-fd));
return; return;
} }
sprintf(pid, "%d\n", os_getpid()); snprintf(pid, sizeof(pid), "%d\n", getpid());
n = os_write_file(fd, pid, strlen(pid)); n = write(fd, pid, strlen(pid));
if(n != strlen(pid)) if(n != strlen(pid))
printf("Write of pid file failed - err = %d\n", -n); printk("Write of pid file failed - err = %d\n", -n);
os_close_file(fd);
close(fd);
} }
int __init set_umid(char *name, int (*printer)(const char *fmt, ...)) int __init set_umid(char *name)
{ {
if(strlen(name) > UMID_LEN - 1) if(strlen(name) > UMID_LEN - 1)
(*printer)("Unique machine name is being truncated to %d " return -E2BIG;
"characters\n", UMID_LEN);
strlcpy(umid, name, sizeof(umid)); strlcpy(umid, name, sizeof(umid));
return 0; return 0;
...@@ -172,44 +195,56 @@ int __init set_umid(char *name, int (*printer)(const char *fmt, ...)) ...@@ -172,44 +195,56 @@ int __init set_umid(char *name, int (*printer)(const char *fmt, ...))
static int umid_setup = 0; static int umid_setup = 0;
int __init make_umid(int (*printer)(const char *fmt, ...)) int __init make_umid(void)
{ {
int fd, err; int fd, err;
char tmp[256]; char tmp[256];
if(umid_setup)
return 0;
make_uml_dir(); make_uml_dir();
if(*umid == '\0'){ if(*umid == '\0'){
strlcpy(tmp, uml_dir, sizeof(tmp)); strlcpy(tmp, uml_dir, sizeof(tmp));
strcat(tmp, "XXXXXX"); strlcat(tmp, "XXXXXX", sizeof(tmp));
fd = mkstemp(tmp); fd = mkstemp(tmp);
if(fd < 0){ if(fd < 0){
(*printer)("make_umid - mkstemp(%s) failed: %s\n", printk("make_umid - mkstemp(%s) failed: %s\n",
tmp,strerror(errno)); tmp, strerror(errno));
return(1); err = -errno;
goto err;
} }
os_close_file(fd); close(fd);
set_umid(&tmp[strlen(uml_dir)]);
/* There's a nice tiny little race between this unlink and /* There's a nice tiny little race between this unlink and
* the mkdir below. It'd be nice if there were a mkstemp * the mkdir below. It'd be nice if there were a mkstemp
* for directories. * for directories.
*/ */
unlink(tmp); if(unlink(tmp)){
set_umid(&tmp[strlen(uml_dir)], printer); err = -errno;
goto err;
}
} }
sprintf(tmp, "%s%s", uml_dir, umid); snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
err = mkdir(tmp, 0777); err = mkdir(tmp, 0777);
if(err < 0){ if(err < 0){
if(errno == EEXIST){ err = -errno;
if(not_dead_yet(tmp)) if(errno != EEXIST)
return -EEXIST; goto err;
err = mkdir(tmp, 0777);
} if(not_dead_yet(tmp) < 0)
goto err;
err = mkdir(tmp, 0777);
} }
if(err < 0){ if(err < 0){
(*printer)("Failed to create %s - errno = %d\n", umid, errno); printk("Failed to create '%s' - err = %d\n", umid, err);
return(-1); goto err_rmdir;
} }
umid_setup = 1; umid_setup = 1;
...@@ -217,13 +252,18 @@ int __init make_umid(int (*printer)(const char *fmt, ...)) ...@@ -217,13 +252,18 @@ int __init make_umid(int (*printer)(const char *fmt, ...))
create_pid_file(); create_pid_file();
return 0; return 0;
err_rmdir:
rmdir(tmp);
err:
return err;
} }
static int __init make_umid_init(void) static int __init make_umid_init(void)
{ {
make_umid(printk); make_umid();
return(0); return 0;
} }
__initcall(make_umid_init); __initcall(make_umid_init);
...@@ -232,48 +272,48 @@ int __init umid_file_name(char *name, char *buf, int len) ...@@ -232,48 +272,48 @@ int __init umid_file_name(char *name, char *buf, int len)
{ {
int n, err; int n, err;
if(!umid_setup){ err = make_umid();
err = make_umid(printk); if(err)
if(err) return err;
return err;
}
n = strlen(uml_dir) + strlen(umid) + strlen("/") + strlen(name) + 1; n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
if(n > len){ if(n >= len){
printk("umid_file_name : buffer too short\n"); printk("umid_file_name : buffer too short\n");
return(-1); return -E2BIG;
} }
sprintf(buf, "%s%s/%s", uml_dir, umid, name); return 0;
return(0);
} }
extern int umid_is_random; char *get_umid(void)
char *get_umid(int only_if_set)
{ {
if(only_if_set && umid_is_random)
return NULL;
return umid; return umid;
} }
static int __init set_uml_dir(char *name, int *add) static int __init set_uml_dir(char *name, int *add)
{ {
if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){ if(*name == '\0'){
uml_dir = malloc(strlen(name) + 2); printf("uml_dir can't be an empty string\n");
if(uml_dir == NULL){ return 0;
printf("Failed to malloc uml_dir - error = %d\n",
errno);
uml_dir = name;
/* Return 0 here because do_initcalls doesn't look at
* the return value.
*/
return(0);
}
sprintf(uml_dir, "%s/", name);
} }
else uml_dir = name;
return(0); if(name[strlen(name) - 1] == '/'){
uml_dir = name;
return 0;
}
uml_dir = malloc(strlen(name) + 2);
if(uml_dir == NULL){
printf("Failed to malloc uml_dir - error = %d\n", errno);
/* Return 0 here because do_initcalls doesn't look at
* the return value.
*/
return 0;
}
sprintf(uml_dir, "%s/", name);
return 0;
} }
__uml_setup("uml_dir=", set_uml_dir, __uml_setup("uml_dir=", set_uml_dir,
...@@ -283,10 +323,13 @@ __uml_setup("uml_dir=", set_uml_dir, ...@@ -283,10 +323,13 @@ __uml_setup("uml_dir=", set_uml_dir,
static void remove_umid_dir(void) static void remove_umid_dir(void)
{ {
char dir[strlen(uml_dir) + UMID_LEN + 1]; char dir[strlen(uml_dir) + UMID_LEN + 1], err;
sprintf(dir, "%s%s", uml_dir, umid); sprintf(dir, "%s%s", uml_dir, umid);
actually_do_remove(dir); err = actually_do_remove(dir);
if(err)
printf("remove_umid_dir - actually_do_remove failed with "
"err = %d\n", err);
} }
__uml_exitcall(remove_umid_dir); __uml_exitcall(remove_umid_dir);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment