aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-06-24 00:47:58 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-06-24 00:47:58 +0000
commit003f9fb213e034dcfd3bdbd93e4d60315002d473 (patch)
tree678580fa963f3ea25f51c63371d56174de3edd87
parentcc3f20b9bdf9a46c41877dab4900eb2997b72019 (diff)
downloadbusybox-w32-003f9fb213e034dcfd3bdbd93e4d60315002d473.tar.gz
busybox-w32-003f9fb213e034dcfd3bdbd93e4d60315002d473.tar.bz2
busybox-w32-003f9fb213e034dcfd3bdbd93e4d60315002d473.zip
hush: kill some old unused fields; small code shrink
function old new delta builtin_exit 48 47 -1 checkjobs 351 335 -16 checkjobs_and_fg_shell 60 35 -25 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-42) Total: -42 bytes
-rw-r--r--shell/hush.c107
1 files changed, 49 insertions, 58 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 01c9dc8ef..b59f67942 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -309,16 +309,14 @@ struct child_prog {
309struct pipe { 309struct pipe {
310 struct pipe *next; 310 struct pipe *next;
311 int num_progs; /* total number of programs in job */ 311 int num_progs; /* total number of programs in job */
312 int running_progs; /* number of programs running (not exited) */ 312 int alive_progs; /* number of programs running (not exited) */
313 int stopped_progs; /* number of programs alive, but stopped */ 313 int stopped_progs; /* number of programs alive, but stopped */
314#if ENABLE_HUSH_JOB 314#if ENABLE_HUSH_JOB
315 int jobid; /* job number */ 315 int jobid; /* job number */
316 pid_t pgrp; /* process group ID for the job */ 316 pid_t pgrp; /* process group ID for the job */
317 char *cmdtext; /* name of job */ 317 char *cmdtext; /* name of job */
318#endif 318#endif
319 char *cmdbuf; /* buffer various argv's point into */
320 struct child_prog *progs; /* array of commands in pipe */ 319 struct child_prog *progs; /* array of commands in pipe */
321 int job_context; /* bitmask defining current context */
322 smallint pi_inverted; /* "! cmd | cmd" */ 320 smallint pi_inverted; /* "! cmd | cmd" */
323 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ 321 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
324 smallint res_word; /* needed for if, for, while, until... */ 322 smallint res_word; /* needed for if, for, while, until... */
@@ -1564,7 +1562,7 @@ static void remove_bg_job(struct pipe *pi)
1564 last_jobid = 0; 1562 last_jobid = 0;
1565} 1563}
1566 1564
1567/* remove a backgrounded job */ 1565/* Remove a backgrounded job */
1568static void delete_finished_bg_job(struct pipe *pi) 1566static void delete_finished_bg_job(struct pipe *pi)
1569{ 1567{
1570 remove_bg_job(pi); 1568 remove_bg_job(pi);
@@ -1574,23 +1572,21 @@ static void delete_finished_bg_job(struct pipe *pi)
1574} 1572}
1575#endif /* JOB */ 1573#endif /* JOB */
1576 1574
1577/* Checks to see if any processes have exited -- if they 1575/* Check to see if any processes have exited -- if they
1578 have, figure out why and see if a job has completed */ 1576 * have, figure out why and see if a job has completed */
1579static int checkjobs(struct pipe* fg_pipe) 1577static int checkjobs(struct pipe* fg_pipe)
1580{ 1578{
1581 int attributes; 1579 int attributes;
1582 int status; 1580 int status;
1583#if ENABLE_HUSH_JOB 1581#if ENABLE_HUSH_JOB
1584 int prognum = 0;
1585 struct pipe *pi; 1582 struct pipe *pi;
1586#endif 1583#endif
1587 pid_t childpid; 1584 pid_t childpid;
1588 int rcode = 0; 1585 int rcode = 0;
1589 1586
1590 attributes = WUNTRACED; 1587 attributes = WUNTRACED;
1591 if (fg_pipe == NULL) { 1588 if (fg_pipe == NULL)
1592 attributes |= WNOHANG; 1589 attributes |= WNOHANG;
1593 }
1594 1590
1595/* Do we do this right? 1591/* Do we do this right?
1596 * bash-3.00# sleep 20 | false 1592 * bash-3.00# sleep 20 | false
@@ -1607,8 +1603,8 @@ static int checkjobs(struct pipe* fg_pipe)
1607 wait_more: 1603 wait_more:
1608// TODO: safe_waitpid? 1604// TODO: safe_waitpid?
1609 while ((childpid = waitpid(-1, &status, attributes)) > 0) { 1605 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
1606 int i;
1610 const int dead = WIFEXITED(status) || WIFSIGNALED(status); 1607 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1611
1612#if DEBUG_JOBS 1608#if DEBUG_JOBS
1613 if (WIFSTOPPED(status)) 1609 if (WIFSTOPPED(status))
1614 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", 1610 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
@@ -1622,76 +1618,70 @@ static int checkjobs(struct pipe* fg_pipe)
1622#endif 1618#endif
1623 /* Were we asked to wait for fg pipe? */ 1619 /* Were we asked to wait for fg pipe? */
1624 if (fg_pipe) { 1620 if (fg_pipe) {
1625 int i;
1626 for (i = 0; i < fg_pipe->num_progs; i++) { 1621 for (i = 0; i < fg_pipe->num_progs; i++) {
1627 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid); 1622 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
1628 if (fg_pipe->progs[i].pid == childpid) { 1623 if (fg_pipe->progs[i].pid != childpid)
1629 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ 1624 continue;
1630 if (dead) { 1625 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1631 fg_pipe->progs[i].pid = 0; 1626 if (dead) {
1632 fg_pipe->running_progs--; 1627 fg_pipe->progs[i].pid = 0;
1633 if (i == fg_pipe->num_progs - 1) { 1628 fg_pipe->alive_progs--;
1634 /* last process gives overall exitstatus */ 1629 if (i == fg_pipe->num_progs - 1) {
1635 rcode = WEXITSTATUS(status); 1630 /* last process gives overall exitstatus */
1636 if (fg_pipe->pi_inverted) 1631 rcode = WEXITSTATUS(status);
1637 rcode = !rcode; 1632 if (fg_pipe->pi_inverted)
1638 } 1633 rcode = !rcode;
1639 } else {
1640 fg_pipe->progs[i].is_stopped = 1;
1641 fg_pipe->stopped_progs++;
1642 } 1634 }
1643 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n", 1635 } else {
1644 fg_pipe->running_progs, fg_pipe->stopped_progs); 1636 fg_pipe->progs[i].is_stopped = 1;
1645 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) { 1637 fg_pipe->stopped_progs++;
1646 /* All processes in fg pipe have exited/stopped */ 1638 }
1639 debug_printf_jobs("fg_pipe: alive_progs %d stopped_progs %d\n",
1640 fg_pipe->alive_progs, fg_pipe->stopped_progs);
1641 if (fg_pipe->alive_progs - fg_pipe->stopped_progs <= 0) {
1642 /* All processes in fg pipe have exited/stopped */
1647#if ENABLE_HUSH_JOB 1643#if ENABLE_HUSH_JOB
1648 if (fg_pipe->running_progs) 1644 if (fg_pipe->alive_progs)
1649 insert_bg_job(fg_pipe); 1645 insert_bg_job(fg_pipe);
1650#endif 1646#endif
1651 return rcode; 1647 return rcode;
1652 }
1653 /* There are still running processes in the fg pipe */
1654 goto wait_more;
1655 } 1648 }
1649 /* There are still running processes in the fg pipe */
1650 goto wait_more; /* do waitpid again */
1656 } 1651 }
1657 /* fall through to searching process in bg pipes */ 1652 /* it wasnt fg_pipe, look for process in bg pipes */
1658 } 1653 }
1659 1654
1660#if ENABLE_HUSH_JOB 1655#if ENABLE_HUSH_JOB
1661 /* We asked to wait for bg or orphaned children */ 1656 /* We asked to wait for bg or orphaned children */
1662 /* No need to remember exitcode in this case */ 1657 /* No need to remember exitcode in this case */
1663 for (pi = job_list; pi; pi = pi->next) { 1658 for (pi = job_list; pi; pi = pi->next) {
1664 prognum = 0; 1659 for (i = 0; i < pi->num_progs; i++) {
1665 while (prognum < pi->num_progs) { 1660 if (pi->progs[i].pid == childpid)
1666 if (pi->progs[prognum].pid == childpid)
1667 goto found_pi_and_prognum; 1661 goto found_pi_and_prognum;
1668 prognum++;
1669 } 1662 }
1670 } 1663 }
1671#endif
1672
1673 /* Happens when shell is used as init process (init=/bin/sh) */ 1664 /* Happens when shell is used as init process (init=/bin/sh) */
1674 debug_printf("checkjobs: pid %d was not in our list!\n", childpid); 1665 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
1675 goto wait_more; 1666 continue; /* do waitpid again */
1676 1667
1677#if ENABLE_HUSH_JOB
1678 found_pi_and_prognum: 1668 found_pi_and_prognum:
1679 if (dead) { 1669 if (dead) {
1680 /* child exited */ 1670 /* child exited */
1681 pi->progs[prognum].pid = 0; 1671 pi->progs[i].pid = 0;
1682 pi->running_progs--; 1672 pi->alive_progs--;
1683 if (!pi->running_progs) { 1673 if (!pi->alive_progs) {
1684 printf(JOB_STATUS_FORMAT, pi->jobid, 1674 printf(JOB_STATUS_FORMAT, pi->jobid,
1685 "Done", pi->cmdtext); 1675 "Done", pi->cmdtext);
1686 delete_finished_bg_job(pi); 1676 delete_finished_bg_job(pi);
1687 } 1677 }
1688 } else { 1678 } else {
1689 /* child stopped */ 1679 /* child stopped */
1680 pi->progs[i].is_stopped = 1;
1690 pi->stopped_progs++; 1681 pi->stopped_progs++;
1691 pi->progs[prognum].is_stopped = 1;
1692 } 1682 }
1693#endif 1683#endif
1694 } 1684 } /* while (waitpid succeeds)... */
1695 1685
1696 /* wait found no children or failed */ 1686 /* wait found no children or failed */
1697 1687
@@ -1708,8 +1698,9 @@ static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1708 /* Job finished, move the shell to the foreground */ 1698 /* Job finished, move the shell to the foreground */
1709 p = getpgid(0); /* pgid of our process */ 1699 p = getpgid(0); /* pgid of our process */
1710 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); 1700 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
1711 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY) 1701 tcsetpgrp(interactive_fd, p);
1712 bb_perror_msg("tcsetpgrp-4a"); 1702// if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1703// bb_perror_msg("tcsetpgrp-4a");
1713 return rcode; 1704 return rcode;
1714} 1705}
1715#endif 1706#endif
@@ -1751,7 +1742,7 @@ static int run_pipe(struct pipe *pi)
1751#if ENABLE_HUSH_JOB 1742#if ENABLE_HUSH_JOB
1752 pi->pgrp = -1; 1743 pi->pgrp = -1;
1753#endif 1744#endif
1754 pi->running_progs = 1; 1745 pi->alive_progs = 1;
1755 pi->stopped_progs = 0; 1746 pi->stopped_progs = 0;
1756 1747
1757 /* Check if this is a simple builtin (not part of a pipe). 1748 /* Check if this is a simple builtin (not part of a pipe).
@@ -1841,7 +1832,7 @@ static int run_pipe(struct pipe *pi)
1841 set_jobctrl_sighandler(SIG_IGN); 1832 set_jobctrl_sighandler(SIG_IGN);
1842 1833
1843 /* Going to fork a child per each pipe member */ 1834 /* Going to fork a child per each pipe member */
1844 pi->running_progs = 0; 1835 pi->alive_progs = 0;
1845 nextin = 0; 1836 nextin = 0;
1846 1837
1847 for (i = 0; i < pi->num_progs; i++) { 1838 for (i = 0; i < pi->num_progs; i++) {
@@ -1905,7 +1896,7 @@ static int run_pipe(struct pipe *pi)
1905 /* Clearly indicate, was it fork or vfork */ 1896 /* Clearly indicate, was it fork or vfork */
1906 bb_perror_msg(BB_MMU ? "fork" : "vfork"); 1897 bb_perror_msg(BB_MMU ? "fork" : "vfork");
1907 } else { 1898 } else {
1908 pi->running_progs++; 1899 pi->alive_progs++;
1909#if ENABLE_HUSH_JOB 1900#if ENABLE_HUSH_JOB
1910 /* Second and next children need to know pid of first one */ 1901 /* Second and next children need to know pid of first one */
1911 if (pi->pgrp < 0) 1902 if (pi->pgrp < 0)
@@ -1921,12 +1912,12 @@ static int run_pipe(struct pipe *pi)
1921 nextin = pipefds[0]; 1912 nextin = pipefds[0];
1922 } 1913 }
1923 1914
1924 if (!pi->running_progs) { 1915 if (!pi->alive_progs) {
1925 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); 1916 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
1926 return 1; 1917 return 1;
1927 } 1918 }
1928 1919
1929 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs); 1920 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_progs);
1930 return -1; 1921 return -1;
1931} 1922}
1932 1923
@@ -4205,7 +4196,7 @@ static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
4205 const char *status_string; 4196 const char *status_string;
4206 4197
4207 for (job = job_list; job; job = job->next) { 4198 for (job = job_list; job; job = job->next) {
4208 if (job->running_progs == job->stopped_progs) 4199 if (job->alive_progs == job->stopped_progs)
4209 status_string = "Stopped"; 4200 status_string = "Stopped";
4210 else 4201 else
4211 status_string = "Running"; 4202 status_string = "Running";