• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            看前面黑咚咚,待俺上前殺它個干干凈凈。。。

             

            Linux守護程序類-daemon

            /////////////////////////////////////////////////////////////////////////////////////
            ///   daemon 測試類
            #include <unistd.h>
            #include <syslog.h>
            #include <stdlib.h>
            #include "daemon.h"

            class logger : public daemon
            {
            public:
                logger() : daemon("日志")
                {}

                void do_something()
                {
                    while(true)
                    {
                        syslog (LOG_NOTICE, "Writing to my Syslog");

                        sleep(5);
                    }
                }
            };

            int main(int argc, char *argv[]) {
                logger l;

                l.run();

                return 0;
            }

            ////////////////////////////////////////////////////////////////////////////
            //   .h

            #ifndef DAEMON_H
            #define DAEMON_H

            #include <string>

            class daemon{
            public:
                const std::string name;
            public:
                daemon(const char* nm);

                void run();

            private:
                static void fork_off_parent();
                static void set_child_context();
                virtual void open_log();
                virtual void do_something() =0;
                virtual void finish();
            private:
                daemon(const daemon&);
                daemon& operator=(const daemon&);
            };

            #endif // DAEMON_H

            /////////////////////////////////////////////////////////////////////////////////
            //  .cpp
            /*
             * cp from  
            http://shahmirj.com/blog/beginners-guide-to-creating-a-daemon-in-linux
             
            */

            #include "daemon.h"

            #include <sys/stat.h>
            #include <stdlib.h>
            #include <unistd.h>
            #include <syslog.h>

            daemon::daemon(const char *nm) : name(nm)
            {}

            void daemon::run()
            {
                fork_off_parent();
                set_child_context();
                open_log();
                do_something();
                finish();
            }

            void daemon::fork_off_parent()
            {
                //Fork the Parent Process
                pid_t pid = fork();

                if (pid < 0)
                {
                    exit(EXIT_FAILURE);
                }

                //We got a good pid, Close the Parent Process
                if (pid > 0)
                {
                    exit(EXIT_SUCCESS);
                }
            }

            void daemon::set_child_context()
            {
                //Change File Mask
                umask(0);

                //Create a new Signature Id for our child
                pid_t sid = setsid();
                if (sid < 0)
                {
                    exit(EXIT_FAILURE);
                }

                //Change Directory
                
            //If we cant find the directory we exit with failure.
                if ((chdir("/")) < 0)
                {
                    exit(EXIT_FAILURE);
                }

                //Close Standard File Descriptors
                close(STDIN_FILENO);
                close(STDOUT_FILENO);
                close(STDERR_FILENO);
            }

            void daemon::open_log()
            {
                //Set our Logging Mask and open the Log
                setlogmask(LOG_UPTO(LOG_NOTICE));
                openlog(name.c_str(), LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);

                syslog(LOG_INFO, "Entering Daemon");
            }

            void daemon::finish()
            {
                //Close the log
                closelog ();
            }

            posted on 2013-05-14 14:01 山城,山 閱讀(324) 評論(0)  編輯 收藏 引用

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品久久久久久| 久久精品国产免费一区| 久久国内免费视频| 中文字幕久久波多野结衣av| 国产精品久久久久久五月尺| 综合久久国产九一剧情麻豆| 精品免费久久久久久久| 9999国产精品欧美久久久久久| 国产成人久久久精品二区三区| 久久影院久久香蕉国产线看观看| 久久婷婷色综合一区二区| 国产精品久久久久久久久免费| 精品久久久久久无码人妻热| 无码精品久久久天天影视| 99久久夜色精品国产网站| 亚洲国产精品无码久久| 久久99久久无码毛片一区二区| 色综合久久无码中文字幕| 色婷婷噜噜久久国产精品12p| 99久久精品影院老鸭窝| 无码国内精品久久综合88| 久久久久亚洲爆乳少妇无| 国内精品久久久久| 久久精品无码午夜福利理论片 | 日韩一区二区三区视频久久| 99久久久精品免费观看国产| 囯产极品美女高潮无套久久久| 久久久精品久久久久久| 国产香蕉97碰碰久久人人| 久久久国产精品网站| 国产成人久久精品一区二区三区| 中文字幕无码久久人妻| 亚洲欧美一级久久精品| 狠狠人妻久久久久久综合蜜桃| 伊人色综合久久天天| 久久精品国产91久久麻豆自制 | 99久久亚洲综合精品网站| 国产99精品久久| 天天久久狠狠色综合| 久久精品国产福利国产琪琪| 国产精品99久久不卡|