diff -Naur -X exclude-files ac_cur/arch/um/include/umid.h ac/arch/um/include/umid.h --- ac_cur/arch/um/include/umid.h Wed Dec 31 19:00:00 1969 +++ ac/arch/um/include/umid.h Sat Aug 4 20:46:24 2001 @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2001 Jeff Dike (jdike@karaya.com) + * Licensed under the GPL + */ + +extern int set_umid(char *name); +extern int umid_file_name(char *name, char *buf, int len); + +/* + * Overrides for Emacs so that we follow Linus's tabbing style. + * Emacs will notice this stuff at the end of the file and automatically + * adjust the settings for this buffer only. This must remain at the end + * of the file. + * --------------------------------------------------------------------------- + * Local variables: + * c-file-style: "linux" + * End: + */ diff -Naur -X exclude-files ac_cur/arch/um/kernel/Makefile ac/arch/um/kernel/Makefile --- ac_cur/arch/um/kernel/Makefile Sat Aug 4 15:57:57 2001 +++ ac/arch/um/kernel/Makefile Sat Aug 4 20:46:24 2001 @@ -4,7 +4,7 @@ irq_user.o mem.o mem_user.o ptrace.o reboot.o resource.o \ setup.o signal_user.o smp.o syscall_kern.o syscall_user.o \ sys_call_table.o time.o time_kern.o tlb.o trap_kern.o \ - trap_user.o um_arch.o user_util.o uaccess_user.o + trap_user.o uaccess_user.o um_arch.o umid.o user_util.o OX_OBJS = ksyms.o process_kern.o signal_kern.o user_syms.o @@ -53,10 +53,13 @@ trap_user.o: trap_user.c $(CC) $(USER_CFLAGS) -c -o $@ $< -user_util.o: user_util.c +uaccess_user.o: uaccess_user.c + $(CC) $(USER_CFLAGS) -c -o $@ $< + +umid.o: umid.c $(CC) $(USER_CFLAGS) -c -o $@ $< -uaccess.o: uaccess_user.c +user_util.o: user_util.c $(CC) $(USER_CFLAGS) -c -o $@ $< unmap.o: unmap.c diff -Naur -X exclude-files ac_cur/arch/um/kernel/process_kern.c ac/arch/um/kernel/process_kern.c --- ac_cur/arch/um/kernel/process_kern.c Sat Aug 4 16:12:11 2001 +++ ac/arch/um/kernel/process_kern.c Sat Aug 4 20:46:24 2001 @@ -778,8 +778,8 @@ return(task->thread.request.u.tracing.restore_regs); } -extern void remove_pid_file(void); -__exitcall(remove_pid_file); +extern void remove_umid_dir(void); +__exitcall(remove_umid_dir); extern exitcall_t __exitcall_begin, __exitcall_end; diff -Naur -X exclude-files ac_cur/arch/um/kernel/um_arch.c ac/arch/um/kernel/um_arch.c --- ac_cur/arch/um/kernel/um_arch.c Sat Aug 4 15:57:57 2001 +++ ac/arch/um/kernel/um_arch.c Sat Aug 4 20:46:24 2001 @@ -28,6 +28,7 @@ #include "kern.h" #include "mprot.h" #include "mem_user.h" +#include "umid.h" unsigned long _stext; @@ -154,7 +155,10 @@ " filesystem can have its contents read in the virtual \n" " machine by running dd on the device. n must be in the range\n" " 0 to 7. Appending an 'r' to the number will cause that device\n" -" to be mounted read-only. For example ubd1r=./ext_fs\n\n"; +" to be mounted read-only. For example ubd1r=./ext_fs\n\n" +"umid=\n" +" This is used to assign a unique identity to this UML machine\n" +" Used for naming the pid file and console socket\n"; static void Usage(void) { @@ -241,7 +245,7 @@ NULL, 0); #endif else if(!strncmp(argv[i], "umid=", strlen("umid="))){ - create_pid_file(&argv[i][strlen("umid=")]); + set_umid(&argv[i][strlen("umid=")]); } else if(!strncmp(argv[i], "iomem=",strlen("iomem="))){ parse_iomem(&argv[i][strlen("iomem=")]); diff -Naur -X exclude-files ac_cur/arch/um/kernel/umid.c ac/arch/um/kernel/umid.c --- ac_cur/arch/um/kernel/umid.c Wed Dec 31 19:00:00 1969 +++ ac/arch/um/kernel/umid.c Sat Aug 4 20:46:24 2001 @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "user.h" +#include "umid.h" + +#define UMID_LEN 64 + +static char umid[UMID_LEN] = { 0 }; + +static int umid_inited = 0; + +int umid_file_name(char *name, char *buf, int len) +{ + int n; + + if(!umid_inited && set_umid(NULL)) return(-1); + + n = sizeof("/tmp/uml") + strlen(umid) + sizeof("/") + strlen(name) + 1; + if(n > len){ + printk("umid_file_name : buffer too short\n"); + return(-1); + } + + sprintf(buf, "/tmp/uml/%s/%s", umid, name); + return(0); +} + +static void create_pid_file(void) +{ + char file[sizeof("/tmp/uml/") + UMID_LEN + sizeof("/pid\0")]; + char pid[sizeof("nnnnn\0")]; + int fd; + + if(umid_file_name("pid", file, sizeof(file))) return; + + if((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0){ + printk("Open of machine pid file \"%s\" failed - " + "errno = %d\n", file, errno); + return; + } + + sprintf(pid, "%d\n", getpid()); + if(write(fd, pid, strlen(pid)) != strlen(pid)) + printk("Write of pid file failed - errno = %d\n", errno); + close(fd); +} + +static int actually_do_remove(char *dir) +{ + DIR *directory; + struct dirent *ent; + int len; + char file[256]; + + if((directory = opendir(dir)) == NULL){ + printk("actually_do_remove : couldn't open directory '%s', " + "errno = %d", dir, errno); + return(1); + } + while((ent = readdir(directory)) != NULL){ + if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) + continue; + len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; + if(len > sizeof(file)){ + printk("Not deleting '%s' from '%s' - name too long\n", + ent->d_name, dir); + continue; + } + sprintf(file, "%s/%s", dir, ent->d_name); + if(unlink(file) < 0){ + printk("actually_do_remove : couldn't remove '%s' " + "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); +} + +void remove_umid_dir(void) +{ + char dir[sizeof("/tmp/uml/") + UMID_LEN + 1]; + if(!umid_inited) return; + + sprintf(dir, "/tmp/uml/%s", umid); + actually_do_remove(dir); +} + +char *get_umid(void) +{ + return(umid); +} + +static int not_dead_yet(char *dir) +{ + char file[sizeof("/tmp/uml/") + UMID_LEN + sizeof("/pid\0")]; + char pid[sizeof("nnnnn\0")], *end; + int dead, fd, p; + + sprintf(file, "%s/pid", dir); + dead = 0; + if((fd = open(file, O_RDONLY)) < 0){ + if(errno != ENOENT){ + printk("not_dead_yet : couldn't open pid file '%s', " + "errno = %d\n", file, errno); + return(1); + } + dead = 1; + } + if(fd > 0){ + if(read(fd, pid, sizeof(pid)) < 0){ + printk("not_dead_yet : couldn't read pid file '%s', " + "errno = %d\n", file, errno); + return(1); + } + p = strtoul(pid, &end, 0); + if(end == pid){ + printk("not_dead_yet : couldn't parse pid file '%s', " + "errno = %d\n", file, errno); + dead = 1; + } + if((kill(p, 0) < 0) && (errno == ESRCH)) dead = 1; + } + if(!dead) return(1); + return(actually_do_remove(dir)); + return(0); +} + +int set_umid(char *name) +{ + int fd, err; + char tmp[sizeof("/tmp/uml/") + UMID_LEN + 1]; + + if(umid_inited){ + printk("Unique machine name can't be set twice\n"); + return(-1); + } + + if((mkdir("/tmp/uml", 0777) < 0) && (errno != EEXIST)){ + printk("Failed to create /tmp/uml - errno = %s\n", errno); + return(-1); + } + + strcpy(tmp, "/tmp/uml/"); + + if(name == NULL){ + strcat(tmp, "XXXXXX"); + fd = mkstemp(tmp); + if(fd < 0){ + printk("set_umid - mkstemp failed, errno = %d\n", + errno); + return(-1); + } + + close(fd); + /* There's a nice tiny little race between this unlink and + * the mkdir below. It'd be nice if there were a mkstemp + * for directories. + */ + unlink(tmp); + name = &tmp[strlen("/tmp/uml/")]; + } + + if(strlen(name) > UMID_LEN - 1) + printk("Unique machine name is being truncated to %s " + "characters\n", UMID_LEN); + strncpy(umid, name, UMID_LEN - 1); + umid[UMID_LEN - 1] = '\0'; + + sprintf(tmp, "/tmp/uml/%s", umid); + + if((err = mkdir(tmp, 0777)) < 0){ + if(errno == EEXIST){ + if(not_dead_yet(tmp)){ + printk("umid '%s' is in use\n", umid); + return(-1); + } + err = mkdir(tmp, 0777); + } + } + if(err < 0){ + printk("Failed to create %s - errno = %d\n", umid, errno); + return(-1); + } + + umid_inited = 1; + create_pid_file(); + return(0); +} + +/* + * Overrides for Emacs so that we follow Linus's tabbing style. + * Emacs will notice this stuff at the end of the file and automatically + * adjust the settings for this buffer only. This must remain at the end + * of the file. + * --------------------------------------------------------------------------- + * Local variables: + * c-file-style: "linux" + * End: + */ diff -Naur -X exclude-files ac_cur/arch/um/kernel/user_util.c ac/arch/um/kernel/user_util.c --- ac_cur/arch/um/kernel/user_util.c Sat Aug 4 15:57:57 2001 +++ ac/arch/um/kernel/user_util.c Sat Aug 4 20:46:24 2001 @@ -331,57 +331,6 @@ close(fd); } -#define UMID_LEN 64 -char umid[UMID_LEN] = { 0 }; - -void create_pid_file(char *name) -{ - char tmp[sizeof("/tmp/uml/") + UMID_LEN + 1]; - int fd; - static int umid_inited = 0; - - if(umid_inited){ - printk("Unique machine name can't be set twice\n"); - return; - } - if((mkdir("/tmp/uml", 0777) < 0) && - (errno != EEXIST)){ - printk("Failed to create /tmp/uml - errno = %s\n", errno); - return; - } - umid_inited = 1; - if(strlen(name) > UMID_LEN - 1) - printk("Unique machine name is being truncated to %s " - "characters\n", UMID_LEN); - strncpy(umid, name, UMID_LEN - 1); - umid[UMID_LEN - 1] = '\0'; - sprintf(tmp, "/tmp/uml/%s", umid); - if((fd = open(tmp, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0){ - printk("Open of machine pid file \"%s\" failed - " - "errno = %d\n", umid, errno); - return; - } - sprintf(tmp, "%d\n", getpid()); - if(write(fd, tmp, strlen(tmp)) != strlen(tmp)) - printk("Write of pid file failed - errno = %d\n", errno); - close(fd); -} - -void remove_pid_file(void) -{ - char tmp[sizeof("/tmp/uml/") + UMID_LEN + 1]; - - if(umid[0] == '\0') return; - sprintf(tmp, "/tmp/uml/%s", umid); - unlink(tmp); - return; -} - -char *get_umid(void) -{ - return(umid); -} - /* * Overrides for Emacs so that we follow Linus's tabbing style. * Emacs will notice this stuff at the end of the file and automatically