PR_GetValueSem (PRSemaphore *sem) { sem_info info; PR_ASSERT(sem != NULL); get_sem_info(sem->sem, &info); return info.count; }
int main(int argc, char **argv) { team_info tinfo; int32 cookie = 0; int32 i; system_info sysinfo; // show up some stats first... get_system_info(&sysinfo); printf("sem: total: %5" B_PRIu32 ", used: %5" B_PRIu32 ", left: %5" B_PRIu32 "\n\n", sysinfo.max_sems, sysinfo.used_sems, sysinfo.max_sems - sysinfo.used_sems); if (argc == 1) { while (get_next_team_info( &cookie, &tinfo) == B_OK) list_sems(&tinfo); return 0; } for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { fprintf(stderr, "Usage: %s [-s semid] [teamid]\n", argv[0]); fputs(" List the semaphores allocated by the specified\n", stderr); fputs(" team, or all teams if none is specified.\n", stderr); fputs("\n", stderr); fputs(" The -s option displays the sem_info data for a\n", stderr); fputs(" specified semaphore.\n", stderr); return 0; } else if (strcmp(argv[i], "-s") == 0) { if (argc < i + 2) printf("-s used without associated sem id\n"); else { sem_id id = atoi(argv[i + 1]); sem_info info; if (get_sem_info(id, &info) == B_OK) { print_header(NULL); print_sem_info(&info); } else printf("semaphore %" B_PRId32 " unknown\n\n", id); i++; } } else { team_id team; team = atoi(argv[i]); if (get_team_info(team, &tinfo) == B_OK) list_sems(&tinfo); else printf("team %" B_PRId32 " unknown\n\n", team); } } return 0; }
int32 App::RelauncherThread(void *data) { App *app = (App*) data; sem_info quitSemInfo; get_sem_info(sQuitSem, &quitSemInfo); while (quitSemInfo.count > -1) { // wait until we need to check for a relaunch acquire_sem(sRelaunchSem); // We can afford to do this because while we have the semaphore, the // app itself is locked. BMessage *msg = app->fRelaunchQueue.NextMessage(); release_sem(sRelaunchSem); get_sem_info(sQuitSem, &quitSemInfo); if (quitSemInfo.count > 0) { if (msg) delete msg; break; } if (msg) { // Attempt to work around R5 shutdown procedures by checking for // debug_server running state. If it's shut down, then chances // are that the system is shutting down or has already done so. // We use the debug server in particular because it's one of // the few services that is available in safe mode which is // closed on system shutdown. if (be_roster->IsRunning("application/x-vnd.Be-DBSV")) { snooze(kRelaunchDelay); app->PostMessage(msg->what); delete msg; } } get_sem_info(sQuitSem, &quitSemInfo); } return 0; }
void MessageLooper::_GetLooperName(char* name, size_t length) { sem_id semaphore = Sem(); sem_info info; if (get_sem_info(semaphore, &info) == B_OK) strlcpy(name, info.name, length); else strlcpy(name, "unnamed looper", length); }
fssh_status_t _fssh_get_sem_info(fssh_sem_id id, struct fssh_sem_info *info, fssh_size_t infoSize) { sem_info systemInfo; status_t result = get_sem_info(id, &systemInfo); if (result != B_OK) return result; copy_sem_info(info, &systemInfo); return FSSH_B_OK; }
int main(int argc, char **argv) { int count = 1; bool force = false; sem_id semid = -1; status_t err; team_id teamid = -1; int i; if (argc < 2 || argc > 5) usage("release"); for (i = 1; i < argc; i++) { if (strncmp(argv[i], "-f", 2) == 0) { force = true; } else if (strncmp(argv[i], "-c", 2) == 0) { i++; if (i < argc) { count = strtol(argv[i], NULL, 10); } else usage("release"); } else { semid = strtol(argv[i], NULL, 10); } } if (semid <=0) usage("release"); if (force) { sem_info sinfo; thread_info tinfo; get_sem_info(semid, &sinfo); teamid = sinfo.team; get_thread_info(find_thread(NULL), &tinfo); set_sem_owner(semid, tinfo.team); } printf("releasing semaphore %d\n", (int)semid); err = release_sem_etc(semid, count, 0); if (err < B_OK) { fprintf(stderr, "release_sem failed: %s\n", strerror(err)); return 1; } else if (force) { set_sem_owner(semid, teamid); } return 0; }
static void printTeamThreads(team_info* teamInfo, bool printSemaphoreInfo) { const char* threadState; int32 threadCookie = 0; sem_info semaphoreInfo; thread_info threadInfo; // Print all info about its threads too while (get_next_thread_info(teamInfo->team, &threadCookie, &threadInfo) >= B_OK) { if (threadInfo.state < B_THREAD_RUNNING || threadInfo.state > B_THREAD_WAITING) // This should never happen threadState = "???"; else threadState = sStates[threadInfo.state - 1]; printf("%-37s %5" B_PRId32 " %8s %4" B_PRId32 " %8" B_PRIu64 " %8" B_PRId64 " ", threadInfo.name, threadInfo.thread, threadState, threadInfo.priority, (threadInfo.user_time / 1000), (threadInfo.kernel_time / 1000)); if (printSemaphoreInfo) { if (threadInfo.state == B_THREAD_WAITING && threadInfo.sem != -1) { status_t status = get_sem_info(threadInfo.sem, &semaphoreInfo); if (status == B_OK) { printf("%s(%" B_PRId32 ")\n", semaphoreInfo.name, semaphoreInfo.sem); } else { printf("%s(%" B_PRId32 ")\n", strerror(status), threadInfo.sem); } } else puts(""); } else puts(""); } }
int gtmrecv_upd_proc_init(boolean_t fresh_start) { /* Update Process initialization */ mstr upd_proc_log_cmd, upd_proc_trans_cmd; char upd_proc_cmd[UPDPROC_CMD_MAXLEN]; int status; int upd_status, save_upd_status; #ifdef UNIX pid_t upd_pid, waitpid_res; #elif defined(VMS) uint4 upd_pid; uint4 cmd_channel; $DESCRIPTOR(cmd_desc, UPDPROC_CMD_STR); #endif /* Check if the update process is alive */ if ((upd_status = is_updproc_alive()) == SRV_ERR) { gtm_putmsg(VARLSTCNT(7) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Receive pool semctl failure"), REPL_SEM_ERRNO); repl_errno = EREPL_UPDSTART_SEMCTL; return(UPDPROC_START_ERR); } else if (upd_status == SRV_ALIVE && !fresh_start) { gtm_putmsg(VARLSTCNT(4) ERR_TEXT, 2, RTS_ERROR_LITERAL("Update process already exists. Not starting it")); return(UPDPROC_EXISTS); } else if (upd_status == SRV_ALIVE) { gtm_putmsg(VARLSTCNT(6) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Update process already exists. Please kill it before a fresh start")); return(UPDPROC_EXISTS); } save_upd_status = recvpool.upd_proc_local->upd_proc_shutdown; recvpool.upd_proc_local->upd_proc_shutdown = NO_SHUTDOWN; #ifdef UNIX if (0 > (upd_pid = fork())) /* BYPASSOK: we exec immediately, no FORK_CLEAN needed */ { recvpool.upd_proc_local->upd_proc_shutdown = save_upd_status; gtm_putmsg(VARLSTCNT(7) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Could not fork update process"), errno); repl_errno = EREPL_UPDSTART_FORK; return(UPDPROC_START_ERR); } if (0 == upd_pid) { /* Update Process */ upd_proc_log_cmd.len = SIZEOF(UPDPROC_CMD) - 1; upd_proc_log_cmd.addr = UPDPROC_CMD; status = TRANS_LOG_NAME(&upd_proc_log_cmd, &upd_proc_trans_cmd, upd_proc_cmd, SIZEOF(upd_proc_cmd), dont_sendmsg_on_log2long); if (status != SS_NORMAL) { gtm_putmsg(VARLSTCNT(6) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Could not find path of Update Process. Check value of $gtm_dist")); if (SS_LOG2LONG == status) gtm_putmsg(VARLSTCNT(5) ERR_LOGTOOLONG, 3, LEN_AND_LIT(UPDPROC_CMD), SIZEOF(upd_proc_cmd) - 1); repl_errno = EREPL_UPDSTART_BADPATH; return(UPDPROC_START_ERR); } upd_proc_cmd[upd_proc_trans_cmd.len] = '\0'; if (EXECL(upd_proc_cmd, upd_proc_cmd, UPDPROC_CMD_ARG1, UPDPROC_CMD_ARG2, NULL) < 0) { gtm_putmsg(VARLSTCNT(7) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Could not exec Update Process"), errno); repl_errno = EREPL_UPDSTART_EXEC; return(UPDPROC_START_ERR); } } #elif defined(VMS) /* Create detached server and write startup commands to it */ status = repl_create_server(&cmd_desc, "GTMU", "", &cmd_channel, &upd_pid, ERR_RECVPOOLSETUP); if (SS_NORMAL != status) { gtm_putmsg(VARLSTCNT(7) ERR_RECVPOOLSETUP, 0, ERR_TEXT, 2, RTS_ERROR_LITERAL("Unable to spawn Update process"), status); recvpool.upd_proc_local->upd_proc_shutdown = save_upd_status; repl_errno = EREPL_UPDSTART_FORK; return(UPDPROC_START_ERR); } #endif if (recvpool.upd_proc_local->upd_proc_pid) recvpool.upd_proc_local->upd_proc_pid_prev = recvpool.upd_proc_local->upd_proc_pid; else recvpool.upd_proc_local->upd_proc_pid_prev = upd_pid; recvpool.upd_proc_local->upd_proc_pid = upd_pid; /* Receiver Server; wait for the update process to startup */ REPL_DPRINT2("Waiting for update process %d to startup\n", upd_pid); while (get_sem_info(RECV, UPD_PROC_COUNT_SEM, SEM_INFO_VAL) == 0 && is_proc_alive(upd_pid, 0)) { /* To take care of reassignment of PIDs, the while condition should be && with the * condition (PPID of pid == process_id) */ REPL_DPRINT2("Waiting for update process %d to startup\n", upd_pid); UNIX_ONLY(WAITPID(upd_pid, &status, WNOHANG, waitpid_res);) /* Release defunct update process if dead */ SHORT_SLEEP(GTMRECV_WAIT_FOR_SRV_START); }
int gtmsource_checkhealth(void) { uint4 gtmsource_pid; int status, semval, save_errno; boolean_t srv_alive, all_files_open; gtmsource_local_ptr_t gtmsourcelocal_ptr; int4 index, num_servers; seq_num reg_seqno, jnlseqno; gd_region *reg, *region_top; sgmnt_addrs *csa; sgmnt_data_ptr_t csd; char errtxt[OUT_BUFF_SIZE]; char *modestr; assert(holds_sem[SOURCE][JNL_POOL_ACCESS_SEM]); if (NULL != jnlpool.gtmsource_local) /* Check health of a specific source server */ gtmsourcelocal_ptr = jnlpool.gtmsource_local; else gtmsourcelocal_ptr = &jnlpool.gtmsource_local_array[0]; num_servers = 0; status = SRV_ALIVE; for (index = 0; index < NUM_GTMSRC_LCL; index++, gtmsourcelocal_ptr++) { if ('\0' == gtmsourcelocal_ptr->secondary_instname[0]) { assert(NULL == jnlpool.gtmsource_local); continue; } gtmsource_pid = gtmsourcelocal_ptr->gtmsource_pid; /* If CHECKHEALTH on a specific secondary instance is requested, print the health information irrespective * of whether a source server for that instance is alive or not. For CHECKHEALTH on ALL secondary instances * print health information only for those instances that have an active or passive source server alive. */ if ((NULL == jnlpool.gtmsource_local) && (0 == gtmsource_pid)) continue; repl_log(stdout, TRUE, TRUE, "Initiating CHECKHEALTH operation on source server pid [%d] for secondary instance" " name [%s]\n", gtmsource_pid, gtmsourcelocal_ptr->secondary_instname); srv_alive = (0 == gtmsource_pid) ? FALSE : is_proc_alive(gtmsource_pid, 0); if (srv_alive) { if (GTMSOURCE_MODE_ACTIVE == gtmsourcelocal_ptr->mode) modestr = "ACTIVE"; else if (GTMSOURCE_MODE_ACTIVE_REQUESTED == gtmsourcelocal_ptr->mode) modestr = "ACTIVE REQUESTED"; else if (GTMSOURCE_MODE_PASSIVE == gtmsourcelocal_ptr->mode) modestr = "PASSIVE"; else if (GTMSOURCE_MODE_PASSIVE_REQUESTED == gtmsourcelocal_ptr->mode) modestr = "PASSIVE REQUESTED"; else { assert(gtmsourcelocal_ptr->mode != gtmsourcelocal_ptr->mode); modestr = "UNKNOWN"; } repl_log(stderr, FALSE, TRUE, FORMAT_STR1, gtmsource_pid, "Source server", "", modestr); status |= SRV_ALIVE; num_servers++; } else { repl_log(stderr, FALSE, TRUE, FORMAT_STR, gtmsource_pid, "Source server", " NOT"); gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(4) ERR_SRCSRVNOTEXIST, 2, LEN_AND_STR(gtmsourcelocal_ptr->secondary_instname)); status |= SRV_DEAD; } if (NULL != jnlpool.gtmsource_local) break; } if (NULL == jnlpool.gtmsource_local) { /* Compare number of servers that were found alive with the current value of the COUNT semaphore. * If they are not equal, report the discrepancy. */ semval = get_sem_info(SOURCE, SRC_SERV_COUNT_SEM, SEM_INFO_VAL); if (-1 == semval) { save_errno = errno; repl_log(stderr, FALSE, TRUE, "Error fetching source server count semaphore value : %s\n", STRERROR(save_errno)); status |= SRV_ERR; } else if (semval != num_servers) { repl_log(stderr, FALSE, FALSE, "Error : Expected %d source server(s) to be alive but found %d actually alive\n", semval, num_servers); repl_log(stderr, FALSE, TRUE, "Error : Check if any pid reported above is NOT a source server process\n"); status |= SRV_ERR; } } /* Check that there are no regions with replication state = WAS_ON (i.e. repl_was_open). If so report that. * But to determine that, we need to attach to all the database regions. */ gvinit(); /* We use the same code dse uses to open all regions but we must make sure they are all open before proceeding. */ all_files_open = region_init(FALSE); if (!all_files_open) { gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(1) ERR_NOTALLDBOPN); status |= SRV_ERR; } else { for (reg = gd_header->regions, region_top = gd_header->regions + gd_header->n_regions; reg < region_top; reg++) { csa = &FILE_INFO(reg)->s_addrs; csd = csa->hdr; if (REPL_WAS_ENABLED(csd)) { assert(!JNL_ENABLED(csd) || REPL_ENABLED(csd)); /* || is for turning replication on concurrently */ reg_seqno = csd->reg_seqno; jnlseqno = (NULL != jnlpool.jnlpool_ctl) ? jnlpool.jnlpool_ctl->jnl_seqno : MAX_SEQNO; sgtm_putmsg(errtxt, VARLSTCNT(8) ERR_REPLJNLCLOSED, 6, DB_LEN_STR(reg), ®_seqno, ®_seqno, &jnlseqno, &jnlseqno); repl_log(stderr, FALSE, TRUE, errtxt); status |= SRV_ERR; } } } if (jnlpool.jnlpool_ctl->freeze) { repl_log(stderr, FALSE, FALSE, "Warning: Instance Freeze is ON\n"); repl_log(stderr, FALSE, TRUE, " Freeze Comment: %s\n", jnlpool.jnlpool_ctl->freeze_comment); status |= SRV_ERR; } return (status + NORMAL_SHUTDOWN); }
status_t thread_debug_thread(void *arg) { Tdebug_thead_param* param = (Tdebug_thead_param*) arg; #ifdef __HAIKU__ debug_thread(param->thread); #else // !__HAIKU__ thread_info thinfo; get_thread_info(param->thread, &thinfo); char text[4096]; sprintf(text, "db %d", int(param->thread)); system(text); if (param->sem >= 0 && thinfo.state == B_THREAD_WAITING && param->sem == thinfo.sem) { snooze(1000000); get_thread_info(param->thread, &thinfo); if (thinfo.state == B_THREAD_WAITING && param->sem == thinfo.sem && param->totalTime == thinfo.user_time + thinfo.kernel_time) { // the thread has been waiting for this semaphore since the before // the alert, not doing anything... Let's push it out of there! sem_info sinfo; thread_info thinfo; info_pack infos; if (get_sem_info(param->sem, &sinfo) == B_OK && get_thread_info(param->thread, &thinfo) == B_OK && get_team_info(thinfo.team, &infos.team_info) == B_OK) { sprintf (text, "This thread is waiting for the " "semaphore called \"%s\". As long as it waits for this " "semaphore, you won't be able to debug that thread.\n", sinfo.name); if (sinfo.team == thinfo.team) strcat(text, "This semaphore belongs to the " "thread's team.\n\nShould I release this semaphore?\n"); else { get_team_name_and_icon(infos); char moreText[1024]; sprintf(moreText, "\nWARNING! This semaphore " "belongs to the team \"%s\"!\n\nShould I release this " "semaphore anyway?\n", infos.team_name); strcat(text, moreText); } BAlert* alert = new BAlert("", text, "Cancel", "Release", NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); alert->SetShortcut(0, B_ESCAPE); if (alert->Go()) { get_thread_info (param->thread, &thinfo); if (thinfo.state == B_THREAD_WAITING && param->sem == thinfo.sem && param->totalTime == thinfo.user_time + thinfo.kernel_time) release_sem(param->sem); else { alert = new BAlert("", "The semaphore wasn't released, " "because it wasn't necessary anymore!", "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); alert->Go(); } } } } } #endif // !__HAIKU__ delete param; return B_OK; }