diff options
author | deva <deva> | 2006-07-20 17:53:04 +0000 |
---|---|---|
committer | deva <deva> | 2006-07-20 17:53:04 +0000 |
commit | c8df40b62cf30029a4acd1a57c8c54add54dda9b (patch) | |
tree | 42d06a4d502c6e0fd0cdbf7f836fdaffd847efa5 | |
parent | a09f1b932560f11caf34a567ee226446a496a584 (diff) |
Utilized the global info object.
Utilized the global info object.
-rw-r--r-- | lib/daemon.cc | 19 | ||||
-rw-r--r-- | lib/file.cc | 37 | ||||
-rw-r--r-- | lib/file.h | 4 | ||||
-rw-r--r-- | lib/info.cc | 2 | ||||
-rw-r--r-- | lib/info.h | 19 | ||||
-rw-r--r-- | lib/network.cc | 23 | ||||
-rw-r--r-- | lib/network.h | 4 | ||||
-rw-r--r-- | lib/socket.cc | 23 | ||||
-rw-r--r-- | lib/socket.h | 7 | ||||
-rw-r--r-- | lib/threadsafe_queue_priority.cc | 5 | ||||
-rw-r--r-- | lib/threadsafe_queue_priority.h | 6 |
11 files changed, 70 insertions, 79 deletions
diff --git a/lib/daemon.cc b/lib/daemon.cc index 6e46bd5..fc052ff 100644 --- a/lib/daemon.cc +++ b/lib/daemon.cc @@ -31,7 +31,7 @@ #include <signal.h> #include <stdio.h> -// For getgrent and getgrent +// For getpwent and getgrent #include <sys/types.h> #include <grp.h> #include <pwd.h> @@ -47,9 +47,6 @@ Daemon::~Daemon() int Daemon::run(const char *user, const char* group) { - int f; - int fd; - // Fetch user id int uid = -1; struct passwd *p = getpwent(); @@ -72,6 +69,8 @@ int Daemon::run(const char *user, const char* group) fprintf(stderr, "Could not find group \"%s\" in /etc/group file.\n", group); } + + /* chdir("/"); umask(0); @@ -86,19 +85,22 @@ int Daemon::run(const char *user, const char* group) if(setgid(gid) != 0) { fprintf(stderr, "Failed to change to group \"%s\" (gid: %d), quitting.\n", group, gid); perror(""); - fprintf(stderr, "Runnning daemon as current group\n"); + fprintf(stderr, "Running daemon as current group\n"); } // Switch to given user if(setuid(uid) != 0) { fprintf(stderr, "Failed to change to user \"%s\" (uid: %d), quitting.\n", user, uid); perror(""); - fprintf(stderr, "Runnning daemon as current user\n"); + fprintf(stderr, "Running daemon as current user\n"); } // Redirect stdin, stdout and stderr to /dev/null fd = open("/dev/null", O_NOCTTY | O_RDWR, 0666); + int f; + int fd; + dup2(0, fd); dup2(1, fd); dup2(2, fd); @@ -115,4 +117,9 @@ int Daemon::run(const char *user, const char* group) // exit(0); return 0; } + */ + + // Glibc to do all of the above + daemon(0,0); + return daemon_main(); } diff --git a/lib/file.cc b/lib/file.cc index 6bd8438..88e8df1 100644 --- a/lib/file.cc +++ b/lib/file.cc @@ -28,6 +28,7 @@ #include "file.h" #include "miav_config.h" +#include "info.h" #include <sys/types.h> #include <sys/stat.h> @@ -42,12 +43,10 @@ #include <stdlib.h> -File::File(char *fn, char* ext, Info *i) +File::File(char *fn, char* ext) { char path[256]; - info = i; - savestate = SAVE; filename = new char[strlen(fn) + 1]; @@ -76,9 +75,9 @@ File::~File() { close(fd); - info->info("This session contains the following files..."); + MIaV::info->info("This session contains the following files..."); for(unsigned int cnt = 0; cnt < filelist.size(); cnt ++) { - info->info("[%s]", filelist[cnt].c_str()); + MIaV::info->info("[%s]", filelist[cnt].c_str()); } std::string *trash = MIaV::config->readString("server_trash"); @@ -86,20 +85,20 @@ File::~File() switch(savestate) { case NO_CHANGE: - info->warn("File had no savestate!"); + MIaV::info->warn("File had no savestate!"); break; case SAVE: - info->info("Files in this session is to be saved."); + MIaV::info->info("Files in this session is to be saved."); break; case DELETE: - info->info("Files in this session is to be deleted (moved to trash)."); + MIaV::info->info("Files in this session is to be deleted (moved to trash)."); Move((char*)trash->c_str()); break; case LATER: - info->info("Files in this session is stored for later decisson."); + MIaV::info->info("Files in this session is stored for later decisson."); Move((char*)later->c_str()); break; } @@ -122,10 +121,10 @@ int File::Move(char *destination) strcpy(filename, (char*)filelist[cnt].c_str()); sprintf(newfile, "%s%s", destination, strrchr(filename, '/')); if(rename((char*)filelist[cnt].c_str(), newfile) == -1) - info->error("Error moving file %s to %s:", - (char*)filelist[cnt].c_str(), - newfile, - strerror(errno)); + MIaV::info->error("Error moving file %s to %s:", + (char*)filelist[cnt].c_str(), + newfile, + strerror(errno)); } return 0; } @@ -153,7 +152,7 @@ int File::Open() // If more than 100 files are created in one day, something is terribly wrong! if(num > 100) { - info->error("Something is wrong with the path [%s]!", fname); + MIaV::info->error("Something is wrong with the path [%s]!", fname); exit(1); } @@ -164,7 +163,7 @@ int File::Open() seqnum ++; - info->info("Output file: %s", fname); + MIaV::info->info("Output file: %s", fname); return 0; } @@ -176,11 +175,11 @@ int File::Write(void* data, int size) w = write(fd, data, size); if(w != size) { - info->info("Wrapping file."); + MIaV::info->info("Wrapping file."); Open(); w = write(fd, data, size); if(w != size) { - info->error("Out of diskspace!"); + MIaV::info->error("Out of diskspace!"); return -1; } } @@ -201,7 +200,7 @@ int File::createPath(char* path) if(strlen(subpath) > 0) createPath(subpath); - info->info("Checking and/or generating directory: %s", path); + MIaV::info->info("Checking and/or generating directory: %s", path); // stat(path, &stats); //if(!S_ISDIR(stats.st_mode) && S_ISREG(stats.st_mode)) @@ -216,7 +215,7 @@ int File::createPath(char* path) void File::setSaveState(n_savestate s) { savestate = s; - info->info("SETTING SAVESTATE TO: %d", savestate); + MIaV::info->info("SETTING SAVESTATE TO: %d", savestate); } #ifdef __TEST_FILE @@ -28,7 +28,6 @@ #ifndef __MIAV_FILE_H__ #define __MIAV_FILE_H__ -#include "info.h" #include <stdio.h> #include <vector> @@ -41,7 +40,7 @@ class File { public: - File(char *filename, char* ext, Info* info); + File(char *filename, char* ext); ~File(); int Write(void* data, int size); @@ -62,7 +61,6 @@ public: private: volatile n_savestate savestate; - Info* info; std::vector<std::string> filelist; diff --git a/lib/info.cc b/lib/info.cc index a7b5f3e..86e4cc4 100644 --- a/lib/info.cc +++ b/lib/info.cc @@ -24,10 +24,10 @@ * along with MIaV; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#include <config.h> #include "info.h" #include <time.h> +#include <stdarg.h> Info::Info() { pthread_mutex_init (&mutex, NULL); @@ -28,18 +28,13 @@ #ifndef __MIAV_INFO_H__ #define __MIAV_INFO_H__ -#include "miav_config.h" -// Cyclic include :( -class MiavConfig; - -#include <time.h> -#include <sys/types.h> -#include <unistd.h> -#include <stdarg.h> +//#include <time.h> +//#include <sys/types.h> +//#include <unistd.h> #include <pthread.h> -#include <semaphore.h> +//#include <semaphore.h> #include <string> -using namespace std; +//using namespace std; class Info { public: @@ -52,10 +47,10 @@ public: void log(char* fmt, ...); protected: - MiavConfig *config; + // MiavConfig *config; pthread_mutex_t mutex; - string log_filename; + std::string log_filename; }; // For the global info object diff --git a/lib/network.cc b/lib/network.cc index 799bc98..f00d358 100644 --- a/lib/network.cc +++ b/lib/network.cc @@ -27,14 +27,15 @@ #include <config.h> #include "network.h" +#include "info.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> -Network::Network(Socket *gs, Info *ginfo) +Network::Network(Socket *gs) { - info = ginfo; s = gs; } @@ -45,13 +46,13 @@ Network::~Network() int Network::write(void *buf, int size) { if(!s->isConnected()) { - // info->error("Write attempted to a socket not connected!"); + // MIaV::info->error("Write attempted to a socket not connected!"); return -1; } int n = send(s->ssocket, buf, size, MSG_WAITALL); if(n == -1) { - info->error("An error occurred!"); + MIaV::info->error("An error occurred!"); } return n; @@ -60,13 +61,13 @@ int Network::write(void *buf, int size) int Network::read(void *buf, int size) { if(!s->isConnected()) { - // info->error("Read attempted from a socket not connected!"); + // MIaV::info->error("Read attempted from a socket not connected!"); return -1; } int n = recv(s->ssocket, buf, size, MSG_WAITALL); if(n == -1) { - info->error("An error occurred!"); + MIaV::info->error("An error occurred!"); } return n; @@ -90,7 +91,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz) struct iovec iovecs[2]; if(!s->isConnected()) { - // info->error("Write attempted to a socket not connected!"); + // MIaV::info->error("Write attempted to a socket not connected!"); return -1; } @@ -107,7 +108,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz) int n = sendmsg(s->ssocket, &msg, 0); if(n < 0) { - info->error("A network error ocurred during sendPackage!"); + MIaV::info->error("A network error ocurred during sendPackage!"); return -1; } @@ -120,7 +121,7 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz) struct iovec iovecs[2]; if(!s->isConnected()) { - // info->error("Read attempted to a socket not connected!"); + // MIaV::info->error("Read attempted to a socket not connected!"); return -1; } @@ -138,12 +139,12 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz) int n = recvmsg(s->ssocket, &msg, MSG_WAITALL); if(n < 0) { - info->error("A network error ocurred during recvPackage!"); + MIaV::info->error("A network error ocurred during recvPackage!"); return -1; } if(msg.msg_iovlen != 2) { - info->error("Wrong package format!"); + MIaV::info->error("Wrong package format!"); return -1; } return n; diff --git a/lib/network.h b/lib/network.h index f64310e..e00dac7 100644 --- a/lib/network.h +++ b/lib/network.h @@ -30,11 +30,10 @@ #include "socket.h" #include "package.h" -#include "info.h" class Network { public: - Network(Socket *gs, Info* ginfo); + Network(Socket *gs); ~Network(); // Raw communication @@ -46,7 +45,6 @@ public: int recvPackage(n_header *h, void* buf, int bufsz); private: - Info *info; Socket *s; }; diff --git a/lib/socket.cc b/lib/socket.cc index 2ae88dc..6189d23 100644 --- a/lib/socket.cc +++ b/lib/socket.cc @@ -27,19 +27,18 @@ #include <config.h> #include "socket.h" +#include "info.h" #include <errno.h> -Socket::Socket(Info *ginfo) +Socket::Socket() { - info = ginfo; connected = false; err = 0; } -Socket::Socket(u_short port, Info *ginfo) +Socket::Socket(u_short port) { - info = ginfo; connected = false; err = 0; @@ -51,7 +50,7 @@ Socket::Socket(u_short port, Info *ginfo) if (ssocket < 0) { err = 1; - info->error("Socket: socket() failed!"); + MIaV::info->error("Socket: socket() failed!"); } socketaddr.sin_family = AF_INET; // Use "internet protocol" IP @@ -71,10 +70,10 @@ Socket::~Socket() Socket Socket::slisten() { - Socket s = Socket(info); + Socket s; if(err) { - //info->error("Socket: No socket present!"); + //MIaV::info->error("Socket: No socket present!"); return s; } if(!connected) { @@ -82,7 +81,7 @@ Socket Socket::slisten() err = bind(ssocket, (struct sockaddr*)&socketaddr, sizeof(socketaddr)); if (err) { - info->error("Socket: bind() failed! %s", strerror(errno)); + MIaV::info->error("Socket: bind() failed! %s", strerror(errno)); return s; } @@ -90,7 +89,7 @@ Socket Socket::slisten() // requests (max 5 in queue) err = listen(ssocket, 5); if(err) { - info->error("Socket: listen() failed! %s", strerror(errno)); + MIaV::info->error("Socket: listen() failed! %s", strerror(errno)); return s; } } @@ -105,7 +104,7 @@ Socket Socket::slisten() if (s.ssocket < 0) { s.connected = false; err = 1; - info->error("Socket: accept() failed! %s", strerror(errno)); + MIaV::info->error("Socket: accept() failed! %s", strerror(errno)); return s; } @@ -119,7 +118,7 @@ int Socket::sconnect(char *ip) { if(err) { connected = false; - info->error("Socket: No socket present!"); + MIaV::info->error("Socket: No socket present!"); return err; } @@ -130,7 +129,7 @@ int Socket::sconnect(char *ip) err = connect(ssocket, (struct sockaddr*)&socketaddr, sizeof(socketaddr)); if (err) { connected = false; - info->error("Socket: connect() failed! %s", strerror(errno)); + MIaV::info->error("Socket: connect() failed! %s", strerror(errno)); return err; } // fprintf(stderr, "Socket connected\n"); diff --git a/lib/socket.h b/lib/socket.h index df2a133..dde4729 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -37,12 +37,10 @@ #include <sys/socket.h> #include <arpa/inet.h> -#include "info.h" - class Socket { public: - Socket(Info *ginfo); - Socket(u_short port, Info *ginfo); + Socket(); + Socket(u_short port); ~Socket(); Socket slisten(); int sconnect(char *ip); @@ -54,7 +52,6 @@ public: bool connected; private: - Info *info; int err; }; diff --git a/lib/threadsafe_queue_priority.cc b/lib/threadsafe_queue_priority.cc index df7ae8c..c2445de 100644 --- a/lib/threadsafe_queue_priority.cc +++ b/lib/threadsafe_queue_priority.cc @@ -29,10 +29,11 @@ #include "util.h" -ThreadSafeQueuePriority::ThreadSafeQueuePriority(Info* i, unsigned int number) +#include "info.h" + +ThreadSafeQueuePriority::ThreadSafeQueuePriority(unsigned int number) // : ThreadSafeQueue< Frame* >() { - info = i; framenumber = number; } diff --git a/lib/threadsafe_queue_priority.h b/lib/threadsafe_queue_priority.h index 8d3cdf1..a310271 100644 --- a/lib/threadsafe_queue_priority.h +++ b/lib/threadsafe_queue_priority.h @@ -35,8 +35,6 @@ #include <queue> #include <functional> -#include "info.h" - // Method for use, when comparing Frames in priority queue. template <typename T> struct priority : std::binary_function<T, T, bool> { @@ -47,7 +45,7 @@ struct priority : std::binary_function<T, T, bool> { class ThreadSafeQueuePriority: public ThreadSafeQueue< Frame* > { public: - ThreadSafeQueuePriority(Info *info, unsigned int framenumber = 0); + ThreadSafeQueuePriority(unsigned int framenumber = 0); ~ThreadSafeQueuePriority(); void push(Frame *frame); @@ -55,8 +53,6 @@ public: int size(); private: - Info* info; - unsigned int framenumber; std::priority_queue< Frame*, std::vector<Frame*>, priority<Frame*> > queue; }; |