Archived
3
0

Merge branch 'tcpd' of ssh://git.karchnu.fr:2202/Karchnu/perfectos-junk into tcpd

This commit is contained in:
Philippe PITTOLI 2016-12-21 01:28:23 +01:00
commit a0d82c2df0
7 changed files with 222 additions and 211 deletions

View File

@ -0,0 +1,31 @@
#include "../../core/process.h"
#include <string.h> /* memset */
#include <stdio.h>
int main() {
int ret;
struct array_proc tab_proc;
memset(&tab_proc, 0, sizeof(struct array_proc));
struct process process_tab[5];
memset(&process_tab, 0, sizeof(struct process) * 5);
int i;
for (i = 0; i < 5; i++) {
process_tab[i].proc_fd = i;
ret = add_proc(&tab_proc, &process_tab[i]);
if (ret == -1) {
printf("erreur realloc\n");
}
}
array_proc_print(&tab_proc);
ret = del_proc(&tab_proc, &process_tab[2]);
if(ret < 0) {
printf("erreur %d\n", ret );
}
array_proc_print(&tab_proc);
return 0;
}

View File

@ -156,3 +156,72 @@ int app_write (struct service *srv, const struct msg *m)
{ {
return msg_write (srv->service_fd, m); return msg_write (srv->service_fd, m);
} }
/*prendre en parametre un tableau de process.
* trouver le processus/service actif et renvoyer CONNECTION/APPLICATION
* si un processus il va etre placer dans proc
* si un service il va etre placer dans service.
*/
int srv_select(struct array_proc *ap, struct service *srv, struct process **proc) {
int i, j;
/* master file descriptor list */
fd_set master;
fd_set readf;
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = srv->service_fd;
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&readf);
/* add the listener to the master set */
FD_SET(listener, &master);
for(i=0; i < ap->size; i++) {
FD_SET(ap->tab_proc[i]->proc_fd, &master);
}
/* keep track of the biggest file descriptor */
fdmax = getMaxFd(ap) > srv->service_fd ? getMaxFd(ap) : srv->service_fd;
while (1) {
readf = master;
if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
}
/*run through the existing connections looking for data to be read*/
for (i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &readf)) {
if (i == listener) {
return CONNECTION;
}else {
for(j = 0; j < ap->size; j++) {
if(i == ap->tab_proc[j]->proc_fd ) {
*proc = ap->tab_proc[j];
return APPLICATION;
}
}
}
}
}
}
}
/*calculer le max filedescriptor*/
int getMaxFd(struct array_proc *ap) {
int i;
int max = 0;
for (i = 0; i < ap->size; i++ ) {
if (ap->tab_proc[i]->proc_fd > max) {
max = ap->tab_proc[i]->proc_fd;
}
}
return max;
}

View File

@ -18,6 +18,9 @@
#define PATH_MAX BUFSIZ #define PATH_MAX BUFSIZ
#define CONNECTION 0
#define APPLICATION 1
struct service { struct service {
unsigned int version; unsigned int version;
unsigned int index; unsigned int index;
@ -39,6 +42,10 @@ int srv_accept (struct service *srv, struct process *p);
int srv_read (const struct process *, struct msg *m); int srv_read (const struct process *, struct msg *m);
int srv_write (const struct process *, const struct msg *m); int srv_write (const struct process *, const struct msg *m);
int srv_select(struct array_proc *, struct service *, struct process **);
int getMaxFd(struct array_proc *);
// APPLICATION // APPLICATION
// Initialize connection with unix socket // Initialize connection with unix socket
@ -51,4 +58,6 @@ int app_close (struct service *);
int app_read (struct service *srv, struct msg *m); int app_read (struct service *srv, struct msg *m);
int app_write (struct service *, const struct msg *m); int app_write (struct service *, const struct msg *m);
#endif #endif

View File

@ -1,4 +1,7 @@
#include "process.h" #include "process.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// TODO // TODO
// tout revoir ici // tout revoir ici
@ -27,9 +30,54 @@ void srv_process_gen (struct process *p
p->index = index; p->index = index;
} }
void srv_process_print (struct process *p)
#endif
int add_proc(struct array_proc *aproc, struct process *p) {
assert(aproc != NULL);
assert(p != NULL);
aproc->size++;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
aproc->tab_proc[aproc->size - 1] = p;
return 0;
}
int del_proc(struct array_proc *aproc, struct process *p) {
assert(aproc != NULL);
assert(p != NULL);
int i;
for (i = 0; i < aproc->size; i++) {
if (aproc->tab_proc[i] == p) {
aproc->tab_proc[i] = aproc->tab_proc[aproc->size-1];
aproc->size--;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
return 0;
}
}
return -2;
}
void process_print (struct process *p)
{ {
if (p != NULL) if (p != NULL)
printf ("process %d : index %d, version %d\n", p->index, p->version); printf ("process %d : index %d, version %d\n", p->proc_fd, p->index, p->version);
} }
#endif
void array_proc_print( struct array_proc *ap) {
int i;
for (i = 0; i < ap->size; i++) {
printf("%d : ", i);
process_print(ap->tab_proc[i]);
}
}

View File

@ -7,8 +7,18 @@ struct process {
int proc_fd; int proc_fd;
}; };
struct array_proc {
struct process **tab_proc;
int size;
};
// TODO // TODO
// tout revoir // tout revoir
int add_proc(struct array_proc *, struct process *);
int del_proc(struct array_proc *, struct process *);
void array_proc_print(struct array_proc *);
#if 0 #if 0

View File

@ -1,17 +1,16 @@
CC=gcc CC=gcc
CFLAGS=-Wall -g CFLAGS=-Wall -g -Wextra
LDFLAGS= -pthread LDFLAGS= -pthread
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c)) EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c) SOURCES=$(wildcard ../lib/*.c ../../core/*.c)
OBJECTS=$(SOURCES:.c=.o) OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC)) TESTS=$(addsuffix .test, $(EXEC))
LCBOR=-lbor
all: $(SOURCES) $(EXEC) all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES) $(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
.c.o: .c.o:
$(CC) -c $(CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $< -o $@

View File

@ -1,51 +1,14 @@
#include "../../lib/communication.h" #include "../../core/communication.h"
#include <pthread.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include "../../core/process.h"
#include <unistd.h>
#define PONGD_SERVICE_NAME "pongd" #define PONGD_SERVICE_NAME "pongd"
#define handle_error(msg) \ #define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/* control the file descriptor*/
void * pongd_thread(void * pdata) {
//struct process *proc = (struct process*) pdata;
int *sockclient = (int*) pdata;
// about the message
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes;
// init unix socket
while (1) {
if ((nbytes = file_read (*sockclient, &buf)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", nbytes);
}
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
printf("------thread shutdown------------\n");
//close(cfd);
close(*sockclient);
free(buf);
break;
}else {
printf ("read, size %d : %s\n", nbytes, buf);
if ((nbytes = file_write (*sockclient, buf, nbytes)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", nbytes);
}
}
}
return NULL;
}
/* /*
* main loop * main loop
@ -57,169 +20,53 @@ void * pongd_thread(void * pdata) {
void main_loop (struct service *srv) void main_loop (struct service *srv)
{ {
int ret;
struct process tab_proc[10];
//thread
pthread_t tab_thread[10];
int cnt = 0;
//init socket unix for server size_t msize = BUFSIZ;
int sfd; char *buf = NULL;
struct sockaddr_un peer_addr; if ( (buf = malloc (BUFSIZ)) == NULL) {
socklen_t peer_addr_size; handle_error ("malloc");
sfd = set_listen_socket(srv->spath);
if (sfd == -1){
handle_error("set_listen_socket");
} }
memset (buf, 0, BUFSIZ);
/* master file descriptor list */ int i,ret, cpt = 0;
fd_set master;
/* temp file descriptor list for select() */
fd_set read_fds;
/* maximum file descriptor number */ struct array_proc ap;
int fdmax; memset(&ap, 0, sizeof(struct array_proc));
/* listening socket descriptor */
int listener = sfd;
/* newly accept()ed socket descriptor */
int newfd;
/* buffer for client data */
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes; struct process *p2 = malloc(sizeof(struct process));
int i; while(cpt < 5) {
ret = srv_select(&ap, srv, &p2);
/* clear the master and temp sets */ if (ret == CONNECTION) {
FD_ZERO(&master); struct process *p = malloc(sizeof(struct process));
FD_ZERO(&read_fds); memset(p, 0, sizeof(struct process));
if (srv_accept (srv, p) < 0) {
handle_error("srv_accept < 0");
}else {
printf("new connection\n");
}
/* add the listener to the master set */ if (add_proc(&ap, p) < 0) {
FD_SET(listener, &master); handle_error("add_proc < 0");
//FD_SET(sfd, &master); }
cpt++;
/* keep track of the biggest file descriptor */ } else {
fdmax = sfd; /* so far, it's this one*/ if (srv_read(p2, &buf, &msize) < 0) {
handle_error("srv_read < 0");
for(;;) { }
/* copy it */
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
}
//printf("Server-select...OK\n");
/*run through the existing connections looking for data to be read*/
for(i = 0; i <= fdmax; i++) {
if(FD_ISSET(i, &read_fds)) {
/* we got one... */
if(i == listener) {
/* handle new connections */
peer_addr_size = sizeof(struct sockaddr_un);
newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
if (newfd == -1) {
handle_error("accept");
}
else
{
printf("Server-accept() is OK...\n");
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &newfd);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}
}
/*else {
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {*/
/* close it... */
//close(i);
/* remove from master set */
/*FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &i);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}*/
}
if (srv_write (p2, buf, msize) < 0) {
handle_error("srv_write < 0");
} }
} }
if (strncmp ("exit", buf, 4) == 0) { }
break;
for (i = 0; i < ap.size; i++) {
if (srv_close_proc (ap.tab_proc[i]) < 0) {
handle_error( "srv_close_proc < 0");
} }
} }
for (i = 0; i < cnt; i++) {
pthread_join(tab_thread[i], NULL);
}
free(buf);
close(sfd);
} }
@ -237,26 +84,24 @@ int main(int argc, char * argv[], char **env)
{ {
struct service srv; struct service srv;
memset (&srv, 0, sizeof (struct service)); memset (&srv, 0, sizeof (struct service));
srv->index = 0; srv.index = 0;
srv->version = 0; srv.version = 0;
srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME, NULL); unlink("/tmp/ipc/pongd-0-0");
if (srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME) < 0) {
handle_error("srv_init < 0");
return EXIT_FAILURE;
}
printf ("Listening on %s.\n", srv.spath); printf ("Listening on %s.\n", srv.spath);
// creates the service named pipe, that listens to client applications
int ret;
if ((ret = srv_create (&srv))) {
fprintf(stdout, "error service_create %d\n", ret);
exit (1);
}
printf("MAIN: server created\n" ); printf("MAIN: server created\n" );
// the service will loop until the end of time, a specific message, a signal // the service will loop until the end of time, a specific message, a signal
main_loop (&srv); main_loop (&srv);
// the application will shut down, and remove the service named pipe // the application will shut down, and remove the service named pipe
if ((ret = srv_close (&srv))) { if (srv_close (&srv) < 0) {
fprintf(stdout, "error service_close %d\n", ret); handle_error("srv_close < 0");
exit (1);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;