
fork + accessing global variable
Hi all,
I developped a program in C, and using fork sistem call, in order to get faster prosessing.
But I encounter a problem to acces a global variable from child process. The global variable is pointer to link-list. I tried to manipulate the list form a module and travers it from the other module.
Does it have any difference to access a global variable from child prosess like this ?
Any sugestion would be great
I list the brief code below:
**************
/* header.h */
struct CLIENTS{
char *clientname;
struct CLIENTS *next;
**************
/* file1.c */
#include <header.h>
struct CLIENTS *client_list;
...
rad_accounting(authreq, activefd){
client = client_list;
while(client != NULL){
if(client->ipaddr == authreq->ipaddr)
break;
client = client->next;
}
if(!client) {
tempclient = (struct CLIENTS *)malloc(sizeof(struct CLIENTS));
tempclient->ipaddr = authreq->ipaddr;
tempclient->clientname = (char *)strdup(clientname);
tempclient->topline = -1;
tempclient->sessionmarkwas = 0;
tempclient->user = (struct USERS *)NULL;
tempclient->next = (struct CLIENTS *)client_list;
client_list = tempclient;
client = tempclient;
}
....
**************
/* file2.c
...
#include <header.h>
extern struct CLIENTS *client_list;
...
if(spawn_flag) {
acct_pid = fork();
if(acct_pid < 0) {
log_err("could not fork to spawn accounting daemon\n");
rad_exit(-1);
}
if(acct_pid > 0) {
close(acctfd);
acctfd = -1;
}else {
close(sockfd);
sockfd = -1;
}
/*
* Receive user requests
*/
sin = (struct sockaddr_in *) & saremote;
if(sockfd != -1) {
update_clients();
}
for(;;) {
FD_ZERO(&readfds);
if(sockfd >= 0) {
FD_SET(sockfd, &readfds);
}
if(acctfd >= 0) {
FD_SET(acctfd, &readfds);
}
status = select(32, &readfds, NULL, NULL, (struct timeval *)NULL);
if(sockfd != -1) {
update_clients();
}
if(status == -1) {
if (errno == EINTR)
continue;
sig_fatal(101);
}
if(sockfd >= 0 && FD_ISSET(sockfd, &readfds)) {
rad_request(sockfd);
}
if(acctfd >=0 && FD_ISSET(acctfd, &readfds)) {
rad_request(acctfd);
}
}
...
rad_request
...
case PW_AUTHENTICATION_REQUEST:
if(spawn_flag) {
rad_spawn_child(authreq, activefd);
}else {
rad_authenticate(authreq, activefd);
}
break;
case PW_ACCOUNTING_REQUEST:
rad_accounting(authreq, activefd);
break;
#ifdef PASSCHANGE
case PW_PASSWORD_REQUEST:
rad_passchange(authreq, activefd);
break;
#endif
case PW_SPECIAL_REQUEST:
log_err("radrespond : PW_SPECIAL\n");
rad_special_req(authreq, activefd);
break;
...
rad_special_req(authreq, activefd) {
...
Client=client_list;
if(client_list==NULL){
log_err("SendACK: Client NULL");
}
....