Replaced team_get_team_struct() with its safer friend team_is_valid().

Replaced SYS_MAX_OS_NAME_LEN with B_OS_NAME_LENGTH.
Banned strncpy().
Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6689 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2004-02-23 04:08:09 +00:00
parent 3402b15625
commit 48f31b9581
+32 -30
View File
@@ -179,18 +179,18 @@ kfree_strings_array(char **strings, int strc)
*/ */
static int static int
user_copy_strings_array(char **strings, int strc, char ***kstrings) user_copy_strings_array(char **userStrings, int strc, char ***kstrings)
{ {
char **lstrings; char **lstrings;
int err; int err;
int cnt; int cnt;
char *source; char *source;
char buf[SYS_THREAD_STRING_LENGTH_MAX]; char buffer[SYS_THREAD_STRING_LENGTH_MAX];
*kstrings = NULL; *kstrings = NULL;
if ((addr)strings >= KERNEL_BASE && (addr)strings <= KERNEL_TOP) if (!IS_USER_ADDRESS(userStrings))
return ERR_VM_BAD_USER_MEMORY; return B_BAD_ADDRESS;
lstrings = (char **)malloc((strc + 1) * sizeof(char *)); lstrings = (char **)malloc((strc + 1) * sizeof(char *));
if (lstrings == NULL) if (lstrings == NULL)
@@ -199,21 +199,20 @@ user_copy_strings_array(char **strings, int strc, char ***kstrings)
// scan all strings and copy to kernel space // scan all strings and copy to kernel space
for (cnt = 0; cnt < strc; cnt++) { for (cnt = 0; cnt < strc; cnt++) {
err = user_memcpy(&source, &(strings[cnt]), sizeof(char *)); err = user_memcpy(&source, &(userStrings[cnt]), sizeof(char *));
if(err < 0) if (err < 0)
goto error; goto error;
if ((addr)source >= KERNEL_BASE && (addr)source <= KERNEL_TOP){ if (!IS_USER_ADDRESS(source)) {
err = ERR_VM_BAD_USER_MEMORY; err = B_BAD_ADDRESS;
goto error; goto error;
} }
err = user_strncpy(buf, source, SYS_THREAD_STRING_LENGTH_MAX - 1); err = user_strlcpy(buffer, source, SYS_THREAD_STRING_LENGTH_MAX);
if (err < 0) if (err < 0)
goto error; goto error;
buf[SYS_THREAD_STRING_LENGTH_MAX - 1] = 0;
lstrings[cnt] = strdup(buf); lstrings[cnt] = strdup(buffer);
if (lstrings[cnt] == NULL){ if (lstrings[cnt] == NULL){
err = ENOMEM; err = ENOMEM;
goto error; goto error;
@@ -260,21 +259,27 @@ wait_for_team(team_id id, status_t *_returnCode)
} }
struct team * /** Quick check to see if we have a valid team ID.
team_get_team_struct(team_id id) */
bool
team_is_valid(team_id id)
{ {
struct team *p; struct team *team;
int state; int state;
if (id <= 0)
return false;
state = disable_interrupts(); state = disable_interrupts();
GRAB_TEAM_LOCK(); GRAB_TEAM_LOCK();
p = team_get_team_struct_locked(id); team = team_get_team_struct_locked(id);
RELEASE_TEAM_LOCK(); RELEASE_TEAM_LOCK();
restore_interrupts(state); restore_interrupts(state);
return p; return team != NULL;
} }
@@ -349,15 +354,13 @@ team_get_current_team_id(void)
static struct team * static struct team *
create_team_struct(const char *name, bool kernel) create_team_struct(const char *name, bool kernel)
{ {
struct team *team; struct team *team = (struct team *)malloc(sizeof(struct team));
team = (struct team *)malloc(sizeof(struct team));
if (team == NULL) if (team == NULL)
goto error; return NULL;
team->next = team->siblings_next = team->children = team->parent = NULL;
team->id = atomic_add(&next_team_id, 1); team->id = atomic_add(&next_team_id, 1);
strncpy(&team->name[0], name, SYS_MAX_OS_NAME_LEN-1); strlcpy(team->name, name, B_OS_NAME_LENGTH);
team->name[SYS_MAX_OS_NAME_LEN-1] = 0;
team->num_threads = 0; team->num_threads = 0;
team->io_context = NULL; team->io_context = NULL;
team->_aspace_id = -1; team->_aspace_id = -1;
@@ -373,13 +376,12 @@ create_team_struct(const char *name, bool kernel)
list_init(&team->image_list); list_init(&team->image_list);
if (arch_team_init_team_struct(team, kernel) < 0) if (arch_team_init_team_struct(team, kernel) < 0)
goto error1; goto error;
return team; return team;
error1:
free(team);
error: error:
free(team);
return NULL; return NULL;
} }
@@ -630,7 +632,7 @@ team_kill_team(team_id id)
/** Fills the team_info structure with information from the specified /** Fills the team_info structure with information from the specified
* team. * team.
* Team lock must be hold when called. * The team lock must be held when called.
*/ */
static status_t static status_t
@@ -667,10 +669,10 @@ _get_team_info(team_id id, team_info *info, size_t size)
int state; int state;
status_t rc = B_OK; status_t rc = B_OK;
struct team *team; struct team *team;
state = disable_interrupts(); state = disable_interrupts();
GRAB_TEAM_LOCK(); GRAB_TEAM_LOCK();
team = team_get_team_struct_locked(id); team = team_get_team_struct_locked(id);
if (!team) { if (!team) {
rc = B_BAD_TEAM_ID; rc = B_BAD_TEAM_ID;
@@ -861,7 +863,7 @@ user_team_create_team(const char *userPath, const char *userName,
char **userArgs, int argCount, char **userEnv, int envCount, int priority) char **userArgs, int argCount, char **userEnv, int envCount, int priority)
{ {
char path[SYS_MAX_PATH_LEN]; char path[SYS_MAX_PATH_LEN];
char name[SYS_MAX_OS_NAME_LEN]; char name[B_OS_NAME_LENGTH];
char **args = NULL; char **args = NULL;
char **env = NULL; char **env = NULL;
int rc; int rc;
@@ -884,7 +886,7 @@ user_team_create_team(const char *userPath, const char *userName,
} }
if (user_copy_strings_array(userEnv, envCount, &env) < B_OK if (user_copy_strings_array(userEnv, envCount, &env) < B_OK
|| user_strlcpy(path, userPath, SYS_MAX_PATH_LEN) < B_OK || user_strlcpy(path, userPath, SYS_MAX_PATH_LEN) < B_OK
|| user_strlcpy(name, userName, SYS_MAX_OS_NAME_LEN) < B_OK) { || user_strlcpy(name, userName, B_OS_NAME_LENGTH) < B_OK) {
rc = B_BAD_ADDRESS; rc = B_BAD_ADDRESS;
goto error; goto error;
} }