Permalink
Browse files

OK, it doesn't exit immediately after working, which is nice.

Also, I'm using it to push this commit to my repo. Sweet.
  • Loading branch information...
1 parent a3eb507 commit 77c80f652c3b8349ded68169ab7d4591c00a0210 @wesleyd committed Nov 19, 2009
Showing with 51 additions and 30 deletions.
  1. +43 −25 charade.c
  2. +7 −4 cmdline.c
  3. +1 −1 cmdline.h
View
@@ -204,28 +204,36 @@ kill_old_agent(void)
void
fork_subprocess(void)
{
- int pid = fork();
+ if (! g_dontfork_flag) {
+ long pid = fork();
- if (-1 == pid) {
- perror("fork");
- exit(1);
- }
+ if (-1 == pid) {
+ perror("fork");
+ exit(1);
+ }
+
+ if (pid) { // Parent
+ printf("%s=%s; export %s\n", SSH_AUTHSOCKET_ENV_NAME, socket_name,
+ SSH_AUTHSOCKET_ENV_NAME);
+ printf("%s=%ld; export %s\n", SSH_AGENTPID_ENV_NAME, (long) pid,
+ SSH_AGENTPID_ENV_NAME);
+
+ // TODO: If argv present, fork and exec it. Only do above if no args.
+
+ remove_socket_at_exit = 0;
+ exit(0);
+ }
+ // Child
+ if (setsid() == -1) {
+ perror("setsid");
+ }
+ } else {
+ int pid = getpid();
- if (pid) { // Parent
printf("%s=%s; export %s\n", SSH_AUTHSOCKET_ENV_NAME, socket_name,
SSH_AUTHSOCKET_ENV_NAME);
printf("%s=%ld; export %s\n", SSH_AGENTPID_ENV_NAME, (long) pid,
SSH_AGENTPID_ENV_NAME);
-
- // TODO: If argv present, fork and exec it. Only do above if no args.
-
- remove_socket_at_exit = 0;
- exit(0);
- }
-
- // Child
- if (setsid() == -1) {
- perror("setsid");
}
}
@@ -306,6 +314,24 @@ accept_new_socket(void)
}
void
+fd_is_closed(int fd)
+{
+ // Remove it from the list...
+
+ // TODO: Remove the fd from the big list!
+ struct socklist_node_t *p;
+ for (p = socklist.tqh_first; p != NULL; p = p->next.tqe_next) {
+ if (p->fd == fd) {
+ TAILQ_REMOVE(&socklist, p, next);
+ break;
+ }
+ }
+
+ // ...and close it I guess...
+ close(fd);
+}
+
+void
deal_with_ready_fds(struct pollfd *fds, int nfds)
{
fprintf(stderr, "%s: nfds=%d.\n", __func__, nfds);
@@ -326,8 +352,7 @@ deal_with_ready_fds(struct pollfd *fds, int nfds)
ssize_t numbytes = read(fds[i].fd, buf, count);
if (0 == numbytes) {
- fprintf(stderr, "TODO: fd %d closed.\n", fds[i].fd);
- exit(1);
+ fd_is_closed(fds[i].fd);
} else if (-1 == numbytes) {
fprintf(stderr, "TODO: fd %d error, errno is %s.\n", fds[i].fd, strerror(errno));
exit(1);
@@ -419,24 +444,17 @@ main(int argc, char **argv)
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
// TODO: sanitise_stdfd();
-#if 0
parse_cmdline(argc, argv);
if (g_kill_flag) {
kill_old_agent();
exit(0);
}
-#endif
init_socket_list();
create_socket();
-#if 0
- if (! g_debug_flag) {
- }
-#endif
-
fork_subprocess();
handle_key_requests_forever();
View
@@ -13,7 +13,7 @@
int g_csh_flag = 0;
int g_sh_flag = 0;
int g_kill_flag = 0;
-int g_debug_flag = 0;
+int g_dontfork_flag = 0;
char *g_socket_name = 0;
int g_subprocess_argc;
@@ -27,7 +27,8 @@ usage(void)
EPRINTF(0, " -c Generate C-shell commands on stdout.\n");
EPRINTF(0, " -s Generate Bourne shell commands on stdout.\n");
EPRINTF(0, " -k Kill the current agent.\n");
- EPRINTF(0, " -d Debug mode.\n");
+ EPRINTF(0, " -d Don't fork.\n");
+ EPRINTF(0, " -v [-v ...] More trace output.\n");
EPRINTF(0, " -a socket Bind agent socket to given name.\n");
exit(1);
}
@@ -39,15 +40,17 @@ parse_cmdline(int argc, char **argv)
extern int optind, opterr;
extern char *optarg;
- while (-1 != (ch = getopt(argc, argv, "cskda:"))) {
+ while (-1 != (ch = getopt(argc, argv, "cskdva:"))) {
switch (ch) {
case 'c': ++g_csh_flag;
break;
case 's': ++g_sh_flag;
break;
case 'k': ++g_kill_flag;
break;
- case 'd': louder();
+ case 'd': g_dontfork_flag = 1;
+ break;
+ case 'v': louder();
break;
case 'a': g_socket_name = optarg;
break;
View
@@ -10,7 +10,7 @@
extern int g_csh_flag;
extern int g_sh_flag;
extern int g_kill_flag;
-extern int g_debug_flag;
+extern int g_dontfork_flag;
extern char *g_socket_name;
extern int g_subprocess_argc;

0 comments on commit 77c80f6

Please sign in to comment.