2002-10-30 02:07:06 +03:00
|
|
|
/* Team functions */
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
/*
|
|
|
|
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
|
|
|
|
** Distributed under the terms of the OpenBeOS License.
|
|
|
|
*/
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
/*
|
|
|
|
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <OS.h>
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <thread_types.h>
|
|
|
|
#include <int.h>
|
|
|
|
#include <khash.h>
|
2002-10-30 02:07:06 +03:00
|
|
|
#include <malloc.h>
|
2002-08-04 03:39:50 +04:00
|
|
|
#include <user_runtime.h>
|
|
|
|
#include <Errors.h>
|
2003-01-12 19:29:28 +03:00
|
|
|
#include <kimage.h>
|
2002-08-04 03:39:50 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <elf.h>
|
|
|
|
#include <syscalls.h>
|
2003-01-07 12:40:59 +03:00
|
|
|
#include <tls.h>
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-11-12 18:37:44 +03:00
|
|
|
#define TRACE_TEAM 0
|
|
|
|
#if TRACE_TEAM
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
struct team_key {
|
|
|
|
team_id id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct team_arg {
|
|
|
|
char *path;
|
|
|
|
char **args;
|
|
|
|
char **envp;
|
|
|
|
unsigned int argc;
|
|
|
|
unsigned int envc;
|
|
|
|
};
|
|
|
|
|
|
|
|
// team list
|
|
|
|
static void *team_hash = NULL;
|
2002-08-05 00:10:06 +04:00
|
|
|
static team_id next_team_id = 1;
|
2002-08-04 03:39:50 +04:00
|
|
|
static struct team *kernel_team = NULL;
|
|
|
|
|
2002-10-26 20:13:36 +04:00
|
|
|
spinlock team_spinlock = 0;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
static struct team *create_team_struct(const char *name, bool kernel);
|
|
|
|
static void delete_team_struct(struct team *p);
|
|
|
|
static int team_struct_compare(void *_p, const void *_key);
|
2002-11-29 11:38:52 +03:00
|
|
|
static uint32 team_struct_hash(void *_p, const void *_key, uint32 range);
|
2002-08-04 03:39:50 +04:00
|
|
|
static void kfree_strings_array(char **strings, int strc);
|
|
|
|
static int user_copy_strings_array(char **strings, int strc, char ***kstrings);
|
|
|
|
static void _dump_team_info(struct team *p);
|
|
|
|
static int dump_team_info(int argc, char **argv);
|
|
|
|
|
|
|
|
|
2002-09-24 03:24:12 +04:00
|
|
|
static void
|
|
|
|
_dump_team_info(struct team *p)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
dprintf("TEAM: %p\n", p);
|
2002-09-24 03:24:12 +04:00
|
|
|
dprintf("id: 0x%lx\n", p->id);
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("name: '%s'\n", p->name);
|
|
|
|
dprintf("next: %p\n", p->next);
|
|
|
|
dprintf("num_threads: %d\n", p->num_threads);
|
|
|
|
dprintf("state: %d\n", p->state);
|
|
|
|
dprintf("pending_signals: 0x%x\n", p->pending_signals);
|
2002-12-03 17:17:53 +03:00
|
|
|
dprintf("io_context: %p\n", p->io_context);
|
|
|
|
// dprintf("path: '%s'\n", p->path);
|
2002-09-24 03:24:12 +04:00
|
|
|
dprintf("aspace_id: 0x%lx\n", p->_aspace_id);
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("aspace: %p\n", p->aspace);
|
|
|
|
dprintf("kaspace: %p\n", p->kaspace);
|
|
|
|
dprintf("main_thread: %p\n", p->main_thread);
|
|
|
|
dprintf("thread_list: %p\n", p->thread_list);
|
|
|
|
}
|
|
|
|
|
2002-09-24 03:24:12 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
dump_team_info(int argc, char **argv)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
struct team *p;
|
|
|
|
int id = -1;
|
|
|
|
unsigned long num;
|
|
|
|
struct hash_iterator i;
|
|
|
|
|
2002-09-24 03:24:12 +04:00
|
|
|
if (argc < 2) {
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("team: not enough arguments\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the argument looks like a hex number, treat it as such
|
2002-09-24 03:24:12 +04:00
|
|
|
if (strlen(argv[1]) > 2 && argv[1][0] == '0' && argv[1][1] == 'x') {
|
2002-08-04 03:39:50 +04:00
|
|
|
num = atoul(argv[1]);
|
2002-09-24 03:24:12 +04:00
|
|
|
if (num > vm_get_kernel_aspace()->virtual_map.base) {
|
2002-08-04 03:39:50 +04:00
|
|
|
// XXX semi-hack
|
|
|
|
_dump_team_info((struct team*)num);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
id = num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk through the thread list, trying to match name or id
|
|
|
|
hash_open(team_hash, &i);
|
2002-09-24 03:24:12 +04:00
|
|
|
while ((p = hash_next(team_hash, &i)) != NULL) {
|
|
|
|
if ((p->name && strcmp(argv[1], p->name) == 0) || p->id == id) {
|
2002-08-04 03:39:50 +04:00
|
|
|
_dump_team_info(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash_close(team_hash, &i, false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
team_init(kernel_args *ka)
|
|
|
|
{
|
|
|
|
// create the team hash table
|
|
|
|
team_hash = hash_init(15, (addr)&kernel_team->next - (addr)kernel_team,
|
|
|
|
&team_struct_compare, &team_struct_hash);
|
|
|
|
|
|
|
|
// create the kernel team
|
|
|
|
kernel_team = create_team_struct("kernel_team", true);
|
2002-08-05 09:26:52 +04:00
|
|
|
if (kernel_team == NULL)
|
2002-08-04 03:39:50 +04:00
|
|
|
panic("could not create kernel team!\n");
|
|
|
|
kernel_team->state = TEAM_STATE_NORMAL;
|
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
kernel_team->io_context = vfs_new_io_context(NULL);
|
|
|
|
if (kernel_team->io_context == NULL)
|
|
|
|
panic("could not create io_context for kernel team!\n");
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
//XXX should initialize kernel_team->path here. Set it to "/"?
|
|
|
|
|
|
|
|
// stick it in the team hash
|
|
|
|
hash_insert(team_hash, kernel_team);
|
|
|
|
|
|
|
|
add_debugger_command("team", &dump_team_info, "list info about a particular team");
|
2002-08-05 09:26:52 +04:00
|
|
|
return 0;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
/** Frees an array of strings in kernel space
|
|
|
|
* Parameters
|
|
|
|
* strings strings array
|
|
|
|
* strc number of strings in array
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
kfree_strings_array(char **strings, int strc)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
int cnt = strc;
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
if (strings != NULL) {
|
|
|
|
for (cnt = 0; cnt < strc; cnt++){
|
|
|
|
free(strings[cnt]);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
2002-10-30 02:07:06 +03:00
|
|
|
free(strings);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
/** Copy an array of strings from user space to kernel space
|
|
|
|
* Parameters
|
|
|
|
* strings userspace strings array
|
|
|
|
* strc number of strings in array
|
|
|
|
* kstrings pointer to the kernel copy
|
|
|
|
* Returns < 0 on error and **kstrings = NULL
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2004-02-23 07:08:09 +03:00
|
|
|
user_copy_strings_array(char **userStrings, int strc, char ***kstrings)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
char **lstrings;
|
|
|
|
int err;
|
|
|
|
int cnt;
|
|
|
|
char *source;
|
2004-02-23 07:08:09 +03:00
|
|
|
char buffer[SYS_THREAD_STRING_LENGTH_MAX];
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
*kstrings = NULL;
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
if (!IS_USER_ADDRESS(userStrings))
|
|
|
|
return B_BAD_ADDRESS;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
lstrings = (char **)malloc((strc + 1) * sizeof(char *));
|
2002-12-03 17:17:53 +03:00
|
|
|
if (lstrings == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
// scan all strings and copy to kernel space
|
|
|
|
|
|
|
|
for (cnt = 0; cnt < strc; cnt++) {
|
2004-02-23 07:08:09 +03:00
|
|
|
err = user_memcpy(&source, &(userStrings[cnt]), sizeof(char *));
|
|
|
|
if (err < 0)
|
2002-08-04 03:39:50 +04:00
|
|
|
goto error;
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
if (!IS_USER_ADDRESS(source)) {
|
|
|
|
err = B_BAD_ADDRESS;
|
2002-08-04 03:39:50 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
err = user_strlcpy(buffer, source, SYS_THREAD_STRING_LENGTH_MAX);
|
2002-08-04 03:39:50 +04:00
|
|
|
if (err < 0)
|
|
|
|
goto error;
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
lstrings[cnt] = strdup(buffer);
|
2002-08-04 03:39:50 +04:00
|
|
|
if (lstrings[cnt] == NULL){
|
|
|
|
err = ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lstrings[strc] = NULL;
|
|
|
|
|
|
|
|
*kstrings = lstrings;
|
|
|
|
return B_NO_ERROR;
|
|
|
|
|
|
|
|
error:
|
|
|
|
kfree_strings_array(lstrings, cnt);
|
|
|
|
dprintf("user_copy_strings_array failed %d \n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
/** Quick check to see if we have a valid team ID.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
team_is_valid(team_id id)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2004-02-23 07:08:09 +03:00
|
|
|
struct team *team;
|
2002-08-04 03:39:50 +04:00
|
|
|
int state;
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
if (id <= 0)
|
|
|
|
return false;
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
team = team_get_team_struct_locked(id);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
return team != NULL;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
2002-08-04 06:04:37 +04:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
struct team *
|
|
|
|
team_get_team_struct_locked(team_id id)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
struct team_key key;
|
|
|
|
|
|
|
|
key.id = id;
|
|
|
|
|
|
|
|
return hash_lookup(team_hash, &key);
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
team_struct_compare(void *_p, const void *_key)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
struct team *p = _p;
|
|
|
|
const struct team_key *key = _key;
|
|
|
|
|
2002-11-29 11:38:52 +03:00
|
|
|
if (p->id == key->id)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2002-11-29 11:38:52 +03:00
|
|
|
static uint32
|
|
|
|
team_struct_hash(void *_p, const void *_key, uint32 range)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
struct team *p = _p;
|
|
|
|
const struct team_key *key = _key;
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
if (p != NULL)
|
2002-11-29 11:38:52 +03:00
|
|
|
return p->id % range;
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2002-11-29 11:38:52 +03:00
|
|
|
return key->id % range;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
team_remove_team_from_hash(struct team *team)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
hash_remove(team_hash, team);
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
struct team *
|
|
|
|
team_get_kernel_team(void)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
return kernel_team;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
team_id
|
|
|
|
team_get_kernel_team_id(void)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2002-08-05 09:26:52 +04:00
|
|
|
if (!kernel_team)
|
2002-08-04 03:39:50 +04:00
|
|
|
return 0;
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
return kernel_team->id;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
team_id
|
|
|
|
team_get_current_team_id(void)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
return thread_get_current_thread()->team->id;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
static struct team *
|
|
|
|
create_team_struct(const char *name, bool kernel)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2004-02-23 07:08:09 +03:00
|
|
|
struct team *team = (struct team *)malloc(sizeof(struct team));
|
2003-01-06 11:10:54 +03:00
|
|
|
if (team == NULL)
|
2004-02-23 07:08:09 +03:00
|
|
|
return NULL;
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2004-02-23 07:08:09 +03:00
|
|
|
team->next = team->siblings_next = team->children = team->parent = NULL;
|
2003-01-06 11:10:54 +03:00
|
|
|
team->id = atomic_add(&next_team_id, 1);
|
2004-02-23 07:08:09 +03:00
|
|
|
strlcpy(team->name, name, B_OS_NAME_LENGTH);
|
2003-01-06 11:10:54 +03:00
|
|
|
team->num_threads = 0;
|
|
|
|
team->io_context = NULL;
|
|
|
|
team->_aspace_id = -1;
|
|
|
|
team->aspace = NULL;
|
|
|
|
team->kaspace = vm_get_kernel_aspace();
|
|
|
|
vm_put_aspace(team->kaspace);
|
|
|
|
team->thread_list = NULL;
|
|
|
|
team->main_thread = NULL;
|
|
|
|
team->state = TEAM_STATE_BIRTH;
|
|
|
|
team->pending_signals = 0;
|
|
|
|
team->death_sem = -1;
|
|
|
|
team->user_env_base = 0;
|
2003-01-27 02:31:38 +03:00
|
|
|
list_init(&team->image_list);
|
2003-01-06 11:10:54 +03:00
|
|
|
|
|
|
|
if (arch_team_init_team_struct(team, kernel) < 0)
|
2004-02-23 07:08:09 +03:00
|
|
|
goto error;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-06 11:10:54 +03:00
|
|
|
return team;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
error:
|
2004-02-23 07:08:09 +03:00
|
|
|
free(team);
|
2002-08-04 03:39:50 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
static void
|
2003-01-06 11:10:54 +03:00
|
|
|
delete_team_struct(struct team *team)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-01-06 11:10:54 +03:00
|
|
|
free(team);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
static int
|
2003-01-12 19:29:28 +03:00
|
|
|
get_arguments_data_size(char **args, int argc)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-09-09 06:36:52 +04:00
|
|
|
uint32 size = 0;
|
|
|
|
int count;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
for (count = 0; count < argc; count++)
|
|
|
|
size += strlen(args[count]) + 1;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
return size + (argc + 1) * sizeof(char *) + sizeof(struct uspace_program_args);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2003-01-27 06:09:33 +03:00
|
|
|
static int32
|
2002-08-05 09:26:52 +04:00
|
|
|
team_create_team2(void *args)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct thread *t;
|
2003-01-07 12:40:59 +03:00
|
|
|
struct team *team;
|
2003-01-12 19:29:28 +03:00
|
|
|
struct team_arg *teamArgs = args;
|
2002-08-04 03:39:50 +04:00
|
|
|
char *path;
|
|
|
|
addr entry;
|
|
|
|
char ustack_name[128];
|
2003-01-07 12:40:59 +03:00
|
|
|
uint32 totalSize;
|
2002-08-04 03:39:50 +04:00
|
|
|
char **uargs;
|
|
|
|
char **uenv;
|
|
|
|
char *udest;
|
2003-01-12 19:29:28 +03:00
|
|
|
struct uspace_program_args *uspa;
|
2002-08-04 03:39:50 +04:00
|
|
|
unsigned int arg_cnt;
|
|
|
|
unsigned int env_cnt;
|
|
|
|
|
|
|
|
t = thread_get_current_thread();
|
2003-01-07 12:40:59 +03:00
|
|
|
team = t->team;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2002-09-24 03:24:12 +04:00
|
|
|
dprintf("team_create_team2: entry thread %ld\n", t->id);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
// create an initial primary stack region
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
// ToDo: make ENV_SIZE variable?
|
|
|
|
// ToDo: when B_BASE_ADDRESS is implemented, we could just allocate the stack from
|
|
|
|
// the bottom of the USER_STACK_REGION.
|
|
|
|
|
|
|
|
totalSize = PAGE_ALIGN(MAIN_THREAD_STACK_SIZE + TLS_SIZE + ENV_SIZE +
|
2003-01-12 19:29:28 +03:00
|
|
|
get_arguments_data_size(teamArgs->args, teamArgs->argc));
|
2003-01-07 12:40:59 +03:00
|
|
|
t->user_stack_base = USER_STACK_REGION + USER_STACK_REGION_SIZE - totalSize;
|
|
|
|
// the exact location at the end of the user stack region
|
|
|
|
|
|
|
|
sprintf(ustack_name, "%s_primary_stack", team->name);
|
2003-08-19 21:38:13 +04:00
|
|
|
t->user_stack_region_id = create_area_etc(team, ustack_name, (void **)&t->user_stack_base,
|
|
|
|
B_EXACT_ADDRESS, totalSize, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
2002-09-24 03:24:12 +04:00
|
|
|
if (t->user_stack_region_id < 0) {
|
2002-08-04 03:39:50 +04:00
|
|
|
panic("team_create_team2: could not create default user stack region\n");
|
|
|
|
return t->user_stack_region_id;
|
|
|
|
}
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
// now that the TLS area is allocated, initialize TLS
|
|
|
|
arch_thread_init_tls(t);
|
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
uspa = (struct uspace_program_args *)(t->user_stack_base + STACK_SIZE + TLS_SIZE + ENV_SIZE);
|
2002-08-04 03:39:50 +04:00
|
|
|
uargs = (char **)(uspa + 1);
|
2003-01-12 19:29:28 +03:00
|
|
|
udest = (char *)(uargs + teamArgs->argc + 1);
|
2002-08-04 03:39:50 +04:00
|
|
|
// dprintf("addr: stack base=0x%x uargs = 0x%x udest=0x%x tot_top_size=%d \n\n",t->user_stack_base,uargs,udest,tot_top_size);
|
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
for (arg_cnt = 0; arg_cnt < teamArgs->argc; arg_cnt++) {
|
2002-08-04 03:39:50 +04:00
|
|
|
uargs[arg_cnt] = udest;
|
2004-02-23 08:08:17 +03:00
|
|
|
udest += user_strlcpy(udest, teamArgs->args[arg_cnt], totalSize) + 1;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
uargs[arg_cnt] = NULL;
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
team->user_env_base = t->user_stack_base + STACK_SIZE + TLS_SIZE;
|
|
|
|
uenv = (char **)team->user_env_base;
|
|
|
|
udest = (char *)team->user_env_base + ENV_SIZE - 1;
|
2003-01-12 19:29:28 +03:00
|
|
|
// dprintf("team_create_team2: envc: %d, envp: 0x%p\n", teamArgs->envc, (void *)teamArgs->envp);
|
|
|
|
for (env_cnt = 0; env_cnt < teamArgs->envc; env_cnt++) {
|
2004-02-23 08:08:17 +03:00
|
|
|
size_t length = strlen(teamArgs->envp[env_cnt]) + 1;
|
|
|
|
udest -= length;
|
2002-08-04 03:39:50 +04:00
|
|
|
uenv[env_cnt] = udest;
|
2004-02-23 08:08:17 +03:00
|
|
|
user_memcpy(udest, teamArgs->envp[env_cnt], length);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
uenv[env_cnt] = NULL;
|
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
user_memcpy(uspa->program_name, team->name, sizeof(uspa->program_name));
|
|
|
|
user_memcpy(uspa->program_path, teamArgs->path, sizeof(uspa->program_path));
|
2002-08-04 03:39:50 +04:00
|
|
|
uspa->argc = arg_cnt;
|
|
|
|
uspa->argv = uargs;
|
|
|
|
uspa->envc = env_cnt;
|
|
|
|
uspa->envp = uenv;
|
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
if (teamArgs->args != NULL)
|
|
|
|
kfree_strings_array(teamArgs->args, teamArgs->argc);
|
|
|
|
if (teamArgs->envp != NULL)
|
|
|
|
kfree_strings_array(teamArgs->envp, teamArgs->envc);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
path = teamArgs->path;
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("team_create_team2: loading elf binary '%s'\n", path);
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
err = elf_load_uspace("/boot/libexec/rld.so", team, 0, &entry);
|
|
|
|
if (err < 0){
|
2002-08-04 03:39:50 +04:00
|
|
|
// XXX clean up team
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free the args
|
2003-01-12 19:29:28 +03:00
|
|
|
free(teamArgs->path);
|
|
|
|
free(teamArgs);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
dprintf("team_create_team2: loaded elf. entry = 0x%lx\n", entry);
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
team->state = TEAM_STATE_NORMAL;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
// jump to the entry point in user space
|
2003-04-18 13:38:28 +04:00
|
|
|
arch_thread_enter_uspace(t, entry, uspa, NULL);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
// never gets here
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
team_id
|
|
|
|
team_create_team(const char *path, const char *name, char **args, int argc, char **envp, int envc, int priority)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-01-07 12:40:59 +03:00
|
|
|
struct team *team;
|
2002-08-04 03:39:50 +04:00
|
|
|
thread_id tid;
|
|
|
|
team_id pid;
|
|
|
|
int err;
|
|
|
|
unsigned int state;
|
|
|
|
// int sem_retcode;
|
2003-01-12 19:29:28 +03:00
|
|
|
struct team_arg *teamArgs;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
dprintf("team_create_team: entry '%s', name '%s' args = %p argc = %d\n", path, name, args, argc);
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
team = create_team_struct(name, false);
|
|
|
|
if (team == NULL)
|
2002-08-04 03:39:50 +04:00
|
|
|
return ENOMEM;
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
pid = team->id;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
2003-01-07 12:40:59 +03:00
|
|
|
hash_insert(team_hash, team);
|
2002-08-04 03:39:50 +04:00
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
// copy the args over
|
2003-01-12 19:29:28 +03:00
|
|
|
teamArgs = (struct team_arg *)malloc(sizeof(struct team_arg));
|
|
|
|
if (teamArgs == NULL){
|
2002-08-04 03:39:50 +04:00
|
|
|
err = ENOMEM;
|
|
|
|
goto err1;
|
|
|
|
}
|
2003-01-12 19:29:28 +03:00
|
|
|
teamArgs->path = strdup(path);
|
|
|
|
if (teamArgs->path == NULL){
|
2002-08-04 03:39:50 +04:00
|
|
|
err = ENOMEM;
|
|
|
|
goto err2;
|
|
|
|
}
|
2003-01-12 19:29:28 +03:00
|
|
|
teamArgs->argc = argc;
|
|
|
|
teamArgs->args = args;
|
|
|
|
teamArgs->envp = envp;
|
|
|
|
teamArgs->envc = envc;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
// create a new io_context for this team
|
2003-01-07 12:40:59 +03:00
|
|
|
team->io_context = vfs_new_io_context(thread_get_current_thread()->team->io_context);
|
|
|
|
if (!team->io_context) {
|
2002-08-04 03:39:50 +04:00
|
|
|
err = ENOMEM;
|
|
|
|
goto err3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an address space for this team
|
2003-01-07 12:40:59 +03:00
|
|
|
team->_aspace_id = vm_create_aspace(team->name, USER_BASE, USER_SIZE, false);
|
|
|
|
if (team->_aspace_id < 0) {
|
|
|
|
err = team->_aspace_id;
|
2002-08-04 03:39:50 +04:00
|
|
|
goto err4;
|
|
|
|
}
|
2003-01-07 12:40:59 +03:00
|
|
|
team->aspace = vm_get_aspace_by_id(team->_aspace_id);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
// create a kernel thread, but under the context of the new team
|
2003-01-27 06:09:33 +03:00
|
|
|
tid = spawn_kernel_thread_etc(team_create_team2, name, B_NORMAL_PRIORITY, teamArgs, team->id);
|
2002-08-04 03:39:50 +04:00
|
|
|
if (tid < 0) {
|
|
|
|
err = tid;
|
|
|
|
goto err5;
|
|
|
|
}
|
|
|
|
|
2003-01-27 06:09:33 +03:00
|
|
|
resume_thread(tid);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
return pid;
|
|
|
|
|
|
|
|
err5:
|
2003-01-07 12:40:59 +03:00
|
|
|
vm_put_aspace(team->aspace);
|
|
|
|
vm_delete_aspace(team->_aspace_id);
|
2002-08-04 03:39:50 +04:00
|
|
|
err4:
|
2003-01-07 12:40:59 +03:00
|
|
|
vfs_free_io_context(team->io_context);
|
2002-08-04 03:39:50 +04:00
|
|
|
err3:
|
2003-01-12 19:29:28 +03:00
|
|
|
free(teamArgs->path);
|
2002-08-04 03:39:50 +04:00
|
|
|
err2:
|
2003-01-12 19:29:28 +03:00
|
|
|
free(teamArgs);
|
2002-08-04 03:39:50 +04:00
|
|
|
err1:
|
|
|
|
// remove the team structure from the team hash table and delete the team structure
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
2003-01-07 12:40:59 +03:00
|
|
|
hash_remove(team_hash, team);
|
2002-08-04 03:39:50 +04:00
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
2003-01-07 12:40:59 +03:00
|
|
|
delete_team_struct(team);
|
2002-08-04 03:39:50 +04:00
|
|
|
//err:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
2004-03-03 03:57:00 +03:00
|
|
|
// #pragma mark -
|
|
|
|
// public team API
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
wait_for_team(team_id id, status_t *_returnCode)
|
|
|
|
{
|
|
|
|
struct team *team;
|
|
|
|
thread_id thread;
|
|
|
|
cpu_status state;
|
|
|
|
|
|
|
|
// find main thread and wait for that
|
|
|
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
|
|
|
|
|
|
|
team = team_get_team_struct_locked(id);
|
|
|
|
if (team && team->main_thread)
|
|
|
|
thread = team->main_thread->id;
|
|
|
|
else
|
|
|
|
thread = B_BAD_THREAD_ID;
|
|
|
|
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
if (thread < 0)
|
|
|
|
return thread;
|
|
|
|
|
|
|
|
return wait_for_thread(thread, _returnCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
kill_team(team_id id)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
int state;
|
2003-01-07 12:40:59 +03:00
|
|
|
struct team *team;
|
2002-08-04 03:39:50 +04:00
|
|
|
// struct thread *t;
|
|
|
|
thread_id tid = -1;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
|
|
|
|
2003-01-07 12:40:59 +03:00
|
|
|
team = team_get_team_struct_locked(id);
|
|
|
|
if (team != NULL)
|
|
|
|
tid = team->main_thread->id;
|
|
|
|
else
|
2004-02-23 07:12:34 +03:00
|
|
|
retval = B_BAD_THREAD_ID;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
2003-01-07 12:40:59 +03:00
|
|
|
|
|
|
|
if (retval < 0)
|
2002-08-04 03:39:50 +04:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
// just kill the main thread in the team. The cleanup code there will
|
|
|
|
// take care of the team
|
2004-03-03 03:57:00 +03:00
|
|
|
return kill_thread(tid);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-12 19:29:28 +03:00
|
|
|
/** Fills the team_info structure with information from the specified
|
|
|
|
* team.
|
2004-02-23 07:08:09 +03:00
|
|
|
* The team lock must be held when called.
|
2003-01-12 19:29:28 +03:00
|
|
|
*/
|
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
static status_t
|
|
|
|
fill_team_info(struct team *team, team_info *info, size_t size)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2002-12-03 17:17:53 +03:00
|
|
|
if (size != sizeof(team_info))
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
// ToDo: Set more informations for team_info
|
|
|
|
memset(info, 0, size);
|
|
|
|
|
|
|
|
info->team = team->id;
|
|
|
|
info->thread_count = team->num_threads;
|
2003-01-12 19:29:28 +03:00
|
|
|
info->image_count = count_images(team);
|
2002-12-03 17:17:53 +03:00
|
|
|
//info->area_count =
|
|
|
|
//info->debugger_nub_thread =
|
|
|
|
//info->debugger_nub_port =
|
|
|
|
//info->argc =
|
|
|
|
//info->args[64] =
|
|
|
|
//info->uid =
|
|
|
|
//info->gid =
|
|
|
|
|
|
|
|
// ToDo: make this to return real argc/argv
|
|
|
|
strlcpy(info->args, team->name, sizeof(info->args));
|
|
|
|
info->argc = 1;
|
|
|
|
|
|
|
|
return B_OK;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
_get_team_info(team_id id, team_info *info, size_t size)
|
|
|
|
{
|
|
|
|
int state;
|
|
|
|
status_t rc = B_OK;
|
|
|
|
struct team *team;
|
2004-02-23 07:08:09 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
2004-02-23 07:08:09 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
team = team_get_team_struct_locked(id);
|
|
|
|
if (!team) {
|
|
|
|
rc = B_BAD_TEAM_ID;
|
|
|
|
goto err;
|
|
|
|
}
|
2002-12-03 17:17:53 +03:00
|
|
|
|
|
|
|
rc = fill_team_info(team, info, size);
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
err:
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
_get_next_team_info(int32 *cookie, team_info *info, size_t size)
|
|
|
|
{
|
2002-12-03 17:17:53 +03:00
|
|
|
status_t status = B_BAD_TEAM_ID;
|
2002-08-04 03:39:50 +04:00
|
|
|
struct team *team = NULL;
|
2002-12-03 17:17:53 +03:00
|
|
|
int32 slot = *cookie;
|
|
|
|
|
|
|
|
int state = disable_interrupts();
|
2002-08-04 03:39:50 +04:00
|
|
|
GRAB_TEAM_LOCK();
|
2002-12-03 17:17:53 +03:00
|
|
|
|
|
|
|
if (slot >= next_team_id)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
// get next valid team
|
2002-08-05 00:10:06 +04:00
|
|
|
while ((slot < next_team_id) && !(team = team_get_team_struct_locked(slot)))
|
2002-08-04 03:39:50 +04:00
|
|
|
slot++;
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
if (team) {
|
2002-12-03 17:17:53 +03:00
|
|
|
status = fill_team_info(team, info, size);
|
|
|
|
*cookie = ++slot;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
err:
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
return status;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
sys_setenv(const char *name, const char *value, int overwrite)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
char var[SYS_THREAD_STRING_LENGTH_MAX];
|
|
|
|
int state;
|
|
|
|
addr env_space;
|
|
|
|
char **envp;
|
|
|
|
int envc;
|
|
|
|
bool var_exists = false;
|
|
|
|
int var_pos = 0;
|
|
|
|
int name_size;
|
|
|
|
int rc = 0;
|
|
|
|
int i;
|
|
|
|
char *p;
|
2003-01-27 06:09:33 +03:00
|
|
|
|
|
|
|
// ToDo: please put me out of the kernel into libroot.so!
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("sys_setenv: entry (name=%s, value=%s)\n", name, value);
|
2003-01-27 06:09:33 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
if (strlen(name) + strlen(value) + 1 >= SYS_THREAD_STRING_LENGTH_MAX)
|
|
|
|
return -1;
|
2003-01-27 06:09:33 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
2003-01-27 06:09:33 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
strcpy(var, name);
|
|
|
|
strncat(var, "=", SYS_THREAD_STRING_LENGTH_MAX-1);
|
|
|
|
name_size = strlen(var);
|
|
|
|
strncat(var, value, SYS_THREAD_STRING_LENGTH_MAX-1);
|
2003-01-27 06:09:33 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
env_space = (addr)thread_get_current_thread()->team->user_env_base;
|
|
|
|
envp = (char **)env_space;
|
2003-01-27 06:09:33 +03:00
|
|
|
for (envc = 0; envp[envc]; envc++) {
|
2002-08-04 03:39:50 +04:00
|
|
|
if (!strncmp(envp[envc], var, name_size)) {
|
|
|
|
var_exists = true;
|
|
|
|
var_pos = envc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!var_exists)
|
|
|
|
var_pos = envc;
|
2003-01-27 06:09:33 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
dprintf("sys_setenv: variable does%s exist\n", var_exists ? "" : " not");
|
|
|
|
if ((!var_exists) || (var_exists && overwrite)) {
|
|
|
|
// XXX- make a better allocator
|
|
|
|
if (var_exists) {
|
|
|
|
if (strlen(var) <= strlen(envp[var_pos])) {
|
|
|
|
strcpy(envp[var_pos], var);
|
2003-01-27 06:09:33 +03:00
|
|
|
} else {
|
|
|
|
for (p = (char *)env_space + ENV_SIZE - 1, i = 0; envp[i]; i++)
|
2002-08-04 03:39:50 +04:00
|
|
|
if (envp[i] < p)
|
|
|
|
p = envp[i];
|
|
|
|
p -= (strlen(var) + 1);
|
|
|
|
if (p < (char *)env_space + (envc * sizeof(char *))) {
|
|
|
|
rc = -1;
|
2003-01-27 06:09:33 +03:00
|
|
|
} else {
|
2002-08-04 03:39:50 +04:00
|
|
|
envp[var_pos] = p;
|
|
|
|
strcpy(envp[var_pos], var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-01-27 06:09:33 +03:00
|
|
|
for (p = (char *)env_space + ENV_SIZE - 1, i=0; envp[i]; i++)
|
2002-08-04 03:39:50 +04:00
|
|
|
if (envp[i] < p)
|
|
|
|
p = envp[i];
|
|
|
|
p -= (strlen(var) + 1);
|
|
|
|
if (p < (char *)env_space + ((envc + 1) * sizeof(char *))) {
|
|
|
|
rc = -1;
|
2003-01-27 06:09:33 +03:00
|
|
|
} else {
|
2002-08-04 03:39:50 +04:00
|
|
|
envp[envc] = p;
|
|
|
|
strcpy(envp[envc], var);
|
|
|
|
envp[envc + 1] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dprintf("sys_setenv: variable set.\n");
|
|
|
|
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:26:52 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
sys_getenv(const char *name, char **value)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
|
|
|
char **envp;
|
|
|
|
char *p;
|
|
|
|
int state;
|
|
|
|
int i;
|
|
|
|
int len = strlen(name);
|
|
|
|
int rc = -1;
|
2003-01-27 06:09:33 +03:00
|
|
|
|
|
|
|
// ToDo: please put me out of the kernel into libroot.so!
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_TEAM_LOCK();
|
|
|
|
|
|
|
|
envp = (char **)thread_get_current_thread()->team->user_env_base;
|
2003-01-27 06:09:33 +03:00
|
|
|
for (i = 0; envp[i]; i++) {
|
2002-08-04 03:39:50 +04:00
|
|
|
if (!strncmp(envp[i], name, len)) {
|
|
|
|
p = envp[i] + len;
|
|
|
|
if (*p == '=') {
|
|
|
|
*value = (p + 1);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE_TEAM_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
// #pragma mark -
|
2004-03-03 03:57:00 +03:00
|
|
|
// User syscalls
|
2002-12-03 17:17:53 +03:00
|
|
|
|
|
|
|
|
2003-01-27 06:09:33 +03:00
|
|
|
status_t
|
2004-03-03 03:57:00 +03:00
|
|
|
_user_wait_for_team(team_id id, status_t *_userReturnCode)
|
2002-12-03 17:17:53 +03:00
|
|
|
{
|
2003-01-27 06:09:33 +03:00
|
|
|
status_t returnCode;
|
|
|
|
status_t status;
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(_userReturnCode))
|
2002-12-03 17:17:53 +03:00
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
2003-01-27 06:09:33 +03:00
|
|
|
status = wait_for_team(id, &returnCode);
|
2002-12-03 17:17:53 +03:00
|
|
|
if (status >= B_OK) {
|
2003-01-27 06:09:33 +03:00
|
|
|
if (user_memcpy(_userReturnCode, &returnCode, sizeof(returnCode)) < B_OK)
|
2002-12-03 17:17:53 +03:00
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
team_id
|
2004-03-03 03:57:00 +03:00
|
|
|
_user_create_team(const char *userPath, const char *userName, char **userArgs,
|
|
|
|
int argCount, char **userEnv, int envCount, int priority)
|
2002-12-03 17:17:53 +03:00
|
|
|
{
|
|
|
|
char path[SYS_MAX_PATH_LEN];
|
2004-02-23 07:08:09 +03:00
|
|
|
char name[B_OS_NAME_LENGTH];
|
2003-11-12 18:37:44 +03:00
|
|
|
char **args = NULL;
|
|
|
|
char **env = NULL;
|
2002-12-03 17:17:53 +03:00
|
|
|
int rc;
|
|
|
|
|
2003-11-12 18:37:44 +03:00
|
|
|
TRACE(("user_team_create_team: argc = %d\n", argCount));
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(userPath)
|
|
|
|
|| !IS_USER_ADDRESS(userName))
|
2003-11-12 18:37:44 +03:00
|
|
|
return B_BAD_ADDRESS;
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2003-11-12 18:37:44 +03:00
|
|
|
rc = user_copy_strings_array(userArgs, argCount, &args);
|
2002-12-03 17:17:53 +03:00
|
|
|
if (rc < 0)
|
|
|
|
goto error;
|
|
|
|
|
2003-11-12 18:37:44 +03:00
|
|
|
if (userEnv == NULL) {
|
|
|
|
// ToDo: this doesn't look particularly safe to me - where
|
|
|
|
// is user_env_base?
|
|
|
|
userEnv = (char **)thread_get_current_thread()->team->user_env_base;
|
|
|
|
for (envCount = 0; userEnv && (userEnv[envCount]); envCount++);
|
|
|
|
}
|
|
|
|
if (user_copy_strings_array(userEnv, envCount, &env) < B_OK
|
|
|
|
|| user_strlcpy(path, userPath, SYS_MAX_PATH_LEN) < B_OK
|
2004-02-23 07:08:09 +03:00
|
|
|
|| user_strlcpy(name, userName, B_OS_NAME_LENGTH) < B_OK) {
|
2003-11-12 18:37:44 +03:00
|
|
|
rc = B_BAD_ADDRESS;
|
2002-12-03 17:17:53 +03:00
|
|
|
goto error;
|
2003-11-12 18:37:44 +03:00
|
|
|
}
|
2002-12-03 17:17:53 +03:00
|
|
|
|
2003-11-12 18:37:44 +03:00
|
|
|
return team_create_team(path, name, args, argCount, env, envCount, priority);
|
2002-12-03 17:17:53 +03:00
|
|
|
|
|
|
|
error:
|
2003-11-12 18:37:44 +03:00
|
|
|
kfree_strings_array(args, argCount);
|
|
|
|
kfree_strings_array(env, envCount);
|
2002-12-03 17:17:53 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-03-03 03:57:00 +03:00
|
|
|
_user_kill_team(team_id team)
|
|
|
|
{
|
|
|
|
return kill_team(team);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
_user_get_team_info(team_id id, team_info *userInfo)
|
2002-12-03 17:17:53 +03:00
|
|
|
{
|
2003-11-12 18:37:44 +03:00
|
|
|
status_t status;
|
|
|
|
team_info info;
|
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(userInfo))
|
2003-11-12 18:37:44 +03:00
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
status = _get_team_info(id, &info, sizeof(team_info));
|
|
|
|
if (status == B_OK) {
|
|
|
|
if (user_memcpy(userInfo, &info, sizeof(team_info)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2002-12-03 17:17:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-03-03 03:57:00 +03:00
|
|
|
_user_get_next_team_info(int32 *userCookie, team_info *userInfo)
|
2002-12-03 17:17:53 +03:00
|
|
|
{
|
2003-11-12 18:37:44 +03:00
|
|
|
status_t status;
|
|
|
|
team_info info;
|
|
|
|
int32 cookie;
|
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(userCookie)
|
|
|
|
|| !IS_USER_ADDRESS(userInfo)
|
2003-11-12 18:37:44 +03:00
|
|
|
|| user_memcpy(&cookie, userCookie, sizeof(int32)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
status = _get_next_team_info(&cookie, &info, sizeof(team_info));
|
|
|
|
if (status != B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (user_memcpy(userCookie, &cookie, sizeof(int32)) < B_OK
|
|
|
|
|| user_memcpy(userInfo, &info, sizeof(team_info)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
return status;
|
2002-12-03 17:17:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-03 03:57:00 +03:00
|
|
|
team_id
|
|
|
|
_user_get_current_team(void)
|
|
|
|
{
|
|
|
|
return team_get_current_team_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-03 17:17:53 +03:00
|
|
|
int
|
|
|
|
user_getenv(const char *userName, char **_userValue)
|
|
|
|
{
|
|
|
|
char name[SYS_THREAD_STRING_LENGTH_MAX];
|
|
|
|
char *value;
|
|
|
|
int rc;
|
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(userName)
|
|
|
|
|| !IS_USER_ADDRESS(_userValue)
|
2002-12-03 17:17:53 +03:00
|
|
|
|| user_strlcpy(name, userName, SYS_THREAD_STRING_LENGTH_MAX) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
rc = sys_getenv(name, &value);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (user_memcpy(_userValue, &value, sizeof(char *)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
user_setenv(const char *userName, const char *userValue, int overwrite)
|
|
|
|
{
|
|
|
|
char name[SYS_THREAD_STRING_LENGTH_MAX];
|
|
|
|
char value[SYS_THREAD_STRING_LENGTH_MAX];
|
|
|
|
|
2004-02-22 17:52:59 +03:00
|
|
|
if (!IS_USER_ADDRESS(userName)
|
|
|
|
|| !IS_USER_ADDRESS(userValue)
|
2002-12-03 17:17:53 +03:00
|
|
|
|| user_strlcpy(name, userName, SYS_THREAD_STRING_LENGTH_MAX) < B_OK
|
|
|
|
|| user_strlcpy(value, userValue, SYS_THREAD_STRING_LENGTH_MAX) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
return sys_setenv(name, value, overwrite);
|
|
|
|
}
|
|
|
|
|