static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd, BacktraceMap* backtrace_map, const std::set<pid_t>& siblings) { if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno)); return false; } int total_sleep_time_usec = 0; while (true) { int signal = wait_for_signal(request.tid, &total_sleep_time_usec); switch (signal) { case -1: ALOGE("debuggerd: timed out waiting for signal"); return false; case SIGSTOP: if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { ALOGV("debuggerd: stopped -- dumping to tombstone"); engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal, request.original_si_code, request.abort_msg_address); } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { ALOGV("debuggerd: stopped -- dumping to fd"); dump_backtrace(fd, -1, backtrace_map, request.pid, request.tid, siblings); } else { ALOGV("debuggerd: stopped -- continuing"); if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) { ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno)); return false; } continue; // loop again } break; case SIGABRT: case SIGBUS: case SIGFPE: case SIGILL: case SIGSEGV: #ifdef SIGSTKFLT case SIGSTKFLT: #endif case SIGTRAP: ALOGV("stopped -- fatal signal\n"); // Send a SIGSTOP to the process to make all of // the non-signaled threads stop moving. Without // this we get a lot of "ptrace detach failed: // No such process". kill(request.pid, SIGSTOP); engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal, request.original_si_code, request.abort_msg_address); break; default: ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal); break; } break; } return true; }
static void handle_request(int fd) { ALOGV("handle_request(%d)\n", fd); ScopedFd closer(fd); debugger_request_t request; memset(&request, 0, sizeof(request)); int status = read_request(fd, &request); if (status != 0) { return; } ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid, request.gid, request.tid); #if defined(__LP64__) // On 64 bit systems, requests to dump 32 bit and 64 bit tids come // to the 64 bit debuggerd. If the process is a 32 bit executable, // redirect the request to the 32 bit debuggerd. if (is32bit(request.tid)) { // Only dump backtrace and dump tombstone requests can be redirected. if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { redirect_to_32(fd, &request); } else { ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action); } return; } #endif // Fork a child to handle the rest of the request. pid_t fork_pid = fork(); if (fork_pid == -1) { ALOGE("debuggerd: failed to fork: %s\n", strerror(errno)); return; } else if (fork_pid != 0) { waitpid(fork_pid, nullptr, 0); return; } // Open the tombstone file if we need it. std::string tombstone_path; int tombstone_fd = -1; switch (request.action) { case DEBUGGER_ACTION_DUMP_TOMBSTONE: case DEBUGGER_ACTION_CRASH: tombstone_fd = open_tombstone(&tombstone_path); if (tombstone_fd == -1) { ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno)); exit(1); } break; case DEBUGGER_ACTION_DUMP_BACKTRACE: break; default: ALOGE("debuggerd: unexpected request action: %d", request.action); exit(1); } // At this point, the thread that made the request is blocked in // a read() call. If the thread has crashed, then this gives us // time to PTRACE_ATTACH to it before it has a chance to really fault. // // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it // won't necessarily have stopped by the time ptrace() returns. (We // currently assume it does.) We write to the file descriptor to // ensure that it can run as soon as we call PTRACE_CONT below. // See details in bionic/libc/linker/debugger.c, in function // debugger_signal_handler(). // Attach to the target process. if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) { ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno)); exit(1); } // Don't attach to the sibling threads if we want to attach gdb. // Supposedly, it makes the process less reliable. bool attach_gdb = should_attach_gdb(&request); int signal_in_fd = -1; int signal_out_fd = -1; pid_t signal_pid = 0; if (attach_gdb) { // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges. if (init_getevent() != 0) { ALOGE("debuggerd: failed to initialize input device, not waiting for gdb"); attach_gdb = false; } // Fork a process that stays root, and listens on a pipe to pause and resume the target. if (!fork_signal_sender(&signal_in_fd, &signal_out_fd, &signal_pid, request.pid)) { attach_gdb = false; } } auto notify_signal_sender = [=]() { char buf[1]; if (TEMP_FAILURE_RETRY(write(signal_in_fd, "", 1)) != 1) { ALOGE("debuggerd: failed to notify signal process: %s", strerror(errno)); } else if (TEMP_FAILURE_RETRY(read(signal_out_fd, buf, 1)) != 1) { ALOGE("debuggerd: failed to read response from signal process: %s", strerror(errno)); } }; std::set<pid_t> siblings; if (!attach_gdb) { ptrace_siblings(request.pid, request.tid, siblings); } // Generate the backtrace map before dropping privileges. std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid)); bool succeeded = false; // Now that we've done everything that requires privileges, we can drop them. if (drop_privileges()) { succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings); if (succeeded) { if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { if (!tombstone_path.empty()) { write(fd, tombstone_path.c_str(), tombstone_path.length()); } } } if (attach_gdb) { // Tell the signal process to send SIGSTOP to the target. notify_signal_sender(); } } if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) { ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno)); } for (pid_t sibling : siblings) { ptrace(PTRACE_DETACH, sibling, 0, 0); } // Wait for gdb, if requested. if (attach_gdb && succeeded) { wait_for_user_action(request); // Tell the signal process to send SIGCONT to the target. notify_signal_sender(); uninit_getevent(); waitpid(signal_pid, nullptr, 0); } exit(!succeeded); }
int main(int argc, char **argv) { char **argv_exec, **additional_argv; int additional_argc; char c; additional_argv = NULL; additional_argc = 0; user_specified_pager = NULL; opterr = 0; while ((c = getopt(argc, argv, "+a:p:")) != -1) { switch(c) { case 'a': additional_argc++; additional_argv = (char **)realloc(additional_argv, additional_argc*sizeof(char *)); additional_argv[additional_argc-1] = optarg; break; case 'p': user_specified_pager = optarg; break; default: break; } } argv_exec = &argv[optind]; if (isatty(1)) { if (*argv_exec) { /* Run the command through a pipe */ pid_t child_pid; int fds[2]; if (additional_argc > 0) { /* Add the additional arguments specified in the command line*/ int i,j; argv_exec = (char **)malloc((argc-optind+additional_argc+1)*sizeof(char *)); for (i=0; i<(argc-optind); i++) argv_exec[i] = argv[i+optind]; for (j=0; j<additional_argc; i++, j++) argv_exec[i] = additional_argv[j]; argv_exec[i] = NULL; } if (pipe(fds) == -1) { perror("pipe"); exit(EXIT_FAILURE); } child_pid = fork(); if (child_pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (!child_pid) { /* The child is the subcommand, its stdout and stderr are captured */ close(fds[0]); safe_dup2(fds[1], 1); if (isatty(2)) safe_dup2(fds[1], 2); execvp_noret(argv_exec); } /* The parent runs the pager */ close(fds[1]); safe_dup2(fds[0], 0); } launch_pager(); } else { if (*argv_exec) { execvp_noret(argv_exec); } else { ssize_t r; char buf[BUFSIZ]; while ((r = read(0, buf, BUFSIZ)) > 0) { if (write(1, buf, r) == -1) { perror("write"); exit(EXIT_FAILURE); } } if (r == -1) { perror("read"); exit(EXIT_FAILURE); } return EXIT_SUCCESS; } } return EXIT_FAILURE; /* This shouldn't be reachable */ }
//********************************************************* int server(void) { struct sockaddr_in monadr, sonadr; int fd, opt=1, taille; int serv_sock, client_sock; int nb_lu; int nfds; fd_set rfds, afds; int error; char message[20]=""; char response[20]=""; MyLog("Create server ...\n"); serv_sock = socket(PF_INET, SOCK_STREAM, 0); setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR,(char *) &opt, sizeof(opt)); /* init socket serveur */ bzero( (char *)&monadr, sizeof monadr); monadr.sin_family = AF_INET; monadr.sin_port = htons(PORT); monadr.sin_addr.s_addr = INADDR_ANY; if( bind(serv_sock, (struct sockaddr *)&monadr, sizeof(monadr)) == -1 ) { return(1); } if( listen(serv_sock, MAXCONN) == -1 ) { return(1); } /* init sockets list*/ nfds = getdtablesize(); FD_ZERO(&afds); FD_SET(serv_sock, &afds); /* gestion des clients */ while(!Terminated) { memcpy(&rfds, &afds, sizeof(rfds)); if( select(nfds, &rfds, 0, 0, 0) == -1 ) {/* signaux non bloquants */ if( errno == EINTR ) continue; return(1); } if( FD_ISSET(serv_sock, &rfds) ) /* New connection ?*/ { taille = sizeof sonadr; if( (client_sock = accept(serv_sock, (struct sockaddr *)&sonadr,(socklen_t *)&taille)) == -1 ) { return(1); } //syslog(LOG_NOTICE,"client connection from %s \n", inet_ntoa(sonadr.sin_addr)); fflush(stdout); FD_SET(client_sock, &afds); fcntl(client_sock, F_SETFL, O_NONBLOCK | fcntl(client_sock, F_GETFL, 0)); } /* Tester si les sockets clientes ont boug�s */ for( fd=0; fd<nfds; fd++ ) { if( fd != serv_sock && FD_ISSET(fd, &rfds) ) { /* traiter le client */ strcpy(message,""); strcpy(response,""); nb_lu = read(fd, message, BUFSIZE); //MyLog("nb_lu : %d/%d/%d\n",nb_lu,strlen(message),fd); if( nb_lu > 0 ) { message[nb_lu]='\0'; if ((error=read_socket(message,response))!=SUCCES) { sprintf(response,"error :%d\n",error); } strcat(response,"\n"); write(fd, response, strlen(response)); } else { close(fd); FD_CLR(fd, &afds); } } } } return(0); }
/************************************************************************************************** * @fn HalGpioSrdyInit * * * @brief Initialise SRDY GPIO. * * @param gpioCfg - SRDY pin configuration parameters * * @return STATUS **************************************************************************************************/ int HalGpioSrdyInit(halGpioCfg_t *gpioCfg) { memcpy(srdyGpioCfg.gpio.value, gpioCfg->gpio.value, strlen(gpioCfg->gpio.value)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] srdyGpioCfg.gpio.value = '%s'\n", __FUNCTION__, srdyGpioCfg.gpio.value); } memcpy(srdyGpioCfg.gpio.direction, gpioCfg->gpio.direction, strlen(gpioCfg->gpio.direction)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] srdyGpioCfg.gpio.direction = '%s'\n", __FUNCTION__, srdyGpioCfg.gpio.direction); } srdyGpioCfg.gpio.active_high_low = gpioCfg->gpio.active_high_low; memcpy(srdyGpioCfg.gpio.edge, gpioCfg->gpio.edge, strlen(gpioCfg->gpio.edge)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] srdyGpioCfg.gpio.edge = '%s'\n", __FUNCTION__, srdyGpioCfg.gpio.edge); } if ( ( gpioCfg->levelshifter.value) && ( gpioCfg->levelshifter.active_high_low) && ( gpioCfg->levelshifter.direction)) { memcpy(srdyGpioCfg.levelshifter.value, gpioCfg->levelshifter.value, strlen(gpioCfg->levelshifter.value)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] srdyGpioCfg.levelshifter.value = '%s'\n", __FUNCTION__, srdyGpioCfg.levelshifter.value); } memcpy(srdyGpioCfg.levelshifter.direction, gpioCfg->levelshifter.direction, strlen(gpioCfg->levelshifter.direction)); srdyGpioCfg.levelshifter.active_high_low = gpioCfg->levelshifter.active_high_low; if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] srdyGpioCfg.levelshifter.direction = '%s'\n", __FUNCTION__, srdyGpioCfg.levelshifter.direction); } //open the GPIO DIR file for the level shifter direction signal gpioSrdyFd = open(srdyGpioCfg.levelshifter.direction, O_RDWR); if(gpioSrdyFd == 0) { perror(srdyGpioCfg.levelshifter.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, srdyGpioCfg.levelshifter.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_LVLSHFT_DIR_OPEN; return NPI_LNX_FAILURE; } //Set the direction of the GPIO to output if (ERROR == write(gpioSrdyFd, "out", 3)) { perror(srdyGpioCfg.levelshifter.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s \n", __FUNCTION__, srdyGpioCfg.levelshifter.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_LVLSHFT_DIR_WRITE; return NPI_LNX_FAILURE; } //close the DIR file close(gpioSrdyFd); //open the GPIO VALUE file for the level shifter direction signal gpioSrdyFd = open(srdyGpioCfg.levelshifter.value, O_RDWR); if(gpioSrdyFd == 0) { perror(srdyGpioCfg.levelshifter.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, srdyGpioCfg.levelshifter.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_LVLSHFT_VAL_OPEN; return NPI_LNX_FAILURE; } //Set the value of the GPIO to 0 (level shifter direction from CC2531 to Host) if (ERROR == write(gpioSrdyFd, "0", 1)) { perror(srdyGpioCfg.levelshifter.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, srdyGpioCfg.levelshifter.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_LVLSHFT_VAL_WRITE; return NPI_LNX_FAILURE; } //close the DIR file close(gpioSrdyFd); } else { if (__BIG_DEBUG_ACTIVE == TRUE) { LOG_ALWAYS("[%s] Wrong Configuration File, one of the following Key value are missing for SRDY.Level Shifter definition: '\n", __FUNCTION__); LOG_ALWAYS("value: \t\t%s\n", srdyGpioCfg.gpio.value); LOG_ALWAYS("direction: \t%s\n", srdyGpioCfg.gpio.direction); LOG_ALWAYS("active_high_low: %d\n", srdyGpioCfg.gpio.active_high_low); LOG_ALWAYS("Level Shifter is optional, please check if you need it or not before continuing...\n"); } } //TODO: Lock the shift register GPIO. //open the SRDY GPIO DIR file gpioSrdyFd = open(srdyGpioCfg.gpio.direction, O_RDWR); if(gpioSrdyFd == 0) { perror(srdyGpioCfg.gpio.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, srdyGpioCfg.gpio.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_GPIO_DIR_OPEN; return NPI_LNX_FAILURE; } //Set SRDY GPIO as input if(ERROR == write(gpioSrdyFd, "in", 2)) { perror(srdyGpioCfg.levelshifter.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, srdyGpioCfg.gpio.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_GPIO_DIR_WRITE; return NPI_LNX_FAILURE; } //close SRDY DIR file close(gpioSrdyFd); //open the SRDY GPIO Edge file gpioSrdyFd = open(srdyGpioCfg.gpio.edge, O_RDWR); if(gpioSrdyFd == 0) { perror(srdyGpioCfg.gpio.edge); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, srdyGpioCfg.gpio.edge); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_GPIO_EDGE_OPEN; return NPI_LNX_FAILURE; } //Set SRDY GPIO edge detection for both rising and falling if(ERROR == write(gpioSrdyFd, "both", 4)) { perror(srdyGpioCfg.levelshifter.edge); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, srdyGpioCfg.gpio.edge); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_GPIO_EDGE_WRITE; return NPI_LNX_FAILURE; } //close SRDY edge file close(gpioSrdyFd); //open the SRDY GPIO VALUE file so it can be written to using the file handle later gpioSrdyFd = open(srdyGpioCfg.gpio.value, O_RDWR| O_NONBLOCK); if(gpioSrdyFd == 0) { perror(srdyGpioCfg.gpio.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, srdyGpioCfg.gpio.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_SRDY_GPIO_VAL_OPEN; return NPI_LNX_FAILURE; } return(gpioSrdyFd); }
/************************************************************************************************** * @fn HalGpioResetInit * * * @brief Initialise RESET GPIO. * * @param gpioCfg - Reset pin configuration parameters * * @return STATUS **************************************************************************************************/ int HalGpioResetInit(halGpioCfg_t *gpioCfg) { memcpy(resetGpioCfg.gpio.value, gpioCfg->gpio.value, strlen(gpioCfg->gpio.value)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] resetGpioCfg.gpio.value = '%s'\n", __FUNCTION__, resetGpioCfg.gpio.value); } memcpy(resetGpioCfg.gpio.direction, gpioCfg->gpio.direction, strlen(gpioCfg->gpio.direction)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] resetGpioCfg.gpio.direction = '%s'\n", __FUNCTION__, resetGpioCfg.gpio.direction); } resetGpioCfg.gpio.active_high_low = gpioCfg->gpio.active_high_low; if ( ( gpioCfg->levelshifter.value) && ( gpioCfg->levelshifter.active_high_low) && ( gpioCfg->levelshifter.direction)) { memcpy(resetGpioCfg.levelshifter.value, gpioCfg->levelshifter.value, strlen(gpioCfg->levelshifter.value)); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] resetGpioCfg.levelshifter.value = '%s'\n", __FUNCTION__, resetGpioCfg.levelshifter.value); } memcpy(resetGpioCfg.levelshifter.direction, gpioCfg->levelshifter.direction, strlen(gpioCfg->levelshifter.direction)); resetGpioCfg.levelshifter.active_high_low = gpioCfg->levelshifter.active_high_low; if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] resetGpioCfg.levelshifter.direction = '%s'\n", __FUNCTION__, resetGpioCfg.levelshifter.direction); } //open the GPIO DIR file for the level shifter direction signal gpioResetFd = open(resetGpioCfg.levelshifter.direction, O_RDWR); if(gpioResetFd == 0) { perror(resetGpioCfg.levelshifter.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, resetGpioCfg.levelshifter.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_LVLSHFT_DIR_OPEN; return NPI_LNX_FAILURE; } //Set the direction of the GPIO to output if (ERROR == write(gpioResetFd, "out", 3)) { perror(resetGpioCfg.levelshifter.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s \n", __FUNCTION__, resetGpioCfg.levelshifter.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_LVLSHFT_DIR_WRITE; return NPI_LNX_FAILURE; } //close the DIR file close(gpioResetFd); //open the GPIO VALUE file for the level shifter direction signal gpioResetFd = open(resetGpioCfg.levelshifter.value, O_RDWR); if(gpioResetFd == 0) { perror(resetGpioCfg.levelshifter.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, resetGpioCfg.levelshifter.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_LVLSHFT_VAL_OPEN; return NPI_LNX_FAILURE; } //Set the value of the GPIO to 0 (level shifter direction from Host to CC2531) if(ERROR == write(gpioResetFd, "1", 1)) { perror(resetGpioCfg.levelshifter.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, resetGpioCfg.levelshifter.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_LVLSHFT_VAL_WRITE; return NPI_LNX_FAILURE; } //close the DIR file close(gpioResetFd); } else { if (__BIG_DEBUG_ACTIVE == TRUE) { LOG_ALWAYS("[%s] Wrong Configuration File, one of the following Key value are missing for MRDY.Level Shifter definition: '\n", __FUNCTION__); LOG_ALWAYS("value: \t\t%s\n", resetGpioCfg.gpio.value); LOG_ALWAYS("direction: \t%s\n", resetGpioCfg.gpio.direction); LOG_ALWAYS("active_high_low: %d\n", resetGpioCfg.gpio.active_high_low); LOG_ALWAYS("Level Shifter is optional, please check if you need it or not before continuing...\n"); } } //open the RESET GPIO DIR file gpioResetFd = open(resetGpioCfg.gpio.direction, O_RDWR); if(gpioResetFd == 0) { perror(resetGpioCfg.gpio.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, resetGpioCfg.gpio.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_GPIO_DIR_OPEN; return NPI_LNX_FAILURE; } //Set RESET GPIO as output if(ERROR == write(gpioResetFd, "out", 3)) { perror(resetGpioCfg.gpio.direction); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, resetGpioCfg.gpio.direction); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_GPIO_DIR_WRITE; return NPI_LNX_FAILURE; } //close RESET DIR file close(gpioResetFd); //open the RESET GPIO VALUE file so it can be written to using the file handle later gpioResetFd = open(resetGpioCfg.gpio.value, O_RDWR); if(gpioResetFd == 0) { perror(resetGpioCfg.gpio.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] %s open failed\n", __FUNCTION__, resetGpioCfg.gpio.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_GPIO_VAL_OPEN; return NPI_LNX_FAILURE; } //Set RESET GPIO to 1 as default if(ERROR == write(gpioResetFd, "1", 3)) { perror(resetGpioCfg.gpio.value); if (__BIG_DEBUG_ACTIVE == TRUE) { time_printf("[%s] can't write in %s\n", __FUNCTION__, resetGpioCfg.gpio.value); } npi_ipc_errno = NPI_LNX_ERROR_HAL_GPIO_RESET_GPIO_VAL_WRITE; return NPI_LNX_FAILURE; } return NPI_LNX_SUCCESS; }
int main(int argc, char *argv[]) { struct exec head; struct ecoff_exechdr ehead; struct ecoff_scnhdr escn[3]; int infd, outfd; int n; if (argc != 3) usage(); infd = open(argv[1], O_RDONLY); if (infd < 0) err(1, argv[1]); outfd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644); if (outfd < 0) err(1, argv[2]); n = read(infd, &head, sizeof(head)); if (n < sizeof(head)) err(1, "read"); if (N_BADMAG(head)) { printf("%s: bad magic number\n", argv[1]); exit(1); } if (head.a_trsize || head.a_drsize) { printf("%s: has relocations\n", argv[1]); exit(1); } /* * Header */ ehead.f.f_magic = 0x016d; /* MC88OMAGIC */ ehead.f.f_nscns = 3; ehead.f.f_timdat = 0; /* ignored */ ehead.f.f_symptr = 0; /* ignored */ ehead.f.f_nsyms = 0; /* ignored */ ehead.f.f_opthdr = sizeof ehead.a; ehead.f.f_flags = 0x020f; /* F_RELFLG | F_EXEC | F_LNNO | 8 | F_AR16WR */ ehead.a.magic = N_GETMAGIC(head); ehead.a.vstamp = 0; /* ignored */ ehead.a.tsize = head.a_text; /* ignored */ ehead.a.dsize = head.a_data; /* ignored */ ehead.a.bsize = head.a_bss; /* ignored */ ehead.a.entry = head.a_entry; ehead.a.text_start = N_TXTADDR(head); /* ignored */ ehead.a.data_start = N_DATADDR(head); /* ignored */ n = write(outfd, &ehead, sizeof(ehead)); if (n != sizeof(ehead)) err(1, "write"); /* * Sections. * Note that we merge .bss into .data since the PROM will not * clear it and locore does not do this either. */ strncpy(escn[0].s_name, ".text", sizeof escn[0].s_name); escn[0].s_paddr = N_TXTADDR(head); /* ignored, 1:1 mapping */ escn[0].s_size = round(head.a_text, 8); escn[0].s_scnptr = round(sizeof(ehead) + sizeof(escn), 0x10); escn[0].s_relptr = 0; escn[0].s_lnnoptr = 0; escn[0].s_nlnno = 0; escn[0].s_flags = 0x20; /* STYP_TEXT */ strncpy(escn[1].s_name, ".data", sizeof escn[1].s_name); escn[1].s_paddr = N_DATADDR(head); /* ignored, 1:1 mapping */ escn[1].s_scnptr = escn[0].s_scnptr + escn[0].s_size; escn[1].s_size = round(head.a_data + head.a_bss, 8); escn[1].s_relptr = 0; escn[1].s_lnnoptr = 0; escn[1].s_nlnno = 0; escn[1].s_flags = 0x40; /* STYP_DATA */ strncpy(escn[2].s_name, ".bss", sizeof escn[2].s_name); escn[2].s_paddr = N_BSSADDR(head) + head.a_bss; /* ignored, 1:1 mapping */ escn[2].s_scnptr = 0; /* nothing in the file */ escn[2].s_size = 0; escn[2].s_relptr = 0; escn[2].s_lnnoptr = 0; escn[2].s_nlnno = 0; escn[2].s_flags = 0x80; /* STYP_BSS */ /* adjust load addresses */ escn[0].s_paddr += (head.a_entry & ~(__LDPGSZ - 1)) - __LDPGSZ; escn[1].s_paddr += (head.a_entry & ~(__LDPGSZ - 1)) - __LDPGSZ; escn[2].s_paddr += (head.a_entry & ~(__LDPGSZ - 1)) - __LDPGSZ; escn[0].s_vaddr = escn[0].s_paddr; escn[1].s_vaddr = escn[1].s_paddr; escn[2].s_vaddr = escn[2].s_paddr; n = write(outfd, &escn, sizeof(escn)); if (n != sizeof(escn)) err(1, "write"); /* * Copy text section */ #ifdef DEBUG printf("copying %s: source %lx dest %lx size %x\n", escn[0].s_name, N_TXTOFF(head), escn[0].s_scnptr, head.a_text); #endif lseek(outfd, escn[0].s_scnptr, SEEK_SET); lseek(infd, N_TXTOFF(head), SEEK_SET); copybits(infd, outfd, head.a_text); /* * Copy data section */ #ifdef DEBUG printf("copying %s: source %lx dest %lx size %x\n", escn[1].s_name, N_DATOFF(head), escn[1].s_scnptr, head.a_data); #endif lseek(outfd, escn[1].s_scnptr, SEEK_SET); lseek(infd, N_DATOFF(head), SEEK_SET); copybits(infd, outfd, head.a_data); /* * ``Copy'' bss section */ #ifdef DEBUG printf("copying %s: size %lx\n", escn[2].s_name, round(head.a_data + head.a_bss, 8) - head.a_data); #endif zerobits(outfd, round(head.a_data + head.a_bss, 8) - head.a_data); close(infd); close(outfd); exit(0); }
bool USBHID::send(HID_REPORT *report) { return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); }
void _profdump(void) { int f; long n; Plink *p; char *vp; char filename[64]; if (_tos->prof.what == 0) return; /* No profiling */ if (_tos->prof.pid != 0 && _tos->pid != _tos->prof.pid) return; /* Not our process */ if(perr) err("%lud Prof errors\n", perr); _tos->prof.pp = NULL; if (_tos->prof.pid) snprintf(filename, sizeof filename - 1, "prof.%ld", _tos->prof.pid); else snprintf(filename, sizeof filename - 1, "prof.out"); f = creat(filename, 0666); if(f < 0) { err("%s: cannot create - %s\n", filename, strerror(errno)); return; } _tos->prof.pid = ~0; /* make sure data gets dumped once */ switch(_tos->prof.what){ case Profkernel: _cycles((uvlong*)&_tos->prof.first->time); _tos->prof.first->time = _tos->prof.first->time + _tos->pcycles; break; case Profuser: _cycles((uvlong*)&_tos->prof.first->time); _tos->prof.first->time = _tos->prof.first->time - _tos->kcycles; break; case Proftime: _cycles((uvlong*)&_tos->prof.first->time); break; case Profsample: _tos->prof.first->time = _tos->clock; break; } vp = (char*)_tos->prof.first; for(p = _tos->prof.first; p <= _tos->prof.next; p++) { /* * short down */ n = 0xffff; if(p->down) n = p->down - _tos->prof.first; vp[0] = n>>8; vp[1] = n; /* * short right */ n = 0xffff; if(p->link) n = p->link - _tos->prof.first; vp[2] = n>>8; vp[3] = n; vp += 4; /* * long pc */ n = p->pc; vp[0] = n>>24; vp[1] = n>>16; vp[2] = n>>8; vp[3] = n; vp += 4; /* * long count */ n = p->count; vp[0] = n>>24; vp[1] = n>>16; vp[2] = n>>8; vp[3] = n; vp += 4; /* * vlong time */ if (havecycles){ n = (vlong)(p->time / (vlong)khz); }else n = p->time; vp[0] = n>>24; vp[1] = n>>16; vp[2] = n>>8; vp[3] = n; vp += 4; } write(f, (char*)_tos->prof.first, vp - (char*)_tos->prof.first); close(f); }
static unsigned int remnode_send(remnode_item *remnode, pwr_sClass_RemTrans *remtrans, char *buf, int buf_size) { unsigned int sts, i; unsigned int size_of_telegram, datasize; unsigned int number_of_DLE = 0; unsigned int delta_pos = 0; unsigned int pos_counter = 0; unsigned int follow_on = FALSE; unsigned int A_telegram = FALSE; unsigned char ch, cpu_number, CPU; unsigned char BCC = DLE ^ ETX; unsigned char datasize_low_byte, datasize_high_byte; unsigned char received_char = '\0'; unsigned char response_buffer[RESP_MESSAGE_SIZE]; unsigned char *restore_buf_ptr = (unsigned char *)buf; static unsigned char sstx[2] = {STX, '\0'}; static unsigned char sdle[2] = {DLE, '\0'}; static unsigned char snak[2] = {NAK, '\0'}; fd_set read_fd; struct timeval tv; /* Define complete telegrams for sending */ struct{ unsigned char telegram_header[HEADER_SIZE]; unsigned char telegram[MAX_SIZE_DATA_BLOCK*2 + NUMBER_OF_STOP_CHAR]; }sendbuffer; struct{ unsigned char telegram_header[FOLLOW_ON_HEADER_SIZE]; unsigned char telegram[MAX_SIZE_DATA_BLOCK*2 + NUMBER_OF_STOP_CHAR]; }follow_on_sendbuffer; do /* Send 128 byte telegrams until message is finished */ { if ( !follow_on ) { A_telegram = TRUE; /*************************************************************************/ /** Send A-telegram. **/ /*************************************************************************/ /** Check if follow on telegrams are needed. **/ /*************************************************************************/ if ( buf_size - pos_counter > MAX_SIZE_DATA_BLOCK ) { delta_pos = MAX_SIZE_DATA_BLOCK; follow_on = TRUE; } else { delta_pos = buf_size - pos_counter; } /*************************************************************************/ /** Calculate the size of the A-telegram. **/ /*************************************************************************/ /* Count DLE characters */ for ( i=0 ; i<delta_pos ; i++ ) { if ( *buf++ == DLE ) number_of_DLE += 1; } size_of_telegram = HEADER_SIZE+ delta_pos+number_of_DLE+NUMBER_OF_STOP_CHAR; /*************************************************************************/ /** Fill in the telegram header and calculate BCC. **/ /*************************************************************************/ /* Size have to be expressed in number of 16 bits words. */ /* If odd number of bytes add one. */ datasize = buf_size/2 + buf_size%2; datasize_low_byte = (unsigned char)(BYTE_MASK & datasize); datasize = datasize >> 8; datasize_high_byte = (unsigned char)(BYTE_MASK & datasize); cpu_number = (unsigned char)remtrans->Address[2]; CPU = '\xFF'; CPU = CPU_NR_MASK & cpu_number; sendbuffer.telegram_header[0] = '\0'; sendbuffer.telegram_header[1] = '\0'; sendbuffer.telegram_header[2] = 'A'; sendbuffer.telegram_header[3] = 'D'; sendbuffer.telegram_header[4] = (unsigned char)remtrans->Address[0]; sendbuffer.telegram_header[5] = (unsigned char)remtrans->Address[1]; sendbuffer.telegram_header[6] = datasize_high_byte; sendbuffer.telegram_header[7] = datasize_low_byte; sendbuffer.telegram_header[8] = '\xFF'; sendbuffer.telegram_header[9] = CPU; /* Calculate checksum for the header */ for ( i=0 ; i<HEADER_SIZE ; i++ ) { BCC ^= sendbuffer.telegram_header[i]; } /*************************************************************************/ /** Fill up A-telegram with contents of message and calculate BCC **/ /*************************************************************************/ buf = (char *)restore_buf_ptr; for ( i=0 ; i<(delta_pos+number_of_DLE) ; i++ ) { ch = sendbuffer.telegram[i] = *buf++; BCC ^= ch; if ( ch == DLE ) { sendbuffer.telegram[++i] = DLE; BCC ^= ch; } } if ( delta_pos%2 ) { /* Ensure that a even number of bytes is treated */ sendbuffer.telegram[i++] = '\0'; size_of_telegram += 1; } sendbuffer.telegram[i++] = DLE; sendbuffer.telegram[i++] = ETX; sendbuffer.telegram[i] = BCC; pos_counter = delta_pos; } else /* follow on telegram */ { /*************************************************************************/ /** Send follow on telegram. **/ /*************************************************************************/ /** Check if more follow on telegrams are needed. **/ /*************************************************************************/ if ( buf_size - pos_counter > MAX_SIZE_DATA_BLOCK ) { delta_pos = MAX_SIZE_DATA_BLOCK; follow_on = TRUE; } else { delta_pos = buf_size - pos_counter; follow_on = FALSE; } /*************************************************************************/ /** Calculate the size of the follow on telegram. **/ /*************************************************************************/ /* Count DLE characters */ restore_buf_ptr = (unsigned char *)buf; number_of_DLE = 0; for ( i=0 ; i<delta_pos ; i++ ) { if ( *buf++ == DLE ) number_of_DLE += 1; } size_of_telegram = FOLLOW_ON_HEADER_SIZE+ delta_pos+number_of_DLE+NUMBER_OF_STOP_CHAR; /*************************************************************************/ /** Fill in the follow on telegram header and calculate BCC. **/ /*************************************************************************/ follow_on_sendbuffer.telegram_header[0] = '\xFF'; follow_on_sendbuffer.telegram_header[1] = '\0'; follow_on_sendbuffer.telegram_header[2] = 'A'; follow_on_sendbuffer.telegram_header[3] = 'D'; /* Calculate checksum for the header */ BCC = DLE ^ ETX; for ( i=0 ; i<FOLLOW_ON_HEADER_SIZE ; i++ ) { BCC ^= follow_on_sendbuffer.telegram_header[i]; } /*************************************************************************/ /* Fill up follow on telegram with contents of message and calculate BCC */ /*************************************************************************/ buf = (char *)restore_buf_ptr; for ( i = 0 ; i < (delta_pos+number_of_DLE) ; i++ ) { ch = follow_on_sendbuffer.telegram[i] = *buf++; BCC ^= ch; if ( ch == DLE ) { follow_on_sendbuffer.telegram[++i] = DLE; BCC ^= ch; } } if ( delta_pos%2 ) { /* Ensure that a even number of bytes is treated */ follow_on_sendbuffer.telegram[i++] = '\0'; size_of_telegram += 1; } follow_on_sendbuffer.telegram[i++] = DLE; follow_on_sendbuffer.telegram[i++] = ETX; follow_on_sendbuffer.telegram[i] = BCC; pos_counter += delta_pos; } /*************************************************************************/ /** Execute the send procedure **/ /*************************************************************************/ /* Send STX and wait for answer */ sts = write(ser_fd, sstx, 1); rlog("snd STX", 0); if ( sts > 0) { load_timeval(&tv, rn_RK512->CharTimeout); FD_ZERO(&read_fd); FD_SET(ser_fd, &read_fd); select(ser_fd+1, &read_fd, NULL, NULL, &tv); sts = read(ser_fd, &received_char, 1); } if ( sts > 0) { //om det inte var timeout if ( received_char == STX ) { rlog("snd STX received", 0); /* Both nodes is in sending mode. */ /* Cancel this send operation and wait for next timeout or receive */ write(ser_fd, snak, 1); return(FALSE); } if ( received_char == DLE ) { /* Contact is established. Send telegram */ rlog("snd DLE received, contact", 0); if ( A_telegram ) { sts = write(ser_fd, &sendbuffer, size_of_telegram); rlog("snd message sent", size_of_telegram); A_telegram = FALSE; } else { sts = write(ser_fd, &follow_on_sendbuffer, size_of_telegram); rlog("snd message sent", size_of_telegram); } if ( sts > 0 ) { /* wait for break character or timeout */ load_timeval(&tv, rn_RK512->CharTimeout); FD_ZERO(&read_fd); FD_SET(ser_fd, &read_fd); select(ser_fd+1, &read_fd, NULL, NULL, &tv); sts = read(ser_fd, &received_char, 1); if ( sts > 0 &&(received_char == DLE) ) { /*************************************************************************/ /** The sending was a SUCCESS. Take care of the response message **/ /*************************************************************************/ rlog("snd DLE received, success", 0); load_timeval(&tv, rn_RK512->CharTimeout); FD_ZERO(&read_fd); FD_SET(ser_fd, &read_fd); select(ser_fd+1, &read_fd, NULL, NULL, &tv); sts = read(ser_fd, &received_char, 1); if ( sts > 0 &&(received_char == STX) ) { /* Send DLE acknowledge and wait for response data */ sts = write(ser_fd, sdle, 1); if ( sts > 0 ) { BCC = '\0'; for (i=0 ; i < RESP_MESSAGE_SIZE && sts > 0; i++ ) { load_timeval(&tv, rn_RK512->CharTimeout); FD_ZERO(&read_fd); FD_SET(ser_fd, &read_fd); select(ser_fd+1, &read_fd, NULL, NULL, &tv); sts = read(ser_fd, &received_char, 1); response_buffer[i] = received_char; BCC ^= received_char; } /* endfor */ if ( sts > 0 && (response_buffer[2] == '\0') ) { /* Compare received BCC with calculated */ load_timeval(&tv, rn_RK512->CharTimeout); FD_ZERO(&read_fd); FD_SET(ser_fd, &read_fd); select(ser_fd+1, &read_fd, NULL, NULL, &tv); sts = read(ser_fd, &received_char, 1); if ( sts > 0 && ( BCC == received_char ) ) { /* Response telegram received OK */ sts = write(ser_fd, sdle, 1); if ( response_buffer[3] != 0 ) { /* This response contains a error code */ errh_CErrLog(REM__SIEMENSERROR, errh_ErrArgL(response_buffer[3]) ); } } else { /* Wrong checksum. */ sts = FALSE; } } else { /* This is not a response message as expected */ sts = write(ser_fd, snak, 1); sts = FALSE; } } /* ENDIF. DLE acknowledge failed */ } else { /* STX character in response message was expected. */ /* Ensure that error status is returned */ sts = FALSE; } } else { /* DLE ack. after sending telegram was expected. */ /* Ensure that error status is returned */ rlog("snd DLE missing", 0); sts = FALSE; } } /* ENDIF. Contact established but tty_write failed */ } else { /* Failed in making contact. Wrong response character. */ /* Ensure that error status is returned */ sts = FALSE; } } /* ENDIF. tty_write or tty_read failed */ /*************************************************************************/ /** Check final status. **/ /*************************************************************************/ if ( EVEN(sts)) { /* The send procedure has failed */ sts = write(ser_fd, snak, 1); rlog("snd failed, NAK sent", 0); follow_on = FALSE; /* Ensure that error status is returned */ sts = FALSE; } }while( follow_on );
int main() { int fd=open("samplefifo",O_WRONLY); write(fd,"hello",15); }
int main (int argc, char const * argv []) { static char const * optv [] = { "qv", "file [file] [...]", "convert ASCII SDRAM configuration files (DM) to binary (toolkit) format", "q\tquiet mode", "v\tverbose mode", (char const *) (0) }; struct config_ram config_ram; char string [(sizeof (config_ram) << 1) + 1]; uint32_t checksum; flag_t flags = (flag_t)(0); signed state = 1; signed fd; signed c; optind = 1; while ((c = getoptv (argc, argv, optv)) != -1) { switch ((char) (c)) { case 'q': _setbits (flags, SDRAM_SILENCE); break; case 'v': _setbits (flags, SDRAM_VERBOSE); break; default: break; } } argc -= optind; argv += optind; while ((argc-- > 0) && (* argv != (char const *)(0))) { #if 0 char const * pathname; char const * filename; char const * extender; for (pathname = filename = * argv; *pathname; pathname++) { if ((*pathname == '/') || (*pathname == '\\')) { filename = pathname + 1; } } for (pathname = extender = filename; *pathname; pathname++) { if (*pathname == '.') { extender = pathname; } } if (extender == filename) { extender = pathname; } #endif if ((fd = open (* argv, O_BINARY|O_RDONLY)) == -1) { error (0, errno, "can't open %s for input", * argv); state = 1; } else if (read (fd, &string, sizeof (string)) < (ssize_t) (sizeof (string) - 1)) { error (0, errno, "can't read %s", * argv); state = 1; } else { close (fd); if (hexencode ((uint8_t *) (&config_ram), sizeof (config_ram), string) == sizeof (config_ram)) { error (1, errno, "%s is suspect", * argv); } checksum = checksum32 (&config_ram, sizeof (config_ram), 0); if ((fd = open (* argv, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1) { error (1, errno, "can't open %s for output", * argv); } write (fd, &config_ram, sizeof (config_ram)); write (fd, &checksum, sizeof (checksum)); } close (fd); argv++; } return (state); }