summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2013-02-12 20:37:10 (UTC)
committer Michael Krelin <hacker@klever.net>2013-02-12 20:37:10 (UTC)
commit13fb4abba3fd3cac0d5cb25d3eccddc298220d41 (patch) (unidiff)
tree8a086ffef06f153e739674a5c8beab1db9388238 /src
parentb80844f51353339cfbb8b35a5585911cdb4301e2 (diff)
downloadiii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.zip
iii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.tar.gz
iii-13fb4abba3fd3cac0d5cb25d3eccddc298220d41.tar.bz2
introduce throwable_exit for nicer stack unwinding
yes, I enjoy abusing features Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiworker.cc4
-rw-r--r--src/eyefiworker.h1
-rw-r--r--src/eyetil.h5
-rw-r--r--src/iiid.cc4
4 files changed, 12 insertions, 2 deletions
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc
index ac75fc1..1979b46 100644
--- a/src/eyefiworker.cc
+++ b/src/eyefiworker.cc
@@ -18,58 +18,60 @@
18# include "iiidb.h" 18# include "iiidb.h"
19#endif 19#endif
20 20
21eyefiworker::eyefiworker() 21eyefiworker::eyefiworker()
22 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { 22 : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) {
23 bind_flags = SO_REUSEADDR; max_keep_alive = 0; 23 bind_flags = SO_REUSEADDR; max_keep_alive = 0;
24 socket_flags = 24 socket_flags =
25#if defined(MSG_NOSIGNAL) 25#if defined(MSG_NOSIGNAL)
26 MSG_NOSIGNAL 26 MSG_NOSIGNAL
27#elif defined(SO_NOSIGPIPE) 27#elif defined(SO_NOSIGPIPE)
28 SO_NOSIGPIPE 28 SO_NOSIGPIPE
29#else 29#else
30#error Something is wrong with sigpipe prevention on the platform 30#error Something is wrong with sigpipe prevention on the platform
31#endif 31#endif
32 ; 32 ;
33 } 33 }
34eyefiworker::~eyefiworker() {
35}
34 36
35int eyefiworker::run(int bindport) { 37int eyefiworker::run(int bindport) {
36#ifdef HAVE_SQLITE 38#ifdef HAVE_SQLITE
37 sqlite3_initialize(); 39 sqlite3_initialize();
38#endif 40#endif
39 if(!soap_valid_socket(bind(0,bindport,64))) 41 if(!soap_valid_socket(bind(0,bindport,64)))
40 throw std::runtime_error("failed to bind()"); 42 throw std::runtime_error("failed to bind()");
41 signal(SIGCHLD,SIG_IGN); 43 signal(SIGCHLD,SIG_IGN);
42 while(true) { 44 while(true) {
43 if(!soap_valid_socket(accept())) 45 if(!soap_valid_socket(accept()))
44 throw std::runtime_error("failed to accept()"); 46 throw std::runtime_error("failed to accept()");
45 pid_t p = fork(); 47 pid_t p = fork();
46 if(p<0) throw std::runtime_error("failed to fork()"); 48 if(p<0) throw std::runtime_error("failed to fork()");
47 if(!p) { 49 if(!p) {
48 recv_timeout = 600; send_timeout = 120; 50 recv_timeout = 600; send_timeout = 120;
49 (void)serve(); 51 (void)serve();
50 soap_destroy(this); soap_end(this); soap_done(this); 52 soap_destroy(this); soap_end(this); soap_done(this);
51#ifndef NDEBUG 53#ifndef NDEBUG
52 struct rusage ru; 54 struct rusage ru;
53 if(getrusage(RUSAGE_SELF,&ru)) { 55 if(getrusage(RUSAGE_SELF,&ru)) {
54 syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno); 56 syslog(LOG_NOTICE,"Failed to getrusage(): %d",errno);
55 }else{ 57 }else{
56 syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss); 58 syslog(LOG_INFO,"maxrss: %ld\n",ru.ru_maxrss);
57 } 59 }
58#endif /* NDEBUG */ 60#endif /* NDEBUG */
59 _exit(0); 61 throw throwable_exit(0);
60 } 62 }
61 close(socket); socket = SOAP_INVALID_SOCKET; 63 close(socket); socket = SOAP_INVALID_SOCKET;
62 } 64 }
63} 65}
64 66
65static binary_t session_nonce; 67static binary_t session_nonce;
66#ifdef HAVE_SQLITE 68#ifdef HAVE_SQLITE
67 static struct { 69 static struct {
68 std::string filesignature; 70 std::string filesignature;
69 long filesize; 71 long filesize;
70 std::string filename; 72 std::string filename;
71 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } 73 inline void reset() { filesignature.erase(); filename.erase(); filesize=0; }
72 inline void set(const std::string n,const std::string sig,long siz) { 74 inline void set(const std::string n,const std::string sig,long siz) {
73 filename = n; filesignature = sig; filesize = siz; 75 filename = n; filesignature = sig; filesize = siz;
74 } 76 }
75 inline bool is(const std::string n,const std::string sig,long siz) { 77 inline bool is(const std::string n,const std::string sig,long siz) {
diff --git a/src/eyefiworker.h b/src/eyefiworker.h
index 6d4082c..6cdecff 100644
--- a/src/eyefiworker.h
+++ b/src/eyefiworker.h
@@ -1,25 +1,26 @@
1#ifndef __EYEFIWORKER_H 1#ifndef __EYEFIWORKER_H
2#define __EYEFIWORKER_H 2#define __EYEFIWORKER_H
3 3
4#include "soapeyefiService.h" 4#include "soapeyefiService.h"
5 5
6class eyefiworker : public eyefiService { 6class eyefiworker : public eyefiService {
7 public: 7 public:
8 8
9 eyefiworker(); 9 eyefiworker();
10 ~eyefiworker();
10 11
11 int run(int port) __attribute__ ((noreturn)); 12 int run(int port) __attribute__ ((noreturn));
12 13
13 int StartSession(std::string macaddress, std::string cnonce, 14 int StartSession(std::string macaddress, std::string cnonce,
14 int transfermode, long transfermodetimestamp, 15 int transfermode, long transfermodetimestamp,
15 struct rns__StartSessionResponse &r); 16 struct rns__StartSessionResponse &r);
16 int GetPhotoStatus(std::string credential, std::string macaddress, 17 int GetPhotoStatus(std::string credential, std::string macaddress,
17 std::string filename, long filesize, std::string filesignature, int flags, 18 std::string filename, long filesize, std::string filesignature, int flags,
18 struct rns__GetPhotoStatusResponse &r); 19 struct rns__GetPhotoStatusResponse &r);
19 int MarkLastPhotoInRoll(std::string macaddress, int mergedelta, 20 int MarkLastPhotoInRoll(std::string macaddress, int mergedelta,
20 struct rns__MarkLastPhotoInRollResponse &r); 21 struct rns__MarkLastPhotoInRollResponse &r);
21 int UploadPhoto(int fileid, std::string macaddress, 22 int UploadPhoto(int fileid, std::string macaddress,
22 std::string filename, long filesize, std::string filesignature, 23 std::string filename, long filesize, std::string filesignature,
23 std::string encryption, int flags, 24 std::string encryption, int flags,
24 struct rns__UploadPhotoResponse &r); 25 struct rns__UploadPhotoResponse &r);
25 26
diff --git a/src/eyetil.h b/src/eyetil.h
index eff2c43..03b9ba8 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -1,25 +1,30 @@
1#ifndef __EYETIL_H 1#ifndef __EYETIL_H
2#define __EYETIL_H 2#define __EYETIL_H
3 3
4#include <vector> 4#include <vector>
5#include <string> 5#include <string>
6#include <archive.h> 6#include <archive.h>
7#include <archive_entry.h> 7#include <archive_entry.h>
8#include "openssl/md5.h" 8#include "openssl/md5.h"
9 9
10struct throwable_exit {
11 int rc;
12 throwable_exit(int rc_) : rc(rc_) { }
13};
14
10class binary_t : public std::vector<unsigned char> { 15class binary_t : public std::vector<unsigned char> {
11 public: 16 public:
12 binary_t() { } 17 binary_t() { }
13 binary_t(size_type n) : std::vector<unsigned char>(n) { } 18 binary_t(size_type n) : std::vector<unsigned char>(n) { }
14 binary_t(const std::string& h) { from_hex(h); } 19 binary_t(const std::string& h) { from_hex(h); }
15 binary_t(const void *d,size_t s) { from_data(d,s); } 20 binary_t(const void *d,size_t s) { from_data(d,s); }
16 21
17 binary_t& from_hex(const std::string& h); 22 binary_t& from_hex(const std::string& h);
18 binary_t& from_data(const void *d,size_t s); 23 binary_t& from_data(const void *d,size_t s);
19 binary_t& make_nonce(); 24 binary_t& make_nonce();
20 25
21 std::string hex() const; 26 std::string hex() const;
22 binary_t md5() const; 27 binary_t md5() const;
23}; 28};
24 29
25struct md5_digester { 30struct md5_digester {
diff --git a/src/iiid.cc b/src/iiid.cc
index d655fe3..b3dd3bf 100644
--- a/src/iiid.cc
+++ b/src/iiid.cc
@@ -76,22 +76,24 @@ int main(int argc,char **argv) try {
76 openlog(ident,LOG_PERROR|LOG_PID,LOG_DAEMON); 76 openlog(ident,LOG_PERROR|LOG_PID,LOG_DAEMON);
77 syslog(LOG_INFO,"Starting iii eye-fi manager"); 77 syslog(LOG_INFO,"Starting iii eye-fi manager");
78 78
79 struct stat st; 79 struct stat st;
80 if(stat(EYEKIN_CONF_DIR,&st) || !S_ISDIR(st.st_mode)) 80 if(stat(EYEKIN_CONF_DIR,&st) || !S_ISDIR(st.st_mode))
81 syslog(LOG_WARNING,"configuration directory '%s' does not exist or is not a directory",EYEKIN_CONF_DIR); 81 syslog(LOG_WARNING,"configuration directory '%s' does not exist or is not a directory",EYEKIN_CONF_DIR);
82 glob_t g; int rg = glob(EYEKIN_CONF_DIR"/????????????.conf",GLOB_NOSORT,NULL,&g); 82 glob_t g; int rg = glob(EYEKIN_CONF_DIR"/????????????.conf",GLOB_NOSORT,NULL,&g);
83 if(rg || !g.gl_pathc) 83 if(rg || !g.gl_pathc)
84 syslog(LOG_WARNING,"I see nothing resembling a card config in '%s'",EYEKIN_CONF_DIR); 84 syslog(LOG_WARNING,"I see nothing resembling a card config in '%s'",EYEKIN_CONF_DIR);
85 else 85 else
86 globfree(&g); 86 globfree(&g);
87 87
88 eyefiworker().run(port); 88 eyefiworker().run(port);
89 89
90 closelog(); 90 closelog();
91 return 0; 91 return 0;
92} catch(std::exception& e) { 92} catch(const throwable_exit& e) {
93 return e.rc;
94} catch(const std::exception& e) {
93 syslog(LOG_CRIT,"Exiting iii daemon, because of error condition"); 95 syslog(LOG_CRIT,"Exiting iii daemon, because of error condition");
94 syslog(LOG_CRIT,"Exception: %s",e.what()); 96 syslog(LOG_CRIT,"Exception: %s",e.what());
95 return 1; 97 return 1;
96} 98}
97 99