using fork() again and pipefd to pass it arguments

it works now, also the parent process correctly reads the output
This commit is contained in:
Jaromil 2011-02-07 11:27:53 +01:00
parent c8e623e450
commit 4f3204e1e8
2 changed files with 49 additions and 20 deletions

View File

@ -20,7 +20,7 @@
# this source code; if not, write to: # this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
VERSION=0.9.1 VERSION=0.9.2
DATE=Feb/2011 DATE=Feb/2011
# PATH=/usr/bin:/usr/sbin:/bin:/sbin # PATH=/usr/bin:/usr/sbin:/bin:/sbin
@ -134,7 +134,7 @@ ask_password() {
exec_as_user xhost 2>&1 >/dev/null exec_as_user xhost 2>&1 >/dev/null
if [ $? = 0 ]; then # we have access to the X display if [ $? = 0 ]; then # we have access to the X display
exec_as_user which tomb-askpass # 2>&1 > /dev/null exec_as_user which tomb-askpass > /dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
export scolopendro="`exec_as_user tomb-askpass ${1} 2>/dev/null`" export scolopendro="`exec_as_user tomb-askpass ${1} 2>/dev/null`"
return return
@ -192,10 +192,9 @@ exec_as_user() {
return $? return $?
fi fi
func "executing as user '$SUDO_USER': ${(f)@}" func "exec_as_user '$SUDO_USER': ${(f)@}"
which sudo > /dev/null which sudo > /dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
func "Using sudo for execution of '${(f)@}' as user $SUDO_USER"
sudo -u $SUDO_USER "${@[@]}" sudo -u $SUDO_USER "${@[@]}"
return $? return $?
else else
@ -212,14 +211,14 @@ check_priv() {
which gksu > /dev/null which gksu > /dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
func "Using gksu for root execution of 'tomb ${(f)ARGS}'" func "Using gksu for root execution of 'tomb ${(f)ARGS}'"
gksu "tomb -q ${ARGS[@]}" gksu "tomb ${ARGS[@]}"
exit 0 exit $?
fi fi
which sudo > /dev/null which sudo > /dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
func "Using sudo for root execution of 'tomb ${(f)ARGS}'" func "Using sudo for root execution of 'tomb ${(f)ARGS}'"
sudo "tomb -q ${ARGS[@]}" sudo "tomb ${ARGS[@]}"
exit 0 exit $?
fi fi
return 1 return 1
fi fi
@ -236,8 +235,12 @@ if [ $? != 0 ]; then
notice "Tomb - simple commandline tool for encrypted storage" notice "Tomb - simple commandline tool for encrypted storage"
act "version $VERSION ($DATE) by Jaromil @ dyne.org" act "version $VERSION ($DATE) by Jaromil @ dyne.org"
fi fi
func "invoked with args \"${(f)@}\" " echo $@ | grep '\-D' 2>&1 > /dev/null
func "running on `date`" if [ $? = 0 ]; then
echo "[D] invoked with args \"${(f)@}\" "
echo "[D] running on `date`"
fi
ARGS=$@[@] ARGS=$@[@]
OPTS=`getopt -o hvqDs:k: -n 'tomb' -- "$@"` OPTS=`getopt -o hvqDs:k: -n 'tomb' -- "$@"`
@ -655,7 +658,7 @@ umount_tomb() {
# check if there are binded dirs and close them first # check if there are binded dirs and close them first
mount | grep "${tombmount}" | grep -v loop 2>&1 > /dev/null mount | grep "${tombmount}" | grep -v loop 2>&1 > /dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
act "closing tomb $tombname binded directories" act "closing bind hooks for tomb $tombname "
unbind=`mount | grep ${tombmount} | grep -v loop | awk ' unbind=`mount | grep ${tombmount} | grep -v loop | awk '
{ print "umount " $3 "; " } { print "umount " $3 "; " }
'` '`
@ -668,7 +671,7 @@ umount_tomb() {
act "closing tomb $tombname on dm-crypt $basemap" act "closing tomb $tombname on dm-crypt $basemap"
mount | grep $mapper 2>&1 >/dev/null mount | grep $mapper 2>&1 >/dev/null
if [ $? = 0 ]; then # still mounted if [ $? = 0 ]; then # still mounted
errno=`umount ${mapper}` umount ${mapper}
if ! [ $? = 0 ]; then if ! [ $? = 0 ]; then
tomb-notify "Tomb '$tombname' is too busy." \ tomb-notify "Tomb '$tombname' is too busy." \
"Close all applications and file managers, then try again." "Close all applications and file managers, then try again."
@ -811,8 +814,8 @@ case "$CMD" in
*) error "command \"$CMD\" not recognized" *) error "command \"$CMD\" not recognized"
act "try -h for help" act "try -h for help"
exit 1 return 1
;; ;;
esac esac
# return codes from called functions # return codes from called functions
exit $? return $?

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <libgen.h> #include <libgen.h>
#include <sys/types.h> #include <sys/types.h>
@ -141,6 +142,7 @@ gboolean left_click(GtkWidget *w, GdkEvent *e) {
gtk_menu_popup(menu_left, NULL, NULL, gtk_menu_popup(menu_left, NULL, NULL,
gtk_status_icon_position_menu, status_tomb, gtk_status_icon_position_menu, status_tomb,
1, gtk_get_current_event_time()); 1, gtk_get_current_event_time());
return TRUE;
} }
gboolean cb_view(GtkWidget *w, GdkEvent *e) { gboolean cb_view(GtkWidget *w, GdkEvent *e) {
// GtkWidget *dialog = // GtkWidget *dialog =
@ -165,17 +167,38 @@ gboolean cb_view(GtkWidget *w, GdkEvent *e) {
} }
gboolean cb_close(GtkWidget *w, GdkEvent *e) { gboolean cb_close(GtkWidget *w, GdkEvent *e) {
pid_t cpid = fork(); int pipefd[2];
int res; pid_t cpid;
char buf;
int c, res;
char map[256];
if (pipe(pipefd) <0) {
fprintf(stderr,"pipe creation error: %s\n", strerror(errno));
return FALSE;
}
cpid = fork();
if (cpid == -1) { if (cpid == -1) {
fprintf(stderr,"error: problem forking process\n"); fprintf(stderr,"fork error: %s\n", strerror(errno));
return FALSE; return FALSE;
} }
if (cpid == 0) { // Child if (cpid == 0) { // Child
execlp("tomb", "tomb", "close", mapper, (char*)NULL); close(pipefd[1]); // close unused write end
exit(1); for(c=0; read(pipefd[0], &buf, 1) > 0; c++)
map[c] = buf;
close(pipefd[0]);
map[c+1] = '\0';
execlp("tomb", "tomb", "close", map, (char*)NULL);
_exit(1);
} }
close(pipefd[0]); // close unused read end
write(pipefd[1], mapper, strlen(mapper));
close(pipefd[1]); // reader will see EOF
waitpid(cpid, &res, 0); waitpid(cpid, &res, 0);
fprintf(stderr,"forked child returns %i",res);
if(res==0) { if(res==0) {
gtk_main_quit(); gtk_main_quit();
notify_uninit(); notify_uninit();
@ -189,6 +212,7 @@ gboolean right_click(GtkWidget *w, GdkEvent *e) {
gtk_menu_popup(menu_right, NULL, NULL, gtk_menu_popup(menu_right, NULL, NULL,
gtk_status_icon_position_menu, status_tomb, gtk_status_icon_position_menu, status_tomb,
1, gtk_get_current_event_time()); 1, gtk_get_current_event_time());
return TRUE;
} }
gboolean cb_about(GtkWidget *w, GdkEvent *e) { gboolean cb_about(GtkWidget *w, GdkEvent *e) {
const gchar *authors[] = {"Tomb is written by Jaromil - http://jaromil.dyne.org",NULL}; const gchar *authors[] = {"Tomb is written by Jaromil - http://jaromil.dyne.org",NULL};
@ -230,5 +254,7 @@ gboolean cb_about(GtkWidget *w, GdkEvent *e) {
gtk_about_dialog_set_wrap_license(GTK_ABOUT_DIALOG(dialog), TRUE); gtk_about_dialog_set_wrap_license(GTK_ABOUT_DIALOG(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG (dialog)); gtk_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy(dialog); gtk_widget_destroy(dialog);
return TRUE;
} }