-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from mengdemao/feature/socket
feat(socket): fix lazybsd lazybsd_socket
- Loading branch information
Showing
21 changed files
with
1,770 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* @file lazybsd_kevent.c | ||
* @author Meng Demao (mengdemao19951021@163.com) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2024-06-12 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#include <sys/param.h> | ||
#include <sys/limits.h> | ||
#include <sys/uio.h> | ||
#include <sys/proc.h> | ||
#include <sys/syscallsubr.h> | ||
#include <sys/module.h> | ||
#include <sys/param.h> | ||
#include <sys/malloc.h> | ||
#include <sys/socketvar.h> | ||
#include <sys/kernel.h> | ||
#include <sys/refcount.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/pcpu.h> | ||
#include <sys/select.h> | ||
#include <sys/poll.h> | ||
#include <sys/file.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <sys/ttycom.h> | ||
#include <sys/filio.h> | ||
#include <sys/sysproto.h> | ||
#include <sys/fcntl.h> | ||
#include <sys/socket.h> | ||
#include <net/route.h> | ||
#include <net/route/route_ctl.h> | ||
|
||
#include "lazybsd_errno.h" | ||
#include "lazybsd_kevent.h" | ||
|
||
int lazybsd_kqueue(void) | ||
{ | ||
int rc; | ||
if ((rc = kern_kqueue(curthread, 0, NULL))) | ||
goto kern_fail; | ||
|
||
rc = curthread->td_retval[0]; | ||
return (rc); | ||
|
||
kern_fail: | ||
lazybsd_os_errno(rc); | ||
return (-1); | ||
} | ||
|
||
struct sys_kevent_args { | ||
int fd; | ||
const struct kevent *changelist; | ||
int nchanges; | ||
void *eventlist; | ||
int nevents; | ||
const struct timespec *timeout; | ||
void (*do_each)(void **, struct kevent *); | ||
}; | ||
|
||
static int | ||
kevent_copyout(void *arg, struct kevent *kevp, int count) | ||
{ | ||
int i; | ||
struct kevent *ke; | ||
struct sys_kevent_args *uap; | ||
|
||
uap = (struct sys_kevent_args *)arg; | ||
|
||
if (!uap->do_each) { | ||
bcopy(kevp, uap->eventlist, count * sizeof *kevp); | ||
uap->eventlist = (void *)((struct kevent *)(uap->eventlist) + count); | ||
|
||
} else { | ||
for (ke = kevp, i = 0; i < count; i++, ke++) { | ||
uap->do_each(&(uap->eventlist), ke); | ||
} | ||
} | ||
|
||
return (0); | ||
} | ||
|
||
/* | ||
* Copy 'count' items from the list pointed to by uap->changelist. | ||
*/ | ||
static int | ||
kevent_copyin(void *arg, struct kevent *kevp, int count) | ||
{ | ||
struct sys_kevent_args *uap; | ||
|
||
uap = (struct sys_kevent_args *)arg; | ||
bcopy(uap->changelist, kevp, count * sizeof *kevp); | ||
|
||
uap->changelist += count; | ||
|
||
return (0); | ||
} | ||
|
||
int lazybsd_kevent_do_each(int kq, const struct kevent *changelist, int nchanges, | ||
void *eventlist, int nevents, const struct timespec *timeout, | ||
void (*do_each)(void **, struct kevent *)) | ||
{ | ||
int rc; | ||
struct timespec ts; | ||
ts.tv_sec = 0; | ||
ts.tv_nsec = 0; | ||
|
||
struct sys_kevent_args ska = { | ||
kq, | ||
changelist, | ||
nchanges, | ||
eventlist, | ||
nevents, | ||
&ts, | ||
do_each | ||
}; | ||
|
||
struct kevent_copyops k_ops = { | ||
&ska, | ||
kevent_copyout, | ||
kevent_copyin | ||
}; | ||
|
||
if ((rc = kern_kevent(curthread, kq, nchanges, nevents, &k_ops, | ||
&ts))) | ||
goto kern_fail; | ||
|
||
rc = curthread->td_retval[0]; | ||
return (rc); | ||
kern_fail: | ||
lazybsd_os_errno(rc); | ||
return (-1); | ||
} | ||
|
||
int | ||
lazybsd_kevent(int kq, const struct kevent *changelist, int nchanges, | ||
struct kevent *eventlist, int nevents, const struct timespec *timeout) | ||
{ | ||
return lazybsd_kevent_do_each(kq, changelist, nchanges, eventlist, nevents, timeout, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @file lazybsd_kevent.h | ||
* @author Meng Demao (mengdemao19951021@163.com) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2024-06-12 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#include <sys/types.h> | ||
#include <sys/event.h> | ||
#ifndef __LAZYBSD_KEVENT_H__ | ||
#define __LAZYBSD_KEVENT_H__ | ||
|
||
int lazybsd_kqueue(void); | ||
|
||
int lazybsd_kevent(int kq, const struct kevent *changelist, int nchanges, | ||
struct kevent *eventlist, int nevents, const struct timespec *timeout); | ||
|
||
int lazybsd_kevent_do_each(int kq, const struct kevent *changelist, int nchanges, | ||
void *eventlist, int nevents, const struct timespec *timeout, | ||
void (*do_each)(void **, struct kevent *)); | ||
|
||
#endif /* __LAZYBSD_KEVENT_H__ */ |
Oops, something went wrong.