|
@@ -13,9 +13,12 @@
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <string.h> //bzero
|
|
#include <string.h> //bzero
|
|
|
|
|
|
|
|
-const char* pathname="/rats";
|
|
|
|
|
|
|
+static const char* pathname="/tmp/rats";
|
|
|
|
|
|
|
|
-static void *udpController(void *dummy)
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ * nc -u 127.0.0.1 5514
|
|
|
|
|
+ */
|
|
|
|
|
+static void *udpController(void *dummy __attribute__((unused)))
|
|
|
{
|
|
{
|
|
|
/*
|
|
/*
|
|
|
*
|
|
*
|
|
@@ -29,8 +32,8 @@ static void *udpController(void *dummy)
|
|
|
|
|
|
|
|
sigset_t sigmask;
|
|
sigset_t sigmask;
|
|
|
sigemptyset(&sigmask);
|
|
sigemptyset(&sigmask);
|
|
|
- sigaddset(&sigmask,SIGINT);
|
|
|
|
|
- if(sigprocmask(SIG_BLOCK,&sigmask,NULL))
|
|
|
|
|
|
|
+ sigaddset(&sigmask, SIGINT);
|
|
|
|
|
+ if(sigprocmask(SIG_BLOCK, &sigmask, NULL))
|
|
|
perror("sigprocmask");
|
|
perror("sigprocmask");
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -40,12 +43,13 @@ static void *udpController(void *dummy)
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
// may use SHM_ANON running FreeBSD while called by the same process
|
|
// may use SHM_ANON running FreeBSD while called by the same process
|
|
|
- int fd=shm_open(pathname,O_RDWR,0600);
|
|
|
|
|
- if (fd < 0){
|
|
|
|
|
|
|
+ int fd = shm_open(pathname, O_RDWR, 0600);
|
|
|
|
|
+
|
|
|
|
|
+ if (fd < 0) {
|
|
|
warn("%s: shm_open", __func__);
|
|
warn("%s: shm_open", __func__);
|
|
|
pthread_exit(NULL);
|
|
pthread_exit(NULL);
|
|
|
}
|
|
}
|
|
|
- if (ftruncate(fd, getpagesize()) < 0){
|
|
|
|
|
|
|
+ if (ftruncate(fd, getpagesize()) < 0) {
|
|
|
warn("%s: ftruncate", __func__);
|
|
warn("%s: ftruncate", __func__);
|
|
|
pthread_exit(NULL);
|
|
pthread_exit(NULL);
|
|
|
}
|
|
}
|
|
@@ -56,8 +60,8 @@ static void *udpController(void *dummy)
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
- int s=socket(PF_INET,SOCK_DGRAM,0);
|
|
|
|
|
- if(-1==s){
|
|
|
|
|
|
|
+ int s = socket(PF_INET,SOCK_DGRAM, 0);
|
|
|
|
|
+ if (-1 == s) {
|
|
|
warn("socket");
|
|
warn("socket");
|
|
|
pthread_exit(NULL);
|
|
pthread_exit(NULL);
|
|
|
}
|
|
}
|
|
@@ -67,50 +71,50 @@ static void *udpController(void *dummy)
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
struct sockaddr_in sa;
|
|
struct sockaddr_in sa;
|
|
|
- bzero(&sa,sizeof sa);
|
|
|
|
|
- sa.sin_len=sizeof sa;
|
|
|
|
|
- sa.sin_family=AF_INET;
|
|
|
|
|
- sa.sin_port=htons(5514);
|
|
|
|
|
|
|
+ bzero(&sa, sizeof sa);
|
|
|
|
|
+ sa.sin_len = sizeof sa;
|
|
|
|
|
+ sa.sin_family = AF_INET;
|
|
|
|
|
+ sa.sin_port = htons(5514);
|
|
|
//sa.sin_addr.s_addr = htonl((((((127 << 8) | 0) << 8) | 0) << 8) | 1);
|
|
//sa.sin_addr.s_addr = htonl((((((127 << 8) | 0) << 8) | 0) << 8) | 1);
|
|
|
sa.sin_addr.s_addr = INADDR_ANY;
|
|
sa.sin_addr.s_addr = INADDR_ANY;
|
|
|
uint32_t cursor;
|
|
uint32_t cursor;
|
|
|
- if(!bind(s,(struct sockaddr*)&sa,sizeof sa)){
|
|
|
|
|
|
|
+ if(!bind(s, (struct sockaddr *)&sa, sizeof sa)) {
|
|
|
|
|
|
|
|
- for(;;){
|
|
|
|
|
|
|
+ for(;;) {
|
|
|
char buffer[getpagesize()];
|
|
char buffer[getpagesize()];
|
|
|
- int n=read(s,&buffer[4],getpagesize()-4);
|
|
|
|
|
- if(n==-1){
|
|
|
|
|
|
|
+ int n = read(s,&buffer[4], getpagesize()-4);
|
|
|
|
|
+ if (n == -1) {
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(n>0){
|
|
|
|
|
- if(!bcmp((char*)&buffer[4],"quit",4)){
|
|
|
|
|
|
|
+ if (n > 0 ) {
|
|
|
|
|
+ if(!bcmp((char *)&buffer[4], "quit", 4)) {
|
|
|
printf("kill request\n");
|
|
printf("kill request\n");
|
|
|
/* bye */
|
|
/* bye */
|
|
|
- kill(getpid(),SIGINT);
|
|
|
|
|
|
|
+ kill(getpid(), SIGINT);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* prepend length into the buffer */
|
|
/* prepend length into the buffer */
|
|
|
- cursor=n+4;
|
|
|
|
|
- buffer[0]= (n>> 24);
|
|
|
|
|
- buffer[1]= (n>> 16);
|
|
|
|
|
- buffer[2]= (n>> 8);
|
|
|
|
|
- buffer[3]= (n);
|
|
|
|
|
|
|
+ cursor = n + 4;
|
|
|
|
|
+ buffer[0] = (n >> 24);
|
|
|
|
|
+ buffer[1] = (n >> 16);
|
|
|
|
|
+ buffer[2] = (n>> 8);
|
|
|
|
|
+ buffer[3] = (n);
|
|
|
|
|
|
|
|
ssize_t len = pwrite(fd, buffer, cursor, 0);
|
|
ssize_t len = pwrite(fd, buffer, cursor, 0);
|
|
|
|
|
|
|
|
- if(len<0){
|
|
|
|
|
|
|
+ if (len < 0) {
|
|
|
warn("%s: pwrite failure", __func__);
|
|
warn("%s: pwrite failure", __func__);
|
|
|
}
|
|
}
|
|
|
- if(len!=cursor){
|
|
|
|
|
|
|
+ if (len != cursor) {
|
|
|
warn("%zu vs %du, %s: pwrite length mismatch",len,cursor, __func__);
|
|
warn("%zu vs %du, %s: pwrite length mismatch",len,cursor, __func__);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- }else
|
|
|
|
|
|
|
+ } else
|
|
|
warn("bind failure");
|
|
warn("bind failure");
|
|
|
|
|
|
|
|
close (s);
|
|
close (s);
|
|
@@ -128,7 +132,7 @@ static void showmem()
|
|
|
|
|
|
|
|
/* extract length */
|
|
/* extract length */
|
|
|
uint8_t curbuf[4];
|
|
uint8_t curbuf[4];
|
|
|
- ssize_t len=pread(fd, &curbuf, 4, 0);
|
|
|
|
|
|
|
+ ssize_t len = pread(fd, &curbuf, 4, 0);
|
|
|
if (len == 4) {
|
|
if (len == 4) {
|
|
|
uint32_t l = (uint32_t)curbuf[0] << 24 |
|
|
uint32_t l = (uint32_t)curbuf[0] << 24 |
|
|
|
(uint32_t)curbuf[1] << 16 |
|
|
(uint32_t)curbuf[1] << 16 |
|
|
@@ -138,8 +142,8 @@ static void showmem()
|
|
|
uint8_t* buf = malloc(l);
|
|
uint8_t* buf = malloc(l);
|
|
|
if (buf) {
|
|
if (buf) {
|
|
|
/* data */
|
|
/* data */
|
|
|
- ssize_t total=pread(fd,buf,l,4);
|
|
|
|
|
- printf("%zu:\t %s",total,buf);
|
|
|
|
|
|
|
+ ssize_t total = pread(fd, buf, l, 4);
|
|
|
|
|
+ printf("%zu:\t %s", total, buf);
|
|
|
free(buf);
|
|
free(buf);
|
|
|
} else {
|
|
} else {
|
|
|
warn("malloc");
|
|
warn("malloc");
|
|
@@ -151,7 +155,7 @@ static void showmem()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void
|
|
|
|
|
|
|
+static void
|
|
|
sigHandler(int signo, siginfo_t *si, void * /* ucontext_t */ uap)
|
|
sigHandler(int signo, siginfo_t *si, void * /* ucontext_t */ uap)
|
|
|
{
|
|
{
|
|
|
switch (signo) {
|
|
switch (signo) {
|
|
@@ -170,13 +174,13 @@ sigHandler(int signo, siginfo_t *si, void * /* ucontext_t */ uap)
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
int main(int argc, char* argv[])
|
|
|
{
|
|
{
|
|
|
- timer_t timerid;
|
|
|
|
|
|
|
+ timer_t timerid;
|
|
|
int fd = shm_open(pathname, O_CREAT | O_RDWR, 0600);
|
|
int fd = shm_open(pathname, O_CREAT | O_RDWR, 0600);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
if (fd < 0)
|
|
|
- err(EPERM, "%s: shm_open", __func__);
|
|
|
|
|
|
|
+ err(EXIT_FAILURE, "%s: shm_open", __func__);
|
|
|
if (ftruncate(fd, getpagesize()) < 0)
|
|
if (ftruncate(fd, getpagesize()) < 0)
|
|
|
- err(EIO, "%s: ftruncate", __func__);
|
|
|
|
|
|
|
+ err(EXIT_FAILURE, "%s: ftruncate", __func__);
|
|
|
|
|
|
|
|
pthread_t pidudp;
|
|
pthread_t pidudp;
|
|
|
pthread_create(&pidudp, NULL, &udpController, NULL);
|
|
pthread_create(&pidudp, NULL, &udpController, NULL);
|
|
@@ -192,12 +196,12 @@ int main(int argc, char* argv[])
|
|
|
warn("SIGNFO not caught.");
|
|
warn("SIGNFO not caught.");
|
|
|
}
|
|
}
|
|
|
if (sigaction(SIGALRM, &sa, NULL)) {
|
|
if (sigaction(SIGALRM, &sa, NULL)) {
|
|
|
- err(2, "sigaction:ALARM");
|
|
|
|
|
|
|
+ err(EXIT_FAILURE, "sigaction:ALARM");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* timer */
|
|
/* timer */
|
|
|
if (timer_create(CLOCK_REALTIME, NULL, &timerid)) {
|
|
if (timer_create(CLOCK_REALTIME, NULL, &timerid)) {
|
|
|
- err(2,"timer_create failed.");
|
|
|
|
|
|
|
+ err(EXIT_FAILURE,"timer_create failed.");
|
|
|
}
|
|
}
|
|
|
/* 3 -> TIMER_MAX stored into timerid->oshandle */
|
|
/* 3 -> TIMER_MAX stored into timerid->oshandle */
|
|
|
printf("Timer #%d now created at address %p.\n", timer_oshandle_np(timerid), timerid);
|
|
printf("Timer #%d now created at address %p.\n", timer_oshandle_np(timerid), timerid);
|
|
@@ -219,7 +223,7 @@ int main(int argc, char* argv[])
|
|
|
sigprocmask(SIG_BLOCK,&sigmask,NULL);
|
|
sigprocmask(SIG_BLOCK,&sigmask,NULL);
|
|
|
|
|
|
|
|
int sigpending;
|
|
int sigpending;
|
|
|
- if(sigwait(&sigmask,&sigpending))
|
|
|
|
|
|
|
+ if (sigwait(&sigmask, &sigpending))
|
|
|
warn("sigwait failure.\n");
|
|
warn("sigwait failure.\n");
|
|
|
//brutal thread killing, I know it s bad. but I 'm punk btw.
|
|
//brutal thread killing, I know it s bad. but I 'm punk btw.
|
|
|
printf("bye\n");
|
|
printf("bye\n");
|
|
@@ -235,5 +239,7 @@ int main(int argc, char* argv[])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// required until SHM_ANON is used, or shm remains (persistent until reboot)
|
|
// required until SHM_ANON is used, or shm remains (persistent until reboot)
|
|
|
- shm_unlink(pathname);
|
|
|
|
|
|
|
+ shm_unlink(pathname);
|
|
|
|
|
+
|
|
|
|
|
+ return EXIT_SUCCESS;
|
|
|
}
|
|
}
|