void do_cmd(char *cmd) { char buf[SS_BUFSIZE]; int slen, tries = 0; if (logtrans) Syslog('-', "< %s", cmd); snprintf(buf, SS_BUFSIZE, "%s", exe_cmd(cmd)); if (logtrans) Syslog('-', "> %s", buf); for (;;) { slen = sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)&from, fromlen); if (slen == -1) Syslog('?', "$do_cmd(): sendto error %d %s", tries, from.sun_path); else if (slen != strlen(buf)) Syslog('?', "do_cmd(): send %d of %d bytes, try=%d", slen, strlen(buf), tries); else return; tries++; if (tries == 3) return; sleep(1); } }
static void set_wifi_throttle(int level) { int i = 0; for ( i=0; i<IFC_NUM; i++) { TM_DBG_LOG("checking %s", IFC_PATH[i]); if (0 == access(IFC_PATH[i], R_OK)) { char buf[80]; int fd = open(IFC_PATH[i], O_RDONLY); if (fd < 0) { TM_INFO_LOG("Can't open %s: %s", IFC_PATH[i], strerror(errno)); continue; } int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { TM_INFO_LOG("Can't read %s: %s", IFC_PATH[i], strerror(errno)); continue; } close(fd); if(!strncmp (buf, "up", 2)) { ifc_set_throttle(IFC_NAME[i], level * ONE_MBITS_PER_SEC, level * ONE_MBITS_PER_SEC); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } else TM_DBG_LOG("%s is down!", IFC_NAME[i]); } } }
static void set_md_ul_throttle(int level) { int i = 0; TM_DBG_LOG("set_md_ul_throttle %d\n", level); for ( i=0; i<MD_IFC_NUM; i++) { TM_DBG_LOG("checking %s", MD_IFC_PATH[i]); if (0 == access(MD_IFC_PATH[i], R_OK)) { char buf[80]; int fd = open(MD_IFC_PATH[i], O_RDONLY); if (fd < 0) { TM_INFO_LOG("Can't open %s: %s", MD_IFC_PATH[i], strerror(errno)); continue; } int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { TM_INFO_LOG("Can't read %s: %s", MD_IFC_PATH[i], strerror(errno)); continue; } close(fd); #if 0 if(!strncmp (buf, "up", 2)) { ifc_set_throttle(MD_IFC_NAME[i], -1, level); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } else TM_DBG_LOG("%s is down!", MD_IFC_NAME[i]); #else if(!strncmp (buf, "down", 4)) { TM_DBG_LOG("%s is down!", MD_IFC_NAME[i]); } else { ifc_set_throttle(MD_IFC_NAME[i], -1, level); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } #endif } } }
void check_socket(int num) /* check for new connection, command, connection closed */ { int fd = -1, avoid_fd = -1, addr_len = sizeof(struct sockaddr_un); struct sockaddr_un client_addr; long int buf_int[SOCKET_MSG_LEN]; int read_len = 0; struct fcrondyn_cl *client = NULL, *prev_client = NULL; if ( num <= 0 ) /* no socket to check : go directly to the end of that function */ goto final_settings; debug("Checking socket ..."); if ( FD_ISSET(listen_fd, &read_set) ) { debug("got new connection ..."); if ((fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) { error_e("could not accept new connection : isset(listen_fd = %d) = %d", listen_fd, FD_ISSET(listen_fd, &read_set)); } else { fcntl(fd, F_SETFD, 1); /* set fd to O_NONBLOCK : we do not want fcron to be stopped on error, etc */ if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) { error_e("Could not set fd attribute O_NONBLOCK : connection rejected."); shutdown(fd, SHUT_RDWR); close(fd); } else { Alloc(client, fcrondyn_cl); client->fcl_sock_fd = fd; /* means : not authenticated yet : */ client->fcl_user = NULL; client->fcl_cmd = NULL; /* include new entry in client list */ client->fcl_next = fcrondyn_cl_base; fcrondyn_cl_base = client; client->fcl_idle_since = now; /* to avoid trying to read from it in this call */ avoid_fd = fd; FD_SET(fd, &master_set); if ( fd > set_max_fd ) set_max_fd = fd; fcrondyn_cl_num += 1; debug("Added connection fd : %d - %d connections", fd, fcrondyn_cl_num); } } } client = fcrondyn_cl_base; while ( client != NULL ) { if (! FD_ISSET(client->fcl_sock_fd, &read_set) || client->fcl_sock_fd==avoid_fd){ /* check if the connection has not been idle for too long ... */ if (client->fcl_user==NULL && now - client->fcl_idle_since > MAX_AUTH_TIME ){ warn("Connection with no auth for more than %ds : closing it.", MAX_AUTH_TIME); remove_connection(&client, prev_client); } else if ( now - client->fcl_idle_since > MAX_IDLE_TIME ) { warn("Connection of %s is idle for more than %ds : closing it.", client->fcl_user, MAX_IDLE_TIME); remove_connection(&client, prev_client); } else { /* nothing to do on this one ... check the next one */ prev_client = client; client = client->fcl_next; } continue; } if ( (read_len = recv(client->fcl_sock_fd, buf_int, sizeof(buf_int), 0)) <= 0 ) { if (read_len == 0) { /* connection closed by client */ remove_connection(&client, prev_client); } else { error_e("error recv() from sock fd %d", client->fcl_sock_fd); prev_client = client; client = client->fcl_next; } } else { client->fcl_cmd_len = read_len; client->fcl_cmd = buf_int; if ( client->fcl_user == NULL ) /* not authenticated yet */ auth_client(client); else { /* we've just read a command ... */ client->fcl_idle_since = now; exe_cmd(client); } prev_client = client; client = client->fcl_next; } } final_settings: /* copy master_set in read_set, because read_set is modified by select() */ read_set = master_set; }