IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

faire un daemon ?


Sujet :

C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    412
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2006
    Messages : 412
    Par défaut faire un daemon ?
    Hello
    J'essaye de mettre faire un daemon sous linux, faute de doc voila ce que j'ai entrepris.
    doc trouvée Mai en C
    http://0pointer.de/lennart/projects/...tml/index.html

    je tente donc d'en faire une class.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    fichier *.h
    #ifndef DEF_ME_DEMON
    #define DEF_ME_DEMON
     
     
    class c_deamon
    {
       public:
          c_deamon();
          void start_in_daemon();
    };
    #endif //fin des macro
    fichier *.cpp

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
     
    /***
      This file is part of libdaemon.
     
      Copyright 2003-2008 Lennart Poettering
     
      libdaemon is free software; you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as
      published by the Free Software Foundation, either version 2.1 of the
      License, or (at your option) any later version.
     
      libdaemon is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      Lesser General Public License for more details.
     
      You should have received a copy of the GNU Lesser General Public
      License along with libdaemon. If not, see
      <http://www.gnu.org/licenses/>.
    ***/
     
    #include <signal.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/unistd.h>
    #include <sys/select.h>
     
    #include <libdaemon/dfork.h>
    #include <libdaemon/dsignal.h>
    #include <libdaemon/dlog.h>
    #include <libdaemon/dpid.h>
    #include <libdaemon/dexec.h>
    #include "c_daemon.h"
    c_deamon::c_deamon(){}
     
    void c_deamon::start_in_daemon()
    {
       pid_t pid;
     
        /* Reset signal handlers */
        if (daemon_reset_sigs(-1) < 0) {
            daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno));
            return ;
        }
     
        /* Unblock signals */
        if (daemon_unblock_sigs(-1) < 0) {
            daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno));
            return ;
        }
     
    int argc;
    char *argv[255];
        /* Set indetification string for the daemon for both syslog and PID file */
        daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
     
        /* Check if we are called with -k parameter */
        if (argc >= 2 && !strcmp(argv[1], "-k")) {
            int ret;
     
            /* Kill daemon with SIGTERM */
     
            /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */
            if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0)
                daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
     
            return ;//ret < 0 ? 1 : 0;
        }
     
        /* Check that the daemon is not rung twice a the same time */
        if ((pid = daemon_pid_file_is_running()) >= 0) {
            daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid);
            return ;
        }
     
        /* Prepare for return value passing from the initialization procedure of the daemon process */
        if (daemon_retval_init() < 0) {
            daemon_log(LOG_ERR, "Failed to create pipe.");
            return ;
        }
     
        /* Do the fork */
        if ((pid = daemon_fork()) < 0) {
     
            /* Exit on error */
            daemon_retval_done();
            return ;
     
        } else if (pid) { /* The parent */
            int ret;
     
            /* Wait for 20 seconds for the return value passed from the daemon process */
            if ((ret = daemon_retval_wait(20)) < 0) {
                daemon_log(LOG_ERR, "Could not recieve return value from daemon process: %s", strerror(errno));
                return ;
            }
     
            daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret);
            return ;//ret;
     
        } else { /* The daemon */
            int fd, quit = 0;
            fd_set fds;
     
            /* Close FDs */
            if (daemon_close_all(-1) < 0) {
                daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno));
     
                /* Send the error condition to the parent process */
                daemon_retval_send(1);
                goto finish;
            }
     
            /* Create the PID file */
            if (daemon_pid_file_create() < 0) {
                daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno));
                daemon_retval_send(2);
                goto finish;
            }
     
            /* Initialize signal handling */
            if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, 0) < 0) {
                daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno));
                daemon_retval_send(3);
                goto finish;
            }
     
            /*... do some further init work here */
     
     
            /* Send OK to parent process */
            daemon_retval_send(0);
     
            daemon_log(LOG_INFO, "Sucessfully started");
     
            /* Prepare for select() on the signal fd */
            FD_ZERO(&fds);
            fd = daemon_signal_fd();
            FD_SET(fd, &fds);
     
            while (!quit) {
                fd_set fds2 = fds;
     
                /* Wait for an incoming signal */
                if (select(FD_SETSIZE, &fds2, 0, 0, 0) < 0) {
     
                    /* If we've been interrupted by an incoming signal, continue */
                    if (errno == EINTR)
                        continue;
     
                    daemon_log(LOG_ERR, "select(): %s", strerror(errno));
                    break;
                }
     
                /* Check if a signal has been recieved */
                if (FD_ISSET(fd, &fds2)) {
                    int sig;
     
                    /* Get signal */
                    if ((sig = daemon_signal_next()) <= 0) {
                        daemon_log(LOG_ERR, "daemon_signal_next() failed: %s", strerror(errno));
                        break;
                    }
     
                    /* Dispatch signal */
                    switch (sig) {
     
                        case SIGINT:
                        case SIGQUIT:
                        case SIGTERM:
                            daemon_log(LOG_WARNING, "Got SIGINT, SIGQUIT or SIGTERM.");
                            quit = 1;
                            break;
     
                        case SIGHUP:
                            daemon_log(LOG_INFO, "Got a HUP");
                            daemon_exec("/", NULL, "/bin/ls", "ls", (char*) NULL);
                            break;
     
                    }
                }
            }
     
            /* Do a cleanup */
    finish:
            daemon_log(LOG_INFO, "Exiting...");
            daemon_retval_send(255);
            daemon_signal_done();
            daemon_pid_file_remove();
     
            return ;
        }
    }
    /*
    int main(int argc, char *argv[]) {
     
    }
     
    */
    le message d'erreur est le suivant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
    main.o: In function `c_deamon::start_in_daemon()':
    main.cpp:(.text+0x4c6): undefined reference to `daemon_reset_sigs'
    main.cpp:(.text+0x4f4): undefined reference to `daemon_log'
    main.cpp:(.text+0x505): undefined reference to `daemon_unblock_sigs'
    main.cpp:(.text+0x533): undefined reference to `daemon_log'
    main.cpp:(.text+0x546): undefined reference to `daemon_ident_from_argv0'
    main.cpp:(.text+0x54b): undefined reference to `daemon_log_ident'
    main.cpp:(.text+0x550): undefined reference to `daemon_log_ident'
    main.cpp:(.text+0x555): undefined reference to `daemon_pid_file_ident'
    main.cpp:(.text+0x589): undefined reference to `daemon_pid_file_kill_wait'
    main.cpp:(.text+0x5c1): undefined reference to `daemon_log'
    main.cpp:(.text+0x5cb): undefined reference to `daemon_pid_file_is_running'
    main.cpp:(.text+0x5f5): undefined reference to `daemon_log'
    main.cpp:(.text+0x5ff): undefined reference to `daemon_retval_init'
    main.cpp:(.text+0x61a): undefined reference to `daemon_log'
    main.cpp:(.text+0x624): undefined reference to `daemon_fork'
    main.cpp:(.text+0x636): undefined reference to `daemon_retval_done'
    main.cpp:(.text+0x651): undefined reference to `daemon_retval_wait'
    main.cpp:(.text+0x685): undefined reference to `daemon_log'
    main.cpp:(.text+0x6c3): undefined reference to `daemon_log'
    main.cpp:(.text+0x6db): undefined reference to `daemon_close_all'
    main.cpp:(.text+0x709): undefined reference to `daemon_log'
    main.cpp:(.text+0x715): undefined reference to `daemon_retval_send'
    main.cpp:(.text+0x71f): undefined reference to `daemon_pid_file_create'
    main.cpp:(.text+0x74d): undefined reference to `daemon_log'
    main.cpp:(.text+0x759): undefined reference to `daemon_retval_send'
    main.cpp:(.text+0x78a): undefined reference to `daemon_signal_init'
    main.cpp:(.text+0x7b8): undefined reference to `daemon_log'
    main.cpp:(.text+0x7c4): undefined reference to `daemon_retval_send'
    main.cpp:(.text+0x7d5): undefined reference to `daemon_retval_send'
    main.cpp:(.text+0x7e9): undefined reference to `daemon_log'
    main.cpp:(.text+0x80d): undefined reference to `daemon_signal_fd'
    main.cpp:(.text+0x8cd): undefined reference to `daemon_log'
    main.cpp:(.text+0x8fd): undefined reference to `daemon_signal_next'
    main.cpp:(.text+0x932): undefined reference to `daemon_log'
    main.cpp:(.text+0x97a): undefined reference to `daemon_log'
    main.cpp:(.text+0x997): undefined reference to `daemon_log'
    main.cpp:(.text+0x9c3): undefined reference to `daemon_exec'
    main.cpp:(.text+0x9e1): undefined reference to `daemon_log'
    main.cpp:(.text+0x9ed):undefined reference  to `daemon_retval_send'
    main.cpp:(.text+0x9f2): undefined reference to `daemon_signal_done'
    main.cpp:(.text+0x9f7): undefined reference to `daemon_pid_file_remove'
    collect2: ld returned 1 exit status
    j'ai simplement enlever les return x; pour faire une methode qui ne renvoie rien . et adapter 2 variable qui son celle du main
    int argc et char *argv[]

    normalement les erreur undefined reference son déclarée dans la lib (enfin je supose)
    si vous avez une idée je suis preneur

    visiblement il arrive pas linker les *.o mai dans ce cas que cherche t-on ?
    j'essaye de trouver de la doc sur l'appelle system deamon()
    mai rien pour le moment

    compiler avec g++
    Merci d'avance

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    412
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2006
    Messages : 412
    Par défaut
    Plop on ma soufler la réponse sur irc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    g++ main.cpp -Wall -Wextra -ansi -pedantic -ldaemon -o main
    il faut linker avec -ldaemon

    Merci systemfault

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [transmission-daemon] comment fair epour savoir si les torrents sont morts
    Par mapmip dans le forum Logiciels Libres & Open Source
    Réponses: 0
    Dernier message: 13/05/2015, 17h55
  2. Faire un daemon
    Par olivier857 dans le forum Administration système
    Réponses: 27
    Dernier message: 12/07/2006, 15h09
  3. faire un selection dans une image aves les APIs
    Par merahyazid dans le forum C++Builder
    Réponses: 3
    Dernier message: 30/04/2002, 10h44
  4. Comment faire pour créer un bitmap
    Par GliGli dans le forum C++Builder
    Réponses: 2
    Dernier message: 24/04/2002, 15h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo