int main(int argc, char **argv) { char *src, *target = NULL, *hw; struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_usec); take_lock(); signal(SIGSEGV,sighandler); signal(SIGKILL,sighandler); signal(SIGTERM,sighandler); signal(SIGSEGV,sighandler); signal(SIGALRM,sighandler); alarm(10); src = getenv("INTERFACE"); if (!src) goto out_unlock; configs = get_configs(); hw = get_hwaddr(src); if (!hw) goto out_unlock; // target = get_config_by_hwaddr(hw, src); if (!target) { char *path = NULL; char *contents = NULL; if (asprintf(&path, "/sys/class/net/%s/ifindex", src) == -1) goto out_unlock ; g_file_get_contents(path, &contents, NULL, NULL); free(path); printf("eth%u", atoi(contents) - 2); } out_unlock: unlink(LOCKFILE); exit(0); }
static int try_lock() { struct lockdaemon *ldp; int i; switch (the_lock.type) { case LOCK_READ: if (lock_wanted.type == LOCK_READ) { i = the_lock.nholders++; the_lock.holding_pid[i] = lock_wanted.pid; the_lock.state = STATE_CLEAR; DPF((stderr, "increment read lockers to %d\n", the_lock.nholders)); take_lock(LOCK_LOCKED); break; } /* write lock has to wait */ break; case LOCK_WRITE: /* lock has to wait until write lock is cleared */ break; case LOCK_NOTLOCKED: if (lock_wanted.type == LOCK_READ) { DPF((stderr, "local locker, 1 lock holder\n")); the_lock.holding_pid[0] = lock_wanted.pid; the_lock.nholders = 1; the_lock.type = LOCK_READ; the_lock.state = STATE_CLEAR; the_lock.remote_daemon = NULL; take_lock(LOCK_LOCKED); return (1); } if (islocalhost(&lock_wanted.remote)) { DPF((stderr, "local locker, take write lock\n")); /* tell everyone I'm locking */ if (lock_wanted.state != STATE_ASKED) { for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) { if (ldp->inuse == 0) break; ldp->state = STATE_ASKED; send_lockmsg(WRITE_LOCK, (pid_t)0, &(ldp->host), 0); } } lock_wanted.state = STATE_ASKED; check_for_write_lock(); the_lock.remote_daemon = NULL; the_lock.state = STATE_ASKED; return (0); } else { DPF((stderr, "remote locker, take write lock\n")); the_lock.type = LOCK_WRITE; the_lock.holder = lock_wanted.remote; the_lock.nholders = 1; the_lock.remote_daemon = find_lockdaemon(&the_lock.holder); the_lock.state = STATE_CLEAR; /* okay to remote */ take_lock(GRANTED); } break; default: DPF((stderr, "weird lock type held - %d\n", the_lock.type)); the_lock.type = LOCK_NOTLOCKED; break; } return (0); }
int take_write_lock(void *session) { return take_lock(& ((biterc_session_t *) session)->write_lock); }
static int biter_connect(const char *tmpdir, biterc_session_t *session) { char c2s_path[MAX_PATH_LEN+1], s2c_path[MAX_PATH_LEN+1]; int result, c2s_fd = -1, s2c_fd = -1, pipe_lock_held = 0; uint32_t local_id, all_connected, rank; struct stat buf; int unique_number; unique_number = biterc_get_unique_number(&session->shared_header->max_rank, &session->shared_header->num_set_max_rank, session); rank = biterc_get_rank(sessions - session); snprintf(c2s_path, MAX_PATH_LEN+1, "%s/biter_c2s.%d", tmpdir, unique_number); c2s_path[MAX_PATH_LEN] = '\0'; snprintf(s2c_path, MAX_PATH_LEN+1, "%s/biter_s2c.%d", tmpdir, unique_number); s2c_path[MAX_PATH_LEN] = '\0'; if (session->leader) { take_lock(&session->pipe_lock); pipe_lock_held = 1; while ((result = stat(c2s_path, &buf)) == -1 && errno == ENOENT) usleep(10000); //.01 sec if (result != 0) { biter_lasterror = "Could not stat client to server path"; goto error; } while ((result = stat(s2c_path, &buf)) == -1 && errno == ENOENT) usleep(10000); //.01 sec if (result != 0) { biter_lasterror = "Could not stat server to client path"; goto error; } session->shared_header->ready = 1; } else { while (session->shared_header->ready == 0) usleep(10000); //.01 sec take_lock(&session->pipe_lock); pipe_lock_held = 1; } c2s_fd = open(c2s_path, O_WRONLY); if (c2s_fd == -1) { biter_lasterror = "Unable to open client to server path"; goto error; } s2c_fd = open(s2c_path, O_RDONLY); if (s2c_fd == -1) { biter_lasterror = "Unable to open server to client path"; goto error; } local_id = (uint32_t) session->id; result = write(c2s_fd, &local_id, sizeof(local_id)); if (result == -1) { biter_lasterror = "Unable to send initialization id to server"; goto error; } result = write(c2s_fd, &rank, sizeof(rank)); if (result == -1) { biter_lasterror = "Unable to send rank to server"; goto error; } result = read(s2c_fd, &all_connected, sizeof(all_connected)); if (result == -1) { biter_lasterror = "Unable to recieved connection message from server"; goto error; } release_lock(&session->pipe_lock); pipe_lock_held = 0; if (all_connected) { session->shared_header->num_procs = all_connected; MEMORY_BARRIER; session->shared_header->ready = 2; } else { while (session->shared_header->ready != 2) { usleep(10000); //.01 sec } } session->c2s_fd = c2s_fd; session->s2c_fd = s2c_fd; return 0; error: if (pipe_lock_held) release_lock(&session->pipe_lock); if (s2c_fd != -1) close(s2c_fd); if (c2s_fd != -1) close(c2s_fd); return -1; }
int take_queue_lock(void *session) { return take_lock(& ((biterc_session_t *) session)->queue_lock); }
int take_heap_lock(void *session) { return take_lock(& ((biterc_session_t *) session)->mem_lock); }