FILE *open_logging() { #ifdef ANDROID return NULL; #endif if (!logfile) { const char *logpath = getenv("SERVALD_LOG_FILE"); if (!logpath) { // If the configuration is locked (eg, it called WHY() or DEBUG() while initialising, which // led back to here) then return NULL to indicate the message cannot be logged. if (confLocked()) return NULL; logpath = confValueGet("log.file", NULL); } if (!logpath) { logfile = stderr; INFO("No logfile configured -- logging to stderr"); } else if ((logfile = fopen(logpath, "a"))) { setlinebuf(logfile); INFOF("Logging to %s (fd %d)", logpath, fileno(logfile)); } else { logfile = stderr; WARN_perror("fopen"); WARNF("Cannot append to %s -- falling back to stderr", logpath); } } return logfile; }
void rhizome_server_poll(struct sched_ent *alarm) { if (alarm->poll.revents & (POLLIN | POLLOUT)) { struct sockaddr addr; unsigned int addr_len = sizeof addr; int sock; while ((sock = accept(rhizome_server_socket, &addr, &addr_len)) != -1) { if (addr.sa_family == AF_INET) { struct sockaddr_in *peerip = (struct sockaddr_in *)&addr; INFOF("RHIZOME HTTP SERVER, ACCEPT addrlen=%u family=%u port=%u addr=%u.%u.%u.%u", addr_len, peerip->sin_family, peerip->sin_port, ((unsigned char*)&peerip->sin_addr.s_addr)[0], ((unsigned char*)&peerip->sin_addr.s_addr)[1], ((unsigned char*)&peerip->sin_addr.s_addr)[2], ((unsigned char*)&peerip->sin_addr.s_addr)[3] ); } else { INFOF("RHIZOME HTTP SERVER, ACCEPT addrlen=%u family=%u data=%s", addr_len, addr.sa_family, alloca_tohex((unsigned char *)addr.sa_data, sizeof addr.sa_data) ); } rhizome_http_request *request = calloc(sizeof(rhizome_http_request), 1); if (request == NULL) { WHYF_perror("calloc(%u, 1)", sizeof(rhizome_http_request)); WHY("Cannot respond to request, out of memory"); } else { /* We are now trying to read the HTTP request */ request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING; request->alarm.function = rhizome_client_poll; connection_stats.name="rhizome_client_poll"; request->alarm.stats=&connection_stats; request->alarm.poll.fd=sock; request->alarm.poll.events=POLLIN; request->alarm.alarm = gettime_ms()+RHIZOME_IDLE_TIMEOUT; request->alarm.deadline = request->alarm.alarm+RHIZOME_IDLE_TIMEOUT; // watch for the incoming http request watch(&request->alarm); // set an inactivity timeout to close the connection schedule(&request->alarm); } } if (errno != EAGAIN) { WARN_perror("accept"); } } if (alarm->poll.revents & (POLLHUP | POLLERR)) { INFO("Error on tcp listen socket"); } }
int overlay_mdp_client_init() { if (mdp_client_socket==-1) { /* Open socket to MDP server (thus connection is always local) */ if (0) WHY("Use of abstract name space socket for Linux not implemented"); mdp_client_socket = socket(AF_UNIX, SOCK_DGRAM, 0); if (mdp_client_socket < 0) { WHY_perror("socket"); return WHY("Could not open socket to MDP server"); } /* We must bind to a temporary file name */ struct sockaddr_un name; unsigned int random_value; if (urandombytes((unsigned char *)&random_value,sizeof(int))) return WHY("urandombytes() failed"); name.sun_family = AF_UNIX; if (overlay_mdp_client_socket_path_len==-1) { char fmt[1024]; if (!FORM_SERVAL_INSTANCE_PATH(fmt, "mdp-client-%d-%08x.socket")) return WHY("Could not form MDP client socket name"); snprintf(overlay_mdp_client_socket_path,1024,fmt,getpid(),random_value); overlay_mdp_client_socket_path_len=strlen(overlay_mdp_client_socket_path)+1; if(config.debug.io) DEBUGF("MDP client socket name='%s'",overlay_mdp_client_socket_path); } if (overlay_mdp_client_socket_path_len > sizeof(name.sun_path) - 1) FATALF("MDP socket path too long (%d > %d)", overlay_mdp_client_socket_path_len, sizeof(name.sun_path) - 1); bcopy(overlay_mdp_client_socket_path,name.sun_path, overlay_mdp_client_socket_path_len); unlink(name.sun_path); int len = 1 + strlen(name.sun_path) + sizeof(name.sun_family) + 1; int r=bind(mdp_client_socket, (struct sockaddr *)&name, len); if (r) { WHY_perror("bind"); return WHY("Could not bind MDP client socket to file name"); } int send_buffer_size=128*1024; if (setsockopt(mdp_client_socket, SOL_SOCKET, SO_RCVBUF, &send_buffer_size, sizeof(send_buffer_size)) == -1) WARN_perror("setsockopt"); } return 0; }
int overlay_mdp_setup_sockets() { struct sockaddr_un name; int len; name.sun_family = AF_UNIX; #ifndef HAVE_LINUX_IF_H /* Abstrack name space (i.e., non-file represented) unix domain sockets are a linux-only thing. */ mdp_abstract.poll.fd = -1; #else if (mdp_abstract.poll.fd<=0) { /* Abstract name space unix sockets is a special Linux thing, which is convenient for us because Android is Linux, but does not have a shared writable path that is on a UFS partition, so we cannot use traditional named unix domain sockets. So the abstract name space gives us a solution. */ name.sun_path[0]=0; /* XXX The 100 should be replaced with the actual maximum allowed. Apparently POSIX requires it to be at least 100, but I would still feel more comfortable with using the appropriate constant. */ snprintf(&name.sun_path[1],100, confValueGet("mdp.socket",DEFAULT_MDP_SOCKET_NAME)); len = 1+strlen(&name.sun_path[1]) + sizeof(name.sun_family); mdp_abstract.poll.fd = socket(AF_UNIX, SOCK_DGRAM, 0); if (mdp_abstract.poll.fd>-1) { int reuseP=1; if (setsockopt( mdp_abstract.poll.fd, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof(reuseP)) == -1) { WARN_perror("setsockopt(SO_REUSEADDR)"); WARN("Could not set socket reuse addresses"); } if (bind(mdp_abstract.poll.fd, (struct sockaddr *)&name, len) == -1) { WARN_perror("bind"); close(mdp_abstract.poll.fd); mdp_abstract.poll.fd = -1; WARN("bind of abstract name space socket failed (not a problem on non-linux systems)"); } int send_buffer_size=64*1024; if (setsockopt(mdp_abstract.poll.fd, SOL_SOCKET, SO_SNDBUF, &send_buffer_size, sizeof(send_buffer_size)) == -1) WARN_perror("setsockopt(SO_SNDBUF)"); mdp_abstract.poll.events = POLLIN; watch(&mdp_abstract); } } #endif if (mdp_named.poll.fd<=0) { if (!form_serval_instance_path(&name.sun_path[0], 100, "mdp.socket")) return WHY("Cannot construct name of unix domain socket."); unlink(&name.sun_path[0]); len = 0+strlen(&name.sun_path[0]) + sizeof(name.sun_family)+1; mdp_named.poll.fd = socket(AF_UNIX, SOCK_DGRAM, 0); if (mdp_named.poll.fd>-1) { int reuseP=1; if(setsockopt( mdp_named.poll.fd, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof(reuseP)) == -1) { WARN_perror("setsockopt(SO_REUSEADDR)"); WARN("Could not set socket reuse addresses"); } if (bind(mdp_named.poll.fd, (struct sockaddr *)&name, len) == -1) { WARN_perror("bind"); close(mdp_named.poll.fd); mdp_named.poll.fd = -1; WARN("Could not bind named unix domain socket"); } int send_buffer_size=64*1024; if (setsockopt(mdp_named.poll.fd, SOL_SOCKET, SO_RCVBUF, &send_buffer_size, sizeof(send_buffer_size)) == -1) WARN_perror("setsockopt(SO_RCVBUF)"); mdp_named.function = overlay_mdp_poll; mdp_named.stats = &mdp_stats; mdp_named.poll.events = POLLIN; watch(&mdp_named); } } return 0; }