Forking from Gtk



Hello,

I have a small demo app. This works on FreeBSD but I can't
get to work on Linux. I know that in Linux setsid will fail
if the child has the same session_id as the parent. So on
Linux you must fork twice. But it also seems that the parent
must do an exit. And I don't want that. The code is not very
long - so I include it here.

---<snipp>---
//----------------------------------------------------------------------
//
//  $Id: GtkFork.c,v 1.2 2008/07/07 20:29:17 gorhas Exp $
//
//  Experiment to run a thing in background
//  This works on FreeBSD but not on Linux...
//
//  Build with
//
//  CFLAGS := `pkg-config glib-2.0 --cflags` `pkg-config gtk+-2.0 
--cflags`
//  LDFLAGS := `pkg-config glib-2.0 --libs` `pkg-config gtk+-2.0 --libs`
//
//  cc $(CFLAGS) -o GtkFork GtkFork.c $(LDFLAGS)
//
//----------------------------------------------------------------------

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

//----------------------------------------------------------------------
// run_btn_callback
//
// Try to run something in the background
//
//----------------------------------------------------------------------
static void run_btn_callback (GtkWidget *button, gpointer data)
{

   int loops_to_run = 0;
   int i = 0;
   int pid = -1;
   int ret = -1;

   // Skriv ut innehållet på skärmen 
   printf("Clicked..\n");
   printf("Data was: %s\n", gtk_entry_get_text( data ));

   loops_to_run = atoi( gtk_entry_get_text(data));

   // We dont want to wait very long...
   if( loops_to_run > 60 )
   {
      loops_to_run = 60;
      printf("Adjusting to 60 loops...\n");
   }
   printf("Loops to run: %d\n", loops_to_run );



   printf("We make a daemon\n");
   if ( ( pid = fork() ) < 0 )
   {
      // Something went wrong
      printf("We could not fork.... just exit");
      exit(-1);
   }
   else if ( pid != 0 )
   {
      
      // This is the parent process
      printf("The background process have pid:  %d\n", pid);
      return;
   }

   // Quit gtk
   gtk_main_quit();

   // Become session leader
   ret = setsid();
   if( ret == -1 )
   {
      perror("We could not be session leader\n");
      exit(-1);
   }

   // Set umask for safety
   umask(0);
   
   // Set root dir
   chdir("/");
  
 
   for( i = 0; i < loops_to_run; i++ )
   {
      printf("We are running: %d\n", i );
      sleep(1);
   } 

   exit(0);

}

//----------------------------------------------------------------------
// When we quit
//----------------------------------------------------------------------
static void quit_callback()
{
   gtk_main_quit ();
}


//----------------------------------------------------------------------
//  main
//
//  Creates a gtk windows to specify how many loops
//  the daemon should run.
//
//----------------------------------------------------------------------
int 
main (int argc, char **argv)
{

  GtkWidget *mainwin = 0L;
  GtkWidget *number_entry = 0L;
  GtkWidget *run_btn = 0L;
  GtkWidget *vbox = 0L;

  /* Initialize i18n support */
  printf("Locale is: %s\n", gtk_set_locale () );

  /* Initialize the widget set */
  gtk_init (&argc, &argv);

  /* Create the main window */
  mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  /* Set up our GUI elements */
  vbox = gtk_vbox_new (FALSE, 0);

  number_entry = gtk_entry_new();

  run_btn = gtk_button_new_with_label("Just run");

  gtk_container_add (GTK_CONTAINER (mainwin), vbox);
  gtk_box_pack_start (GTK_BOX (vbox), number_entry, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), run_btn, TRUE, TRUE, 0);

  
  // Function to call when main window is destroyed
  g_signal_connect (G_OBJECT (mainwin),
                    "destroy",
                    GTK_SIGNAL_FUNC (quit_callback),
                    NULL);

  // Function to call when we click the button
  g_signal_connect(GTK_OBJECT(run_btn), "clicked",
                     G_CALLBACK(run_btn_callback),
                     number_entry);

  /* Show the application window */
  gtk_widget_show_all (mainwin);

  /* Enter the main event loop, and wait for user interaction */
  gtk_main ();

  /* The user lost interest */
  return 0;

}

//------------------------------------------------------------------
// END
//------------------------------------------------------------------

---<snipp>---

-- 
Göran Hasse

----------------------------------------------------------------
Göran Hasse           email: gorhas raditex se  Tel: 019-450105
Raditex AB             http://www.raditex.se    
Planiavägen 15, 1tr                             Mob: 070-5530148
131 34  NACKA, SWEDEN                         OrgNr: 556240-0589
VAT: SE556240058901
------------------------------------------------------------------




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]