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

View File

@ -21,6 +21,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <sys/types.h>
@ -141,6 +142,7 @@ gboolean left_click(GtkWidget *w, GdkEvent *e) {
gtk_menu_popup(menu_left, NULL, NULL,
gtk_status_icon_position_menu, status_tomb,
1, gtk_get_current_event_time());
return TRUE;
}
gboolean cb_view(GtkWidget *w, GdkEvent *e) {
// GtkWidget *dialog =
@ -165,17 +167,38 @@ gboolean cb_view(GtkWidget *w, GdkEvent *e) {
}
gboolean cb_close(GtkWidget *w, GdkEvent *e) {
pid_t cpid = fork();
int res;
int pipefd[2];
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) {
fprintf(stderr,"error: problem forking process\n");
fprintf(stderr,"fork error: %s\n", strerror(errno));
return FALSE;
}
if (cpid == 0) { // Child
execlp("tomb", "tomb", "close", mapper, (char*)NULL);
exit(1);
close(pipefd[1]); // close unused write end
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);
fprintf(stderr,"forked child returns %i",res);
if(res==0) {
gtk_main_quit();
notify_uninit();
@ -189,6 +212,7 @@ gboolean right_click(GtkWidget *w, GdkEvent *e) {
gtk_menu_popup(menu_right, NULL, NULL,
gtk_status_icon_position_menu, status_tomb,
1, gtk_get_current_event_time());
return TRUE;
}
gboolean cb_about(GtkWidget *w, GdkEvent *e) {
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_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
return TRUE;
}