Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "/usr/include/postgresql/libpq-fe.h"
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7.  
  8. int main()
  9. {
  10.         char       * conntr = "host='83.168.204.143' dbname='travelfast' user='postgres' password='negergreger'";       /* specify the login settings */
  11.         char        query_string[256];                                                                                  /* Holds constructed SQL query */
  12.         char        modify_string[256];                                                                                 /* Holds constructed SQL query */
  13.         char        modify_first_string[256];                                                                           /* Holds constructed SQL query */
  14.         int         i;                                                                                                  /* Holds the for loop variable */
  15.         int        pid;                                                                                                 /* Holds the pid */
  16.         PGconn     *conn;                                                                                               /* Holds database connection */
  17.         PGresult   *res;                                                                                                /* Holds query result */
  18.         PGresult   *mod;                                                                                                /* Holds query result */
  19.         PGresult   *work;                                                                                               /* Holds query result */
  20.  
  21. // Create the daemon
  22.  
  23.         if(fork()) return 0;
  24.         pid = fork();
  25.  
  26.  
  27.         if(pid)
  28.         {
  29.                 return 0;
  30.         }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /* DB connection */
  38.  
  39.         conn = PQconnectdb(conntr);                                                                              
  40.         if (PQstatus(conn) == CONNECTION_BAD)                                                                           /* did the connection fail? */
  41.         {
  42.                 fprintf(stderr, "Connection to database failed.\n");
  43.                 fprintf(stderr, "%s", PQerrorMessage(conn));
  44.                 exit(1);                                                                                                /* Then exit program */
  45.         }
  46.  
  47.  
  48. /* loop while the PID is running */
  49.  
  50.         while(1)
  51.         {
  52.  
  53.  
  54.  
  55.  
  56.  
  57.                                                                 /* === The SQL Query === */
  58.  
  59.                         /*  Modify first, "assign these DB-lines to me" */
  60.         sprintf(modify_first_string, "UPDATE foo set server_id = '1' WHERE number IN (SELECT number from foo where working_on = '0' and server_id = '0')");
  61.         mod = PQexec(conn, modify_first_string);
  62.  
  63.                 if (PQcmdTuples(mod) != 0)                                                                              /* if any row were modified, proceed with the program*/
  64.                 {
  65.                         PQclear(mod);
  66.                         /*  Select those values */
  67.                         sprintf(query_string, "SELECT * FROM foo WHERE server_id = '1' AND working_on = '0'");          /* Create an SELECT query */
  68.                         res = PQexec(conn, query_string);                                                               /* send the query */
  69.  
  70.                                 if (PQresultStatus(res) != PGRES_TUPLES_OK)                                             /* did the query fail? */
  71.                                 {
  72.                                         fprintf(stderr, "SELECT query failed.\n");
  73.                                         PQclear(res);
  74.                                         PQfinish(conn);
  75.                                         exit(1);
  76.                                 }
  77.  
  78.                                 if (PQntuples(res) > 0)
  79.                                 {
  80.                                         for (i = 0; i < PQntuples(res); i++)                                                            /* loop through all rows returned */
  81.                                         sprintf(modify_string, "UPDATE foo SET working_on = '1' WHERE server_id = '1' AND number = '%s'", PQgetvalue(res, i, 0));
  82.                                         PQclear(res);                                                                                   /* free res for the select-query earlier */
  83.                                         work = PQexec(conn, modify_string);
  84.                                         PQclear(work);                                                                                  /* free result for the modify string */
  85.                                 }
  86.  
  87.                                 else
  88.                                 {
  89.                                         sleep(1);
  90.                                 }
  91.                 }
  92.  
  93.                 else
  94.                 {
  95.                         printf("sleep 1sek\n");
  96.                         sleep(1);
  97.                 }
  98. /* End of the while-loop */
  99.      }
  100.  
  101.  
  102. /* Close the connection */
  103.         PQfinish(conn);                                                                                                 /* disconnect from the database */
  104.  
  105.  
  106.         /* Exit the program */
  107.         return 0;
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement