Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions common/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* Common libphoenix utilities
*
* Copyright 2026 Phoenix Systems
* Author: Julian Uziembło
*
* %LICENSE%
*/

#ifndef _LIBPHOENIX_COMMON_UTIL_H_
#define _LIBPHOENIX_COMMON_UTIL_H_

#include <stdlib.h>
#include <string.h>


static inline size_t __strlenNull(const char *str)
{
return (str == NULL) ? 0 : strlen(str);
}


#endif /* _LIBPHOENIX_COMMON_UTIL_H_ */
4 changes: 4 additions & 0 deletions include/posix/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef _LIBPHOENIX_POSIX_UTILS_H_
#define _LIBPHOENIX_POSIX_UTILS_H_

#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -25,6 +26,9 @@ extern "C" {
extern int create_dev(oid_t *oid, const char *path);


extern int destroy_dev(const char *path);


extern void splitname(char *path, char **base, char **dir);


Expand Down
2 changes: 2 additions & 0 deletions include/sys/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extern void portDestroy(uint32_t port);

extern int portRegister(uint32_t port, const char *name, oid_t *oid);

extern int portUnregister(const char *name);

extern int lookup(const char *name, oid_t *file, oid_t *dev);


Expand Down
2 changes: 1 addition & 1 deletion sys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#

OBJS += $(addprefix $(PREFIX_O)sys/, events.o ioctl.o list.o mount.o rb.o resource.o select.o \
semaphore.o socket.o stat.o statvfs.o threads.o time.o times.o wait.o uio.o proto.o mman.o uname.o perf.o)
semaphore.o socket.o stat.o statvfs.o threads.o time.o times.o wait.o uio.o proto.o mman.o uname.o perf.o msg.o)
31 changes: 31 additions & 0 deletions sys/msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* sys/msg
*
* Copyright 2026 Phoenix Systems
* Author: Julian Uziembło
*
* %LICENSE%
*/

#include "../common/util.h"


extern int sys_portRegister(uint32_t port, const char *name, size_t len, oid_t *oid);

extern int sys_portUnregister(const char *name, size_t len);


int portRegister(uint32_t port, const char *name, oid_t *oid)
{
return sys_portRegister(port, name, __strlenNull(name), oid);
}


int portUnregister(const char *name)
{
return sys_portUnregister(name, __strlenNull(name));
}
112 changes: 112 additions & 0 deletions unistd/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,118 @@ int create_dev(oid_t *oid, const char *path)
}


int destroy_dev(const char *path)
{
static int nodevfs = 0;

oid_t oid, odev;
int retry = 0, err;
char *canonical_path, *dir, *sep, *tpathalloc = NULL;
const char *tpath = path;

if (path == NULL) {
return -EINVAL;
}

while (lookup("devfs", NULL, &odev) < 0) {
/* if remove_dev() is called by anyone started from syspage devfs
* may be not registered yet so we try 3 times until we give up */
if (++retry > 3 || nodevfs != 0) {
nodevfs = 1;

if (lookup("/dev", NULL, &odev) < 0) {
/* Looks like we don't have a filesystem.
* Fall back to portUnregister. */
if (*path != '/') {
/* Move point to /dev */
tpathalloc = malloc(strlen(path) + 6);
if (tpathalloc == NULL) {
return -ENOMEM;
}
strcpy(tpathalloc, "/dev/");
strcat(tpathalloc, path);
tpath = tpathalloc;
}

err = portUnregister(tpath);
free(tpathalloc);
return err;
}
break;
}
else {
usleep(100000);
}
}

if (strncmp("/dev/", path, 5) == 0) {
canonical_path = strdup(path);
}
else {
size_t len = 5 + strlen(path) + 1;
canonical_path = malloc(len);
snprintf(canonical_path, len, "/dev/%s", path);
}

if (lookup(canonical_path, NULL, &oid) < 0) {
return -ENODEV;
}

dir = canonical_path + 5;

for (;;) {
sep = strchr(dir, '/');
if (sep == NULL) {
break;
}
*sep = '\0';

msg_t msg;
msg.type = mtLookup;
msg.oid = odev;
msg.i.size = strlen(dir) + 1;
msg.i.data = dir;

err = msgSend(odev.port, &msg);
if (err < 0) {
free(canonical_path);
return err;
}

if (msg.o.err >= 0) {
odev = msg.o.lookup.dev;
}
else {
free(canonical_path);
return msg.o.err;
}

do {
sep++;
} while (*sep == '/');
dir = sep;
}

path = dir;

msg_t msg;
msg.type = mtUnlink,
msg.oid = odev,
msg.i.ln.oid = oid,
msg.i.data = path,
msg.i.size = strlen(path) + 1,

err = msgSend(odev.port, &msg);
if (err < 0) {
return err;
}

free(canonical_path);

return msg.o.err;
}


extern int sys_fcntl(int fd, int cmd, unsigned val);


Expand Down
Loading