static void progress_meter(int flag) { /* We can be called from signal handler */ int save_errno = errno; if (flag == -1) { /* first call to progress_meter */ bb_progress_init(&G.pmt); } #ifdef MY_ABC_HERE bb_progress_update(&G.pmt, G.curfile, G.beg_range, G.transferred, G.chunked ? 0 : G.content_len + G.beg_range + G.transferred); #else bb_progress_update(&G.pmt, G.curfile, G.beg_range, G.transferred, G.chunked ? 0 : G.content_len + G.beg_range); #endif if (flag == 0) { /* last call to progress_meter */ alarm(0); fputc('\n', stderr); G.transferred = 0; } else { if (flag == -1) { /* first call to progress_meter */ signal_SA_RESTART_empty_mask(SIGALRM, progress_meter); } alarm(1); } errno = save_errno; }
/* SIGALRM logic nicked from the wget applet */ static void progress_meter(int flag) { /* We can be called from signal handler */ int save_errno = errno; if (flag == -1) { /* first call to progress_meter */ bb_progress_init(&G.pmt); } bb_progress_update(&G.pmt, G.file, 0, G.pos, G.size); if (flag == 0) { /* last call to progress_meter */ alarm(0); bb_putchar_stderr('\n'); } else { if (flag == -1) { /* first call to progress_meter */ signal_SA_RESTART_empty_mask(SIGALRM, progress_meter); } alarm(1); } errno = save_errno; }
int arping_main(int argc UNUSED_PARAM, char **argv) { const char *device = "eth0"; char *source = NULL; char *target; unsigned char *packet; char *err_str; INIT_G(); sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0); // Drop suid root privileges // Need to remove SUID_NEVER from applets.h for this to work //xsetuid(getuid()); err_str = xasprintf("interface %s %%s", device); { unsigned opt; char *str_timeout; /* Dad also sets quit_on_reply. * Advert also sets unsolicited. */ opt_complementary = "=1:Df:AU:c+"; opt = getopt32(argv, "DUAqfbc:w:I:s:", &count, &str_timeout, &device, &source); if (opt & 0x80) /* -w: timeout */ timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000; //if (opt & 0x200) /* -s: source */ option_mask32 &= 0x3f; /* set respective flags */ } target = argv[optind]; xfunc_error_retval = 2; { struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy_IFNAMSIZ(ifr.ifr_name, device); /* We use ifr.ifr_name in error msg so that problem * with truncated name will be visible */ ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found"); me.sll_ifindex = ifr.ifr_ifindex; xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr); if (!(ifr.ifr_flags & IFF_UP)) { bb_error_msg_and_die(err_str, "is down"); } if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { bb_error_msg(err_str, "is not ARPable"); return (option_mask32 & DAD ? 0 : 2); } } /* if (!inet_aton(target, &dst)) - not needed */ { len_and_sockaddr *lsa; lsa = xhost_and_af2sockaddr(target, 0, AF_INET); dst = lsa->u.sin.sin_addr; if (ENABLE_FEATURE_CLEAN_UP) free(lsa); } if (source && !inet_aton(source, &src)) { bb_error_msg_and_die("invalid source address %s", source); } if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0) src = dst; if (!(option_mask32 & DAD) || src.s_addr) { struct sockaddr_in saddr; int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); setsockopt_bindtodevice(probe_fd, device); memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; if (src.s_addr) { /* Check that this is indeed our IP */ saddr.sin_addr = src; xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); } else { /* !(option_mask32 & DAD) case */ /* Find IP address on this iface */ socklen_t alen = sizeof(saddr); saddr.sin_port = htons(1025); saddr.sin_addr = dst; if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1) bb_perror_msg("setsockopt(SO_DONTROUTE)"); xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) { bb_perror_msg_and_die("getsockname"); } if (saddr.sin_family != AF_INET) bb_error_msg_and_die("no IP address configured"); src = saddr.sin_addr; } close(probe_fd); } me.sll_family = AF_PACKET; //me.sll_ifindex = ifindex; - done before me.sll_protocol = htons(ETH_P_ARP); xbind(sock_fd, (struct sockaddr *) &me, sizeof(me)); { socklen_t alen = sizeof(me); if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) { bb_perror_msg_and_die("getsockname"); } } if (me.sll_halen == 0) { bb_error_msg(err_str, "is not ARPable (no ll address)"); return (option_mask32 & DAD ? 0 : 2); } he = me; memset(he.sll_addr, -1, he.sll_halen); if (!(option_mask32 & QUIET)) { /* inet_ntoa uses static storage, can't use in same printf */ printf("ARPING to %s", inet_ntoa(dst)); printf(" from %s via %s\n", inet_ntoa(src), device); } signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish); signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher); catcher(); packet = xmalloc(4096); while (1) { sigset_t sset, osset; struct sockaddr_ll from; socklen_t alen = sizeof(from); int cc; cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen); if (cc < 0) { bb_perror_msg("recvfrom"); continue; } sigemptyset(&sset); sigaddset(&sset, SIGALRM); sigaddset(&sset, SIGINT); sigprocmask(SIG_BLOCK, &sset, &osset); recv_pack(packet, cc, &from); sigprocmask(SIG_SETMASK, &osset, NULL); } }
int vlock_main(int argc UNUSED_PARAM, char **argv) { #ifdef __linux__ struct vt_mode vtm; struct vt_mode ovtm; #endif struct termios term; struct termios oterm; struct passwd *pw; pw = xgetpwuid(getuid()); opt_complementary = "=0"; /* no params! */ getopt32(argv, "a"); /* Ignore some signals so that we don't get killed by them */ bb_signals(0 + (1 << SIGTSTP) + (1 << SIGTTIN) + (1 << SIGTTOU) + (1 << SIGHUP ) + (1 << SIGCHLD) /* paranoia :) */ + (1 << SIGQUIT) + (1 << SIGINT ) , SIG_IGN); #ifdef __linux__ /* We will use SIGUSRx for console switch control: */ /* 1: set handlers */ signal_SA_RESTART_empty_mask(SIGUSR1, release_vt); signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt); /* 2: unmask them */ sig_unblock(SIGUSR1); sig_unblock(SIGUSR2); #endif /* Revert stdin/out to our controlling tty * (or die if we have none) */ xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO); xdup2(STDIN_FILENO, STDOUT_FILENO); #ifdef __linux__ xioctl(STDIN_FILENO, VT_GETMODE, &vtm); ovtm = vtm; /* "console switches are controlled by us, not kernel!" */ vtm.mode = VT_PROCESS; vtm.relsig = SIGUSR1; vtm.acqsig = SIGUSR2; ioctl(STDIN_FILENO, VT_SETMODE, &vtm); #endif //TODO: use set_termios_to_raw() tcgetattr(STDIN_FILENO, &oterm); term = oterm; term.c_iflag |= IGNBRK; /* ignore serial break (why? VTs don't have breaks, right?) */ term.c_iflag &= ~BRKINT; /* redundant? "dont translate break to SIGINT" */ term.c_lflag &= ~(ISIG | ECHO | ECHOCTL); /* ignore ^C ^Z, echo off */ tcsetattr_stdin_TCSANOW(&term); while (1) { printf("Virtual console%s locked by %s.\n", /* "s" if -a, else "": */ "s" + !option_mask32, pw->pw_name ); if (ask_and_check_password(pw) > 0) { break; } bb_do_delay(LOGIN_FAIL_DELAY); puts("Incorrect password"); } #ifdef __linux__ ioctl(STDIN_FILENO, VT_SETMODE, &ovtm); #endif tcsetattr_stdin_TCSANOW(&oterm); fflush_stdout_and_exit(EXIT_SUCCESS); }
int dd_main(int argc UNUSED_PARAM, char **argv) { enum { /* Must be in the same order as OP_conv_XXX! */ /* (see "flags |= (1 << what)" below) */ FLAG_NOTRUNC = 1 << 0, FLAG_SYNC = 1 << 1, FLAG_NOERROR = 1 << 2, FLAG_FSYNC = 1 << 3, /* end of conv flags */ FLAG_TWOBUFS = 1 << 4, FLAG_COUNT = 1 << 5, }; static const char keywords[] ALIGN1 = "bs\0""count\0""seek\0""skip\0""if\0""of\0" #if ENABLE_FEATURE_DD_IBS_OBS "ibs\0""obs\0""conv\0" #endif ; #if ENABLE_FEATURE_DD_IBS_OBS static const char conv_words[] ALIGN1 = "notrunc\0""sync\0""noerror\0""fsync\0"; #endif enum { OP_bs = 0, OP_count, OP_seek, OP_skip, OP_if, OP_of, #if ENABLE_FEATURE_DD_IBS_OBS OP_ibs, OP_obs, OP_conv, /* Must be in the same order as FLAG_XXX! */ OP_conv_notrunc = 0, OP_conv_sync, OP_conv_noerror, OP_conv_fsync, /* Unimplemented conv=XXX: */ //nocreat do not create the output file //excl fail if the output file already exists //fdatasync physically write output file data before finishing //swab swap every pair of input bytes //lcase change upper case to lower case //ucase change lower case to upper case //block pad newline-terminated records with spaces to cbs-size //unblock replace trailing spaces in cbs-size records with newline //ascii from EBCDIC to ASCII //ebcdic from ASCII to EBCDIC //ibm from ASCII to alternate EBCDIC #endif }; int exitcode = EXIT_FAILURE; size_t ibs = 512, obs = 512; ssize_t n, w; char *ibuf, *obuf; /* And these are all zeroed at once! */ struct { int flags; size_t oc; off_t count; off_t seek, skip; const char *infile, *outfile; } Z; #define flags (Z.flags ) #define oc (Z.oc ) #define count (Z.count ) #define seek (Z.seek ) #define skip (Z.skip ) #define infile (Z.infile ) #define outfile (Z.outfile) memset(&Z, 0, sizeof(Z)); INIT_G(); //fflush(NULL); - is this needed because of NOEXEC? #if ENABLE_FEATURE_DD_SIGNAL_HANDLING signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); #endif for (n = 1; argv[n]; n++) { int what; char *val; char *arg = argv[n]; #if ENABLE_DESKTOP /* "dd --". NB: coreutils 6.9 will complain if they see * more than one of them. We wouldn't. */ if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') continue; #endif val = strchr(arg, '='); if (val == NULL) bb_show_usage(); *val = '\0'; what = index_in_strings(keywords, arg); if (what < 0) bb_show_usage(); /* *val = '='; - to preserve ps listing? */ val++; #if ENABLE_FEATURE_DD_IBS_OBS if (what == OP_ibs) { /* Must fit into positive ssize_t */ ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); /*continue;*/ } if (what == OP_obs) { obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); /*continue;*/ } if (what == OP_conv) { while (1) { /* find ',', replace them with NUL so we can use val for * index_in_strings() without copying. * We rely on val being non-null, else strchr would fault. */ arg = strchr(val, ','); if (arg) *arg = '\0'; what = index_in_strings(conv_words, val); if (what < 0) bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv"); flags |= (1 << what); if (!arg) /* no ',' left, so this was the last specifier */ break; /* *arg = ','; - to preserve ps listing? */ val = arg + 1; /* skip this keyword and ',' */ } continue; /* we trashed 'what', can't fall through */ } #endif if (what == OP_bs) { ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); /*continue;*/ } /* These can be large: */ if (what == OP_count) { flags |= FLAG_COUNT; count = XATOU_SFX(val, dd_suffixes); /*continue;*/ } if (what == OP_seek) { seek = XATOU_SFX(val, dd_suffixes); /*continue;*/ } if (what == OP_skip) { skip = XATOU_SFX(val, dd_suffixes); /*continue;*/ } if (what == OP_if) { infile = val; /*continue;*/ } if (what == OP_of) { outfile = val; /*continue;*/ } } /* end of "for (argv[n])" */ //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever ibuf = obuf = xmalloc(ibs); if (ibs != obs) { flags |= FLAG_TWOBUFS; obuf = xmalloc(obs); } if (infile != NULL) xmove_fd(xopen(infile, O_RDONLY), ifd); else { infile = bb_msg_standard_input; } if (outfile != NULL) { int oflag = O_WRONLY | O_CREAT; if (!seek && !(flags & FLAG_NOTRUNC)) oflag |= O_TRUNC; xmove_fd(xopen(outfile, oflag), ofd); if (seek && !(flags & FLAG_NOTRUNC)) { if (ftruncate(ofd, seek * obs) < 0) { struct stat st; if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) goto die_outfile; } } } else { outfile = bb_msg_standard_output; } if (skip) { if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { while (skip-- > 0) { n = safe_read(ifd, ibuf, ibs); if (n < 0) goto die_infile; if (n == 0) break; } } } if (seek) { if (lseek(ofd, seek * obs, SEEK_CUR) < 0) goto die_outfile; } while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */ memset(ibuf, 0, ibs); n = safe_read(ifd, ibuf, ibs); if (n == 0) break; if (n < 0) { if (!(flags & FLAG_NOERROR)) goto die_infile; n = ibs; bb_simple_perror_msg(infile); } if ((size_t)n == ibs) G.in_full++; else { G.in_part++; if (flags & FLAG_SYNC) { memset(ibuf + n, '\0', ibs - n); n = ibs; } } if (flags & FLAG_TWOBUFS) { char *tmp = ibuf; while (n) { size_t d = obs - oc; if (d > (size_t)n) d = n; memcpy(obuf + oc, tmp, d); n -= d; tmp += d; oc += d; if (oc == obs) { if (write_and_stats(obuf, obs, obs, outfile)) goto out_status; oc = 0; } } } else if (write_and_stats(ibuf, n, obs, outfile)) goto out_status; if (flags & FLAG_FSYNC) { if (fsync(ofd) < 0) goto die_outfile; } } if (ENABLE_FEATURE_DD_IBS_OBS && oc) { w = full_write_or_warn(obuf, oc, outfile); if (w < 0) goto out_status; if (w > 0) G.out_part++; } if (close(ifd) < 0) { die_infile: bb_simple_perror_msg_and_die(infile); } if (close(ofd) < 0) { die_outfile: bb_simple_perror_msg_and_die(outfile); } exitcode = EXIT_SUCCESS; out_status: dd_output_status(0); return exitcode; }
int vlock_main(int argc ATTRIBUTE_UNUSED, char **argv) { struct vt_mode vtm; struct termios term; struct termios oterm; struct vt_mode ovtm; uid_t uid; struct passwd *pw; uid = getuid(); pw = getpwuid(uid); if (pw == NULL) bb_error_msg_and_die("unknown uid %d", (int)uid); opt_complementary = "=0"; /* no params! */ getopt32(argv, "a"); /* Ignore some signals so that we don't get killed by them */ bb_signals(0 + (1 << SIGTSTP) + (1 << SIGTTIN) + (1 << SIGTTOU) + (1 << SIGHUP ) + (1 << SIGCHLD) /* paranoia :) */ + (1 << SIGQUIT) + (1 << SIGINT ) , SIG_IGN); /* We will use SIGUSRx for console switch control: */ /* 1: set handlers */ signal_SA_RESTART_empty_mask(SIGUSR1, release_vt); signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt); /* 2: unmask them */ sig_unblock(SIGUSR1); sig_unblock(SIGUSR2); /* Revert stdin/out to our controlling tty * (or die if we have none) */ xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO); xdup2(STDIN_FILENO, STDOUT_FILENO); xioctl(STDIN_FILENO, VT_GETMODE, &vtm); ovtm = vtm; /* "console switches are controlled by us, not kernel!" */ vtm.mode = VT_PROCESS; vtm.relsig = SIGUSR1; vtm.acqsig = SIGUSR2; ioctl(STDIN_FILENO, VT_SETMODE, &vtm); tcgetattr(STDIN_FILENO, &oterm); term = oterm; term.c_iflag &= ~BRKINT; term.c_iflag |= IGNBRK; term.c_lflag &= ~ISIG; term.c_lflag &= ~(ECHO | ECHOCTL); tcsetattr(STDIN_FILENO, TCSANOW, &term); do { printf("Virtual console%s locked by %s.\n", option_mask32 /*o_lock_all*/ ? "s" : "", pw->pw_name); if (correct_password(pw)) { break; } bb_do_delay(FAIL_DELAY); puts("Password incorrect"); } while (1); ioctl(STDIN_FILENO, VT_SETMODE, &ovtm); tcsetattr(STDIN_FILENO, TCSANOW, &oterm); fflush_stdout_and_exit(EXIT_SUCCESS); }