• <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 山城,山 閱讀(323) 評論(0)  編輯 收藏 引用

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日日狠狠久久偷偷色综合0| 久久国产精品久久国产精品| 久久亚洲高清综合| 久久99久久99精品免视看动漫| 97久久婷婷五月综合色d啪蜜芽| 蜜臀av性久久久久蜜臀aⅴ| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久天天躁狠狠躁夜夜2020| 偷窥少妇久久久久久久久| 久久国产色AV免费看| 四虎国产精品成人免费久久| 久久AV高清无码| 亚洲伊人久久综合影院| 久久国产免费观看精品| 中文国产成人精品久久不卡| 成人精品一区二区久久| 久久精品国产亚洲AV高清热| 一本久久a久久精品综合香蕉| 国产午夜久久影院| 欧洲人妻丰满av无码久久不卡| 青青草国产97免久久费观看| 国产ww久久久久久久久久| 久久99热只有频精品8| 2021久久精品免费观看| 久久精品综合一区二区三区| 99久久免费国产特黄| 亚洲综合日韩久久成人AV| 久久午夜夜伦鲁鲁片免费无码影视 | 国内精品伊人久久久久影院对白| 亚洲午夜久久久久久噜噜噜| 亚洲国产精品成人久久蜜臀 | 中文字幕精品无码久久久久久3D日动漫 | 久久久噜噜噜久久中文字幕色伊伊| 久久九九精品99国产精品| 色欲综合久久躁天天躁蜜桃| 久久国产色av免费看| 777午夜精品久久av蜜臀| 亚洲国产另类久久久精品小说| 久久人人爽人人人人片av| 久久久久久免费视频| 久久夜色精品国产噜噜亚洲a|