/*! \brief very inefficient for now - FIXME * keep in sync with tcpconn_destroy, the "delete" part should be * the same except for io_watch_del.. * \todo FIXME (very inefficient for now) */ static inline void __tcpconn_lifetime(int force) { struct tcp_connection *c, *next; unsigned int ticks,part; unsigned h; int fd; if (have_ticks()) ticks=get_ticks(); else ticks=0; for( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) { TCPCONN_LOCK(part); /* fixme: we can lock only on delete IMO */ for(h=0; h<TCP_ID_HASH_SIZE; h++){ c=TCP_PART(part).tcpconn_id_hash[h]; while(c){ next=c->id_next; if (force ||((c->refcnt==0) && (ticks>c->lifetime))) { if (!force) LM_DBG("timeout for hash=%d - %p" " (%d > %d)\n", h, c, ticks, c->lifetime); fd=c->s; _tcpconn_rm(c); if ((!force)&&(fd>0)&&(c->refcnt==0)) { if (!(c->flags & F_CONN_REMOVED)){ reactor_del_all( fd, -1, IO_FD_CLOSING); c->flags|=F_CONN_REMOVED; } close(fd); } tcp_connections_no--; } c=next; } } TCPCONN_UNLOCK(part); } }
/*! \brief * handles an io event on one of the watched tcp connections * * \param tcpconn - pointer to the tcp_connection for which we have an io ev. * \param fd_i - index in the fd_array table (needed for delete) * \return handle_* return convention, but on success it always returns 0 * (because it's one-shot, after a succesfull execution the fd is * removed from tcp_main's watch fd list and passed to a child => * tcp_main is not interested in further io events that might be * queued for this fd) */ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i, int event_type) { int fd; int err; int id; unsigned int err_len; if (event_type == IO_WATCH_READ) { /* pass it to child, so remove it from the io watch list */ LM_DBG("data available on %p %d\n", tcpconn, tcpconn->s); if (reactor_del_reader(tcpconn->s, fd_i, 0)==-1) return -1; tcpconn->flags|=F_CONN_REMOVED; tcpconn_ref(tcpconn); /* refcnt ++ */ if (send2child(tcpconn,IO_WATCH_READ)<0){ LM_ERR("no children available\n"); id = tcpconn->id; TCPCONN_LOCK(id); tcpconn->refcnt--; if (tcpconn->refcnt==0){ fd=tcpconn->s; _tcpconn_rm(tcpconn); close(fd); }else tcpconn->lifetime=0; /* force expire*/ TCPCONN_UNLOCK(id); } return 0; /* we are not interested in possibly queued io events, the fd was either passed to a child, or closed */ } else { LM_DBG("connection %p fd %d is now writable\n", tcpconn, tcpconn->s); /* we received a write event */ if (tcpconn->state==S_CONN_CONNECTING) { /* we're coming from an async connect & write * let's see if we connected successfully*/ err_len=sizeof(err); if (getsockopt(tcpconn->s, SOL_SOCKET, SO_ERROR, &err, &err_len) < 0 || \ err != 0) { LM_DBG("Failed connection attempt\n"); tcpconn_ref(tcpconn); reactor_del_all(tcpconn->s, fd_i, IO_FD_CLOSING); tcpconn->flags|=F_CONN_REMOVED; tcpconn_destroy(tcpconn); return 0; } /* we successfully connected - further treat this case as if we * were coming from an async write */ tcpconn->state = S_CONN_OK; LM_DBG("Successfully completed previous async connect\n"); goto async_write; } else { /* we're coming from an async write - * just pass to child and have it write * our TCP chunks */ async_write: /* no more write events for now */ if (reactor_del_writer( tcpconn->s, fd_i, 0)==-1) return -1; tcpconn->flags|=F_CONN_REMOVED; tcpconn_ref(tcpconn); /* refcnt ++ */ if (send2child(tcpconn,IO_WATCH_WRITE)<0){ LM_ERR("no children available\n"); id = tcpconn->id; TCPCONN_LOCK(id); tcpconn->refcnt--; if (tcpconn->refcnt==0){ fd=tcpconn->s; _tcpconn_rm(tcpconn); close(fd); }else tcpconn->lifetime=0; /* force expire*/ TCPCONN_UNLOCK(id); } return 0; } } }
/*! \brief handles io from a "generic" ser process (get fd or new_fd from a tcp_send) * * \param p - pointer in the ser processes array (pt[]), to the entry for * which an io event was detected * \param fd_i - fd index in the fd_array (useful for optimizing * io_watch_deletes) * \return handle_* return convention: * - -1 on error reading from the fd, * - 0 on EAGAIN or when no more io events are queued * (receive buffer empty), * - >0 on successfull reads from the fd (the receive buffer might * be non-empty). */ inline static int handle_worker(struct process_table* p, int fd_i) { struct tcp_connection* tcpconn; long response[2]; int cmd; int bytes; int ret; int fd; ret=-1; if (p->unix_sock<=0){ /* (we can't have a fd==0, 0 is never closed )*/ LM_CRIT("fd %d for %d (pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid); goto error; } /* get all bytes and the fd (if transmitted) * (this is a SOCK_STREAM so read is not atomic) */ bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd, MSG_DONTWAIT); if (bytes<(int)sizeof(response)){ /* too few bytes read */ if (bytes==0){ /* EOF -> bad, child has died */ LM_DBG("dead child %d, pid %d" " (shutting down?)\n", (int)(p-&pt[0]), p->pid); /* don't listen on it any more */ reactor_del_reader( p->unix_sock, fd_i, 0/*flags*/); goto error; /* child dead => no further io events from it */ }else if (bytes<0){ /* EAGAIN is ok if we try to empty the buffer * e.g: SIGIO_RT overflow mode or EPOLL ET */ if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){ LM_CRIT("read from child %d (pid %d): %s [%d]\n", (int)(p-&pt[0]), p->pid, strerror(errno), errno); ret=-1; }else{ ret=0; } /* try to ignore ? */ goto end; }else{ /* should never happen */ LM_CRIT("too few bytes received (%d)\n", bytes ); ret=0; /* something was read so there is no error; otoh if receive_fd returned less then requested => the receive buffer is empty => no more io queued on this fd */ goto end; } } ret=1; /* something was received, there might be more queued */ LM_DBG("read response= %lx, %ld, fd %d from %d (%d)\n", response[0], response[1], fd, (int)(p-&pt[0]), p->pid); cmd=response[1]; tcpconn=(struct tcp_connection*)response[0]; if (tcpconn==0){ LM_CRIT("null tcpconn pointer received from child %d (pid %d)" "%lx, %lx\n", (int)(p-&pt[0]), p->pid, response[0], response[1]) ; goto end; } switch(cmd){ case CONN_ERROR: if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){ reactor_del_all( tcpconn->s, -1, IO_FD_CLOSING); tcpconn->flags|=F_CONN_REMOVED; } tcpconn_destroy(tcpconn); /* will close also the fd */ break; case CONN_GET_FD: /* send the requested FD */ /* WARNING: take care of setting refcnt properly to * avoid race condition */ if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn), tcpconn->s)<=0){ LM_ERR("send_fd failed\n"); } break; case CONN_NEW: /* update the fd in the requested tcpconn*/ /* WARNING: take care of setting refcnt properly to * avoid race condition */ if (fd==-1){ LM_CRIT(" cmd CONN_NEW: no fd received\n"); break; } tcpconn->s=fd; /* add tcpconn to the list*/ tcpconn_add(tcpconn); reactor_add_reader( tcpconn->s, F_TCPCONN, RCT_PRIO_NET, tcpconn); tcpconn->flags&=~F_CONN_REMOVED; break; case ASYNC_CONNECT: /* connection is not yet linked to hash = not yet * available to the outside world */ if (fd==-1){ LM_CRIT(" cmd CONN_NEW: no fd received\n"); break; } tcpconn->s=fd; /* add tcpconn to the list*/ tcpconn_add(tcpconn); /* FIXME - now we have lifetime==default_lifetime - should we * set a shorter one when waiting for a connect ??? */ /* only maintain the socket in the IO_WATCH_WRITE watcher * while we have stuff to write - otherwise we're going to get * useless events */ reactor_add_writer( tcpconn->s, F_TCPCONN, RCT_PRIO_NET, tcpconn); tcpconn->flags&=~F_CONN_REMOVED; break; case ASYNC_WRITE: if (tcpconn->state==S_CONN_BAD){ tcpconn->lifetime=0; break; } /* must be after the de-ref*/ reactor_add_writer( tcpconn->s, F_TCPCONN, RCT_PRIO_NET, tcpconn); tcpconn->flags&=~F_CONN_REMOVED; break; default: LM_CRIT("unknown cmd %d\n", cmd); } end: return ret; error: return -1; }
/*! \brief * handle io routine, based on the fd_map type * (it will be called from reactor_main_loop ) * params: fm - pointer to a fd hash entry * idx - index in the fd_array (or -1 if not known) * return: -1 on error, or when we are not interested any more on reads * from this fd (e.g.: we are closing it ) * 0 on EAGAIN or when by some other way it is known that no more * io events are queued on the fd (the receive buffer is empty). * Usefull to detect when there are no more io events queued for * sigio_rt, epoll_et, kqueue. * >0 on successfull read from the fd (when there might be more io * queued -- the receive buffer might still be non-empty) */ inline static int handle_io(struct fd_map* fm, int idx,int event_type) { int ret=0; int n; struct tcp_connection* con; int s,rw; long resp; long response[2]; switch(fm->type){ case F_TIMER_JOB: handle_timer_job(); break; case F_SCRIPT_ASYNC: async_resume_f( fm->fd, fm->data); return 0; case F_TCPMAIN: again: ret=n=receive_fd(fm->fd, response, sizeof(response), &s, 0); if (n<0){ if (errno == EWOULDBLOCK || errno == EAGAIN){ ret=0; break; }else if (errno == EINTR) goto again; else{ LM_CRIT("read_fd: %s \n", strerror(errno)); abort(); /* big error*/ } } if (n==0){ LM_WARN("0 bytes read\n"); break; } con = (struct tcp_connection *)response[0]; rw = (int)response[1]; if (con==0){ LM_CRIT("null pointer\n"); break; } if (s==-1) { LM_BUG("read_fd:no fd read\n"); /* FIXME? */ return -1; } if (con==tcp_conn_lst){ LM_CRIT("duplicate" " connection received: %p, id %d, fd %d, refcnt %d" " state %d (n=%d)\n", con, con->id, con->fd, con->refcnt, con->state, n); tcpconn_release(con, CONN_ERROR,0); break; /* try to recover */ } LM_DBG("We have received conn %p with rw %d on fd %d\n",con,rw,s); if (rw & IO_WATCH_READ) { /* 0 attempts so far for this SIP MSG */ con->msg_attempts = 0; /* must be before reactor_add, as the add might catch some * already existing events => might call handle_io and * handle_io might decide to del. the new connection => * must be in the list */ tcpconn_listadd(tcp_conn_lst, con, c_next, c_prev); con->timeout = con->lifetime; if (reactor_add_reader( s, F_TCPCONN, RCT_PRIO_NET, con )<0) { LM_CRIT("failed to add new socket to the fd list\n"); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); goto con_error; } /* mark that the connection is currently in our process future writes to this con won't have to acquire FD */ con->proc_id = process_no; /* save FD which is valid in context of this TCP worker */ con->fd=s; } else if (rw & IO_WATCH_WRITE) { LM_DBG("Received con for async write %p ref = %d\n",con,con->refcnt); lock_get(&con->write_lock); resp = protos[con->type].net.write( (void*)con, s ); lock_release(&con->write_lock); if (resp<0) { ret=-1; /* some error occured */ con->state=S_CONN_BAD; tcpconn_release(con, CONN_ERROR,1); break; } else if (resp==1) { tcpconn_release(con, ASYNC_WRITE,1); } else { tcpconn_release(con, CONN_RELEASE,1); } ret = 0; /* we always close the socket received for writing */ close(s); } break; case F_TCPCONN: if (event_type & IO_WATCH_READ) { con=(struct tcp_connection*)fm->data; resp = protos[con->type].net.read( (void*)con, &ret ); if (resp<0) { ret=-1; /* some error occured */ con->state=S_CONN_BAD; reactor_del_all( con->fd, idx, IO_FD_CLOSING ); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); con->proc_id = -1; if (con->fd!=-1) { close(con->fd); con->fd = -1; } tcpconn_release(con, CONN_ERROR,0); } else if (con->state==S_CONN_EOF) { reactor_del_all( con->fd, idx, IO_FD_CLOSING ); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); con->proc_id = -1; if (con->fd!=-1) { close(con->fd); con->fd = -1; } tcpconn_release(con, CONN_EOF,0); } else { //tcpconn_release(con, CONN_RELEASE); /* keep the connection for now */ break; } } break; case F_NONE: LM_CRIT("empty fd map %p: " "{%d, %d, %p}\n", fm, fm->fd, fm->type, fm->data); goto error; default: LM_CRIT("uknown fd type %d\n", fm->type); goto error; } return ret; con_error: con->state=S_CONN_BAD; tcpconn_release(con, CONN_ERROR,0); return ret; error: return -1; }