|
@@ -1,4 +1,5 @@
|
|
|
-
|
|
|
+
|
|
|
+ * mode:auto-fill; fill-column:78; -*- */
|
|
|
|
|
|
|
|
|
|
|
@@ -24,27 +25,27 @@
|
|
|
* "dev:".
|
|
|
*/
|
|
|
|
|
|
-#include "pal_defs.h"
|
|
|
-#include "pal_linux_defs.h"
|
|
|
+#include "api.h"
|
|
|
#include "pal.h"
|
|
|
-#include "pal_internal.h"
|
|
|
-#include "pal_linux.h"
|
|
|
#include "pal_debug.h"
|
|
|
+#include "pal_defs.h"
|
|
|
#include "pal_error.h"
|
|
|
-#include "api.h"
|
|
|
+#include "pal_internal.h"
|
|
|
+#include "pal_linux.h"
|
|
|
+#include "pal_linux_defs.h"
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
typedef __kernel_pid_t pid_t;
|
|
|
-#include <linux/stat.h>
|
|
|
-#include <asm/stat.h>
|
|
|
-#include <asm/fcntl.h>
|
|
|
#include <asm/errno.h>
|
|
|
+#include <asm/fcntl.h>
|
|
|
+#include <asm/stat.h>
|
|
|
+#include <linux/stat.h>
|
|
|
|
|
|
-#define DEVICE_OPS(handle) \
|
|
|
- ({ int _type = (handle)->dev.dev_type; \
|
|
|
- (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
|
|
|
- NULL : pal_device_ops[_type]; \
|
|
|
- })
|
|
|
+#define DEVICE_OPS(handle) \
|
|
|
+ ({ \
|
|
|
+ int _type = (handle)->dev.dev_type; \
|
|
|
+ (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? NULL : pal_device_ops[_type]; \
|
|
|
+ })
|
|
|
|
|
|
enum {
|
|
|
device_type_none = 0,
|
|
@@ -54,20 +55,18 @@ enum {
|
|
|
|
|
|
static struct handle_ops term_ops;
|
|
|
|
|
|
-static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
|
|
|
- NULL,
|
|
|
- &term_ops,
|
|
|
- };
|
|
|
+static const struct handle_ops* pal_device_ops[PAL_DEVICE_TYPE_BOUND] = {
|
|
|
+ NULL, &term_ops,
|
|
|
+};
|
|
|
|
|
|
|
|
|
for stream handler wich will open or access the device */
|
|
|
-static int parse_device_uri (const char ** uri, const char ** type,
|
|
|
- struct handle_ops ** ops)
|
|
|
-{
|
|
|
- struct handle_ops * dops = NULL;
|
|
|
- const char * p, * u = (*uri);
|
|
|
+static int parse_device_uri(const char** uri, const char** type, struct handle_ops** ops) {
|
|
|
+ struct handle_ops* dops = NULL;
|
|
|
+ const char *p, *u = (*uri);
|
|
|
|
|
|
- for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
|
|
|
+ for (p = u; (*p) && (*p) != ',' && (*p) != '/'; p++)
|
|
|
+ ;
|
|
|
|
|
|
if (strpartcmp_static(u, "tty"))
|
|
|
dops = &term_ops;
|
|
@@ -83,22 +82,15 @@ static int parse_device_uri (const char ** uri, const char ** type,
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static inline void
|
|
|
-dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat);
|
|
|
+static inline void dev_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat);
|
|
|
|
|
|
-static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
|
|
|
- void * buffer);
|
|
|
-static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
|
|
|
- const void * buffer);
|
|
|
-static int term_attrquery (const char * type, const char * uri,
|
|
|
- PAL_STREAM_ATTR * attr);
|
|
|
-static int term_attrquerybyhdl (PAL_HANDLE hdl,
|
|
|
- PAL_STREAM_ATTR * attr);
|
|
|
+static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer);
|
|
|
+static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer);
|
|
|
+static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr);
|
|
|
+static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr);
|
|
|
|
|
|
|
|
|
-static int open_standard_term (PAL_HANDLE * handle, const char * param,
|
|
|
- int access)
|
|
|
-{
|
|
|
+static int open_standard_term(PAL_HANDLE* handle, const char* param, int access) {
|
|
|
if (param)
|
|
|
return -PAL_ERROR_NOTIMPLEMENTED;
|
|
|
|
|
@@ -111,7 +103,7 @@ static int open_standard_term (PAL_HANDLE * handle, const char * param,
|
|
|
hdl->dev.fd_in = 0;
|
|
|
}
|
|
|
|
|
|
- if (access & (PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) {
|
|
|
+ if (access & (PAL_ACCESS_WRONLY | PAL_ACCESS_RDWR)) {
|
|
|
HANDLE_HDR(hdl)->flags |= WFD(1);
|
|
|
hdl->dev.fd_out = 1;
|
|
|
}
|
|
@@ -121,13 +113,12 @@ static int open_standard_term (PAL_HANDLE * handle, const char * param,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
|
|
|
- int access, int share, int create, int options)
|
|
|
-{
|
|
|
- const char * term = NULL;
|
|
|
- const char * param = NULL;
|
|
|
+static int term_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
|
|
|
+ int create, int options) {
|
|
|
+ const char* term = NULL;
|
|
|
+ const char* param = NULL;
|
|
|
|
|
|
- const char * tmp = uri;
|
|
|
+ const char* tmp = uri;
|
|
|
while (*tmp) {
|
|
|
if (!term && *tmp == '/')
|
|
|
term = tmp + 1;
|
|
@@ -144,48 +135,41 @@ static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
|
|
|
return open_standard_term(handle, param, access);
|
|
|
}
|
|
|
|
|
|
-static int term_close (PAL_HANDLE handle)
|
|
|
-{
|
|
|
+static int term_close(PAL_HANDLE handle) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int term_attrquery (const char * type, const char * uri,
|
|
|
- PAL_STREAM_ATTR * attr)
|
|
|
-{
|
|
|
- attr->handle_type = pal_type_dev;
|
|
|
- attr->readable = PAL_TRUE;
|
|
|
- attr->writeable = PAL_TRUE;
|
|
|
- attr->runnable = PAL_FALSE;
|
|
|
+static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
|
|
|
+ attr->handle_type = pal_type_dev;
|
|
|
+ attr->readable = PAL_TRUE;
|
|
|
+ attr->writeable = PAL_TRUE;
|
|
|
+ attr->runnable = PAL_FALSE;
|
|
|
attr->pending_size = 0;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int term_attrquerybyhdl (PAL_HANDLE hdl,
|
|
|
- PAL_STREAM_ATTR * attr)
|
|
|
-{
|
|
|
- attr->handle_type = pal_type_dev;
|
|
|
- attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
|
|
|
- attr->writeable = (hdl->dev.fd_out != PAL_IDX_POISON);
|
|
|
- attr->runnable = PAL_FALSE;
|
|
|
+static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr) {
|
|
|
+ attr->handle_type = pal_type_dev;
|
|
|
+ attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
|
|
|
+ attr->writeable = (hdl->dev.fd_out != PAL_IDX_POISON);
|
|
|
+ attr->runnable = PAL_FALSE;
|
|
|
attr->pending_size = 0;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
static struct handle_ops term_ops = {
|
|
|
- .open = &term_open,
|
|
|
- .close = &term_close,
|
|
|
- .read = &char_read,
|
|
|
- .write = &char_write,
|
|
|
- .attrquery = &term_attrquery,
|
|
|
- .attrquerybyhdl = &term_attrquerybyhdl,
|
|
|
- };
|
|
|
+ .open = &term_open,
|
|
|
+ .close = &term_close,
|
|
|
+ .read = &char_read,
|
|
|
+ .write = &char_write,
|
|
|
+ .attrquery = &term_attrquery,
|
|
|
+ .attrquerybyhdl = &term_attrquerybyhdl,
|
|
|
+};
|
|
|
|
|
|
|
|
|
-static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
- void * buffer)
|
|
|
-{
|
|
|
+static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
|
|
|
int fd = handle->dev.fd_in;
|
|
|
|
|
|
if (fd == PAL_IDX_POISON)
|
|
@@ -200,9 +184,7 @@ static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
- const void * buffer)
|
|
|
-{
|
|
|
+static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
|
|
|
int fd = handle->dev.fd_out;
|
|
|
|
|
|
if (fd == PAL_IDX_POISON)
|
|
@@ -217,12 +199,11 @@ static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
|
|
|
- int access, int share, int create, int options)
|
|
|
-{
|
|
|
- struct handle_ops * ops = NULL;
|
|
|
- const char * dev_type = NULL;
|
|
|
- int ret = 0;
|
|
|
+static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
|
|
|
+ int create, int options) {
|
|
|
+ struct handle_ops* ops = NULL;
|
|
|
+ const char* dev_type = NULL;
|
|
|
+ int ret = 0;
|
|
|
|
|
|
ret = parse_device_uri(&uri, &dev_type, &ops);
|
|
|
|
|
@@ -230,23 +211,20 @@ static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
|
|
|
return ret;
|
|
|
|
|
|
if (!ops->open)
|
|
|
- return -PAL_ERROR_NOTSUPPORT;
|
|
|
+ return -PAL_ERROR_NOTSUPPORT;
|
|
|
|
|
|
PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
|
|
|
SET_HANDLE_TYPE(hdl, dev);
|
|
|
hdl->dev.fd_in = PAL_IDX_POISON;
|
|
|
hdl->dev.fd_out = PAL_IDX_POISON;
|
|
|
- *handle = hdl;
|
|
|
+ *handle = hdl;
|
|
|
|
|
|
- return ops->open(handle, dev_type, uri,
|
|
|
- access, share, create, options);
|
|
|
+ return ops->open(handle, dev_type, uri, access, share, create, options);
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int64_t dev_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
- void * buffer)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (!ops || !ops->read)
|
|
|
return -PAL_ERROR_NOTSUPPORT;
|
|
@@ -255,10 +233,8 @@ static int64_t dev_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int64_t dev_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
- const void * buffer)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (!ops || !ops->write)
|
|
|
return -PAL_ERROR_NOTSUPPORT;
|
|
@@ -267,9 +243,8 @@ static int64_t dev_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_close (PAL_HANDLE handle)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int dev_close(PAL_HANDLE handle) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (ops && ops->close)
|
|
|
return ops->close(handle);
|
|
@@ -297,15 +272,14 @@ static int dev_close (PAL_HANDLE handle)
|
|
|
}
|
|
|
|
|
|
if (handle->file.realpath)
|
|
|
- free((void *) handle->file.realpath);
|
|
|
+ free((void*)handle->file.realpath);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_delete (PAL_HANDLE handle, int access)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int dev_delete(PAL_HANDLE handle, int access) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (!ops || !ops->delete)
|
|
|
return -PAL_ERROR_DENIED;
|
|
@@ -315,13 +289,12 @@ static int dev_delete (PAL_HANDLE handle, int access)
|
|
|
if (ret < 0)
|
|
|
return ret;
|
|
|
|
|
|
- return ops->delete(handle, access);
|
|
|
+ return ops->delete (handle, access);
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_flush (PAL_HANDLE handle)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int dev_flush(PAL_HANDLE handle) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (ops && ops->flush)
|
|
|
return ops->flush(handle);
|
|
@@ -342,8 +315,7 @@ static int dev_flush (PAL_HANDLE handle)
|
|
|
|
|
|
|
|
|
flush output stream as well */
|
|
|
- if (handle->dev.fd_out != PAL_IDX_POISON &&
|
|
|
- handle->dev.fd_out != handle->dev.fd_in) {
|
|
|
+ if (handle->dev.fd_out != PAL_IDX_POISON && handle->dev.fd_out != handle->dev.fd_in) {
|
|
|
int fd = handle->dev.fd_out;
|
|
|
|
|
|
int ret = INLINE_SYSCALL(fsync, 1, fd);
|
|
@@ -359,24 +331,20 @@ static int dev_flush (PAL_HANDLE handle)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static inline void
|
|
|
-dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
|
|
|
-{
|
|
|
+static inline void dev_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat) {
|
|
|
attr->handle_type = pal_type_dev;
|
|
|
|
|
|
- attr->readable = stataccess(stat, ACCESS_R);
|
|
|
- attr->writeable = stataccess(stat, ACCESS_W);
|
|
|
- attr->runnable = stataccess(stat, ACCESS_X);
|
|
|
+ attr->readable = stataccess(stat, ACCESS_R);
|
|
|
+ attr->writeable = stataccess(stat, ACCESS_W);
|
|
|
+ attr->runnable = stataccess(stat, ACCESS_X);
|
|
|
attr->pending_size = stat->st_size;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_attrquery (const char * type, const char * uri,
|
|
|
- PAL_STREAM_ATTR * attr)
|
|
|
-{
|
|
|
- struct handle_ops * ops = NULL;
|
|
|
- const char * dev_type = NULL;
|
|
|
- int ret = 0;
|
|
|
+static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
|
|
|
+ struct handle_ops* ops = NULL;
|
|
|
+ const char* dev_type = NULL;
|
|
|
+ int ret = 0;
|
|
|
|
|
|
ret = parse_device_uri(&uri, &dev_type, &ops);
|
|
|
|
|
@@ -390,15 +358,13 @@ static int dev_attrquery (const char * type, const char * uri,
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int dev_attrquerybyhdl (PAL_HANDLE handle,
|
|
|
- PAL_STREAM_ATTR * attr)
|
|
|
-{
|
|
|
- const struct handle_ops * ops = DEVICE_OPS(handle);
|
|
|
+static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
|
|
|
+ const struct handle_ops* ops = DEVICE_OPS(handle);
|
|
|
|
|
|
if (ops && ops->attrquerybyhdl)
|
|
|
return ops->attrquerybyhdl(handle, attr);
|
|
|
|
|
|
- struct stat stat_buf, * stat_in = NULL, * stat_out = NULL;
|
|
|
+ struct stat stat_buf, *stat_in = NULL, *stat_out = NULL;
|
|
|
int ret;
|
|
|
|
|
|
attr->handle_type = pal_type_dev;
|
|
@@ -417,27 +383,25 @@ static int dev_attrquerybyhdl (PAL_HANDLE handle,
|
|
|
stat_out = &stat_buf;
|
|
|
}
|
|
|
|
|
|
- attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
|
|
|
- attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
|
|
|
- attr->writeable = (stat_out && stataccess(stat_out, ACCESS_W));
|
|
|
- attr->pending_size = stat_in ? stat_in->st_size :
|
|
|
- (stat_out ? stat_out->st_size : 0);
|
|
|
+ attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
|
|
|
+ attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
|
|
|
+ attr->writeable = (stat_out && stataccess(stat_out, ACCESS_W));
|
|
|
+ attr->pending_size = stat_in ? stat_in->st_size : (stat_out ? stat_out->st_size : 0);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static const char * dev_getrealpath (PAL_HANDLE handle)
|
|
|
-{
|
|
|
+static const char* dev_getrealpath(PAL_HANDLE handle) {
|
|
|
return handle->dev.realpath;
|
|
|
}
|
|
|
|
|
|
struct handle_ops dev_ops = {
|
|
|
- .getrealpath = &dev_getrealpath,
|
|
|
- .open = &dev_open,
|
|
|
- .read = &dev_read,
|
|
|
- .write = &dev_write,
|
|
|
- .close = &dev_close,
|
|
|
- .delete = &dev_delete,
|
|
|
- .flush = &dev_flush,
|
|
|
- .attrquery = &dev_attrquery,
|
|
|
- .attrquerybyhdl = &dev_attrquerybyhdl,
|
|
|
- };
|
|
|
+ .getrealpath = &dev_getrealpath,
|
|
|
+ .open = &dev_open,
|
|
|
+ .read = &dev_read,
|
|
|
+ .write = &dev_write,
|
|
|
+ .close = &dev_close,
|
|
|
+ .delete = &dev_delete,
|
|
|
+ .flush = &dev_flush,
|
|
|
+ .attrquery = &dev_attrquery,
|
|
|
+ .attrquerybyhdl = &dev_attrquerybyhdl,
|
|
|
+};
|