Skip to content

Commit b68bdf2

Browse files
dnejezchlebcarlescufi
authored andcommitted
lib/os: fdtable: add locking to posix api
Added locking to posix read(), write(), close() for additional protection. In read() missing lock would create uneven calls to locking mechanism in sockets.c after k_condvar_wait(). That results in socket lock not ever being unlocked Signed-off-by: Daniel Nejezchleb <[email protected]>
1 parent 1f22a2b commit b68bdf2

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/os/fdtable.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,37 @@ int z_alloc_fd(void *obj, const struct fd_op_vtable *vtable)
224224

225225
ssize_t read(int fd, void *buf, size_t sz)
226226
{
227+
ssize_t res;
228+
227229
if (_check_fd(fd) < 0) {
228230
return -1;
229231
}
230232

231-
return fdtable[fd].vtable->read(fdtable[fd].obj, buf, sz);
233+
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
234+
235+
res = fdtable[fd].vtable->read(fdtable[fd].obj, buf, sz);
236+
237+
k_mutex_unlock(&fdtable[fd].lock);
238+
239+
return res;
232240
}
233241
FUNC_ALIAS(read, _read, ssize_t);
234242

235243
ssize_t write(int fd, const void *buf, size_t sz)
236244
{
245+
ssize_t res;
246+
237247
if (_check_fd(fd) < 0) {
238248
return -1;
239249
}
240250

241-
return fdtable[fd].vtable->write(fdtable[fd].obj, buf, sz);
251+
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
252+
253+
res = fdtable[fd].vtable->write(fdtable[fd].obj, buf, sz);
254+
255+
k_mutex_unlock(&fdtable[fd].lock);
256+
257+
return res;
242258
}
243259
FUNC_ALIAS(write, _write, ssize_t);
244260

@@ -250,8 +266,12 @@ int close(int fd)
250266
return -1;
251267
}
252268

269+
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
270+
253271
res = fdtable[fd].vtable->close(fdtable[fd].obj);
254272

273+
k_mutex_unlock(&fdtable[fd].lock);
274+
255275
z_free_fd(fd);
256276

257277
return res;

0 commit comments

Comments
 (0)