author | Michael Krelin <hacker@klever.net> | 2011-03-20 02:19:04 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2011-03-20 02:19:04 (UTC) |
commit | 16c80883ba6c8657bc6129fb9e2695eaf3d3c09b (patch) (unidiff) | |
tree | 14861e99fe9522a2e33b076325815a46f415858f | |
parent | c457b6da4401d13c3d914f34b8a41a1711027dd2 (diff) | |
download | iii-16c80883ba6c8657bc6129fb9e2695eaf3d3c09b.zip iii-16c80883ba6c8657bc6129fb9e2695eaf3d3c09b.tar.gz iii-16c80883ba6c8657bc6129fb9e2695eaf3d3c09b.tar.bz2 |
don't double-fork
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyefiservice.cc | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 0bf26c4..4ca2777 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -1,85 +1,76 @@ | |||
1 | #include <cassert> | 1 | #include <cassert> |
2 | #include <iostream> | 2 | #include <iostream> |
3 | #include <fstream> | 3 | #include <fstream> |
4 | #include <stdexcept> | 4 | #include <stdexcept> |
5 | #include <iterator> | 5 | #include <iterator> |
6 | #include <syslog.h> | 6 | #include <syslog.h> |
7 | #include <sys/wait.h> | 7 | #include <sys/wait.h> |
8 | #include <autosprintf.h> | 8 | #include <autosprintf.h> |
9 | #include "eyekinfig.h" | 9 | #include "eyekinfig.h" |
10 | #include "eyetil.h" | 10 | #include "eyetil.h" |
11 | #include "soapeyefiService.h" | 11 | #include "soapeyefiService.h" |
12 | 12 | ||
13 | static binary_t session_nonce; | 13 | static binary_t session_nonce; |
14 | 14 | ||
15 | static bool detached_child() { | 15 | static bool detached_child() { |
16 | pid_t p = fork(); | 16 | pid_t p = fork(); |
17 | if(p<0) throw std::runtime_error("failed to fork()"); | 17 | if(p<0) { |
18 | syslog(LOG_ERR,"Failed to fork away for hook execution"); | ||
19 | _exit(-1); | ||
20 | } | ||
18 | if(!p) { | 21 | if(!p) { |
19 | p = fork(); | 22 | setsid(); |
20 | if(p<0) { | 23 | for(int i=getdtablesize();i>=0;--i) close(i); |
21 | syslog(LOG_ERR,"Failed to re-fork child process"); | 24 | int i=open("/dev/null",O_RDWR); assert(i==0); |
22 | _exit(-1); | 25 | i = dup(i); assert(i==1); |
23 | } | 26 | i = dup(i); assert(i==2); |
24 | if(!p) { | 27 | return true; |
25 | setsid(); | ||
26 | for(int i=getdtablesize();i>=0;--i) close(i); | ||
27 | int i=open("/dev/null",O_RDWR); assert(i==0); | ||
28 | i = dup(i); assert(i==1); | ||
29 | i = dup(i); assert(i==2); | ||
30 | return true; | ||
31 | } | ||
32 | _exit(0); | ||
33 | } | 28 | } |
34 | int rc; | ||
35 | if(waitpid(p,&rc,0)<0) throw std::runtime_error("failed to waitpid()"); | ||
36 | if(!WIFEXITED(rc)) throw std::runtime_error("error in forked process"); | ||
37 | if(WEXITSTATUS(rc)) throw std::runtime_error("forked process signalled error"); | ||
38 | return false; | 29 | return false; |
39 | } | 30 | } |
40 | 31 | ||
41 | int eyefiService::StartSession( | 32 | int eyefiService::StartSession( |
42 | std::string macaddress,std::string cnonce, | 33 | std::string macaddress,std::string cnonce, |
43 | int transfermode,long transfermodetimestamp, | 34 | int transfermode,long transfermodetimestamp, |
44 | struct rns__StartSessionResponse &r ) try { | 35 | struct rns__StartSessionResponse &r ) try { |
45 | #ifndef NDEBUG | 36 | #ifndef NDEBUG |
46 | syslog(LOG_DEBUG, | 37 | syslog(LOG_DEBUG, |
47 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", | 38 | "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", |
48 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); | 39 | macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); |
49 | #endif | 40 | #endif |
50 | eyekinfig_t eyekinfig(macaddress); | 41 | eyekinfig_t eyekinfig(macaddress); |
51 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); | 42 | r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); |
52 | 43 | ||
53 | r.snonce = session_nonce.make_nonce().hex(); | 44 | r.snonce = session_nonce.make_nonce().hex(); |
54 | r.transfermode=transfermode; | 45 | r.transfermode=transfermode; |
55 | r.transfermodetimestamp=transfermodetimestamp; | 46 | r.transfermodetimestamp=transfermodetimestamp; |
56 | r.upsyncallowed=false; | 47 | r.upsyncallowed=false; |
57 | 48 | ||
58 | std::string cmd = eyekinfig.get_on_start_session(); | 49 | std::string cmd = eyekinfig.get_on_start_session(); |
59 | if(!cmd.empty()) { | 50 | if(!cmd.empty()) { |
60 | if(detached_child()) { | 51 | if(detached_child()) { |
61 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 52 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
62 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); | 53 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); |
63 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); | 54 | putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); |
64 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; | 55 | char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; |
65 | execv("/bin/sh",argv); | 56 | execv("/bin/sh",argv); |
66 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); | 57 | syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); |
67 | _exit(-1); | 58 | _exit(-1); |
68 | } | 59 | } |
69 | } | 60 | } |
70 | return SOAP_OK; | 61 | return SOAP_OK; |
71 | }catch(std::runtime_error& e) { | 62 | }catch(std::runtime_error& e) { |
72 | syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); | 63 | syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); |
73 | } | 64 | } |
74 | 65 | ||
75 | int eyefiService::GetPhotoStatus( | 66 | int eyefiService::GetPhotoStatus( |
76 | std::string credential, std::string macaddress, | 67 | std::string credential, std::string macaddress, |
77 | std::string filename, long filesize, std::string filesignature, | 68 | std::string filename, long filesize, std::string filesignature, |
78 | int flags, | 69 | int flags, |
79 | struct rns__GetPhotoStatusResponse &r ) { | 70 | struct rns__GetPhotoStatusResponse &r ) { |
80 | #ifndef NDEBUG | 71 | #ifndef NDEBUG |
81 | syslog(LOG_DEBUG, | 72 | syslog(LOG_DEBUG, |
82 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", | 73 | "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", |
83 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, | 74 | macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, |
84 | session_nonce.hex().c_str() ); | 75 | session_nonce.hex().c_str() ); |
85 | #endif | 76 | #endif |