9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit a074501aeb8c9764eb13c50813cf51fcf5142792
parent 44a2f95676a50d4f9503f994064f3b52cb072a5f
Author: Dimitris Papastamos <dimitris.papastamos@arm.com>
Date:   Tue, 19 Feb 2019 11:27:14 +0000

Take a lock when accessing fdset[]

Change-Id: I143a33ac043c921ef156cdcb4bfd59ca51823f9d
Signed-off-by: Dimitris Papastamos <dimitris.papastamos@arm.com>

Diffstat:
Mdrivers/dev.c | 31++++++++++++++++++-------------
1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/drivers/dev.c b/drivers/dev.c @@ -9,20 +9,27 @@ static Chan fdset[NR_CHANS]; static Chan slash; +static mutex_t chanlock; static Chan * -newchan(void) +newchan(unsigned char type) { - Chan *c, *lim; + Chan *c = NULL; + int i; - lim = &fdset[NR_CHANS]; - for (c = fdset; c < lim; c++) { - if (c->type == NODEV) - return c; + lock(&chanlock); + for (i = 0; i < NR_CHANS; i++) { + if (fdset[i].type == NODEV) { + c = &fdset[i]; + c->type = type; + break; + } } + unlock(&chanlock); - errno = ENOMEM; - return NULL; + if (!c) + errno = ENOMEM; + return c; } static Chan * @@ -105,9 +112,9 @@ static void chanclose(Chan *c) { c->qid = 0; - c->type = NODEV; c->dev = 0; c->offset = 0; + c->type = NODEV; } Chan * @@ -163,11 +170,10 @@ notfound: Chan * devclone(Chan *c, Chan *nc) { - if (!nc && (nc = newchan()) == NULL) + if (!nc && (nc = newchan(c->type)) == NULL) return NULL; nc->qid = c->qid; - nc->type = c->type; nc->dev = c->dev; nc->mode = c->mode; nc->offset = c->offset; @@ -183,10 +189,9 @@ devattach(const char *spec, int id) if ((type = devtype(id)) < 0) return NULL; - if ((c = newchan()) == NULL) + if ((c = newchan(devtype(id))) == NULL) return NULL; c->qid = CHDIR; - c->type = devtype(id); return c; }