Skip to content

Commit 479fab7

Browse files
yangpengyaRbb666
authored andcommitted
[ipc]修复pipe创建失败时的资源异常释放
1 parent 69be78f commit 479fab7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

components/drivers/ipc/pipe.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ rt_ssize_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, r
557557
}
558558

559559
pbuf = (uint8_t*)buffer;
560-
rt_mutex_take(&pipe->lock, -1);
560+
rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
561561

562562
while (write_bytes < count)
563563
{
@@ -619,6 +619,14 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
619619
rt_pipe_t *pipe;
620620
rt_device_t dev;
621621

622+
RT_ASSERT(name != RT_NULL);
623+
RT_ASSERT(bufsz < 0xFFFF);
624+
625+
if (rt_device_find(name) != RT_NULL)
626+
{
627+
/* pipe device has been created */
628+
return RT_NULL;
629+
}
622630
pipe = (rt_pipe_t *)rt_malloc(sizeof(rt_pipe_t));
623631
if (pipe == RT_NULL) return RT_NULL;
624632

@@ -635,7 +643,6 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
635643
pipe->writer = 0;
636644
pipe->reader = 0;
637645

638-
RT_ASSERT(bufsz < 0xFFFF);
639646
pipe->bufsz = bufsz;
640647

641648
dev = &pipe->parent;
@@ -654,15 +661,8 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
654661
dev->rx_indicate = RT_NULL;
655662
dev->tx_complete = RT_NULL;
656663

657-
if (rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
658-
{
659-
rt_mutex_detach(&pipe->lock);
660-
#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
661-
resource_id_put(&id_mgr, pipe->pipeno);
662-
#endif
663-
rt_free(pipe);
664-
return RT_NULL;
665-
}
664+
rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
665+
666666
#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE)
667667
dev->fops = (void *)&pipe_fops;
668668
#endif
@@ -762,13 +762,15 @@ int pipe(int fildes[2])
762762
fildes[1] = open(dev_name, O_WRONLY, 0);
763763
if (fildes[1] < 0)
764764
{
765-
close(fildes[0]);
765+
rt_pipe_delete(dname);
766766
return -1;
767767
}
768768

769769
fildes[0] = open(dev_name, O_RDONLY, 0);
770770
if (fildes[0] < 0)
771771
{
772+
close(fildes[1]);
773+
rt_pipe_delete(dname);
772774
return -1;
773775
}
774776

0 commit comments

Comments
 (0)