Пример #1
0
static int
g2ipmsg_main (int argc, char *argv[])
{

#ifdef ENABLE_NLS
  bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (PACKAGE, "UTF-8");
  textdomain (PACKAGE);
#endif

  gnome_program_init (PACKAGE, VERSION, LIBGNOMEUI_MODULE,
                      argc, argv,
		      GNOME_PROGRAM_STANDARD_PROPERTIES,
                      GNOME_PARAM_APP_DATADIR, PACKAGE_DATA_DIR,
                      NULL);
  if (create_lock_file()) {
    exit(1); /* Can not lock */
  }

  hostinfo_init_hostinfo();
  ui_thread=g_thread_create(ipmsg_ui_thread,
				    NULL,
				    TRUE,
				    NULL);
  tcp_thread=g_thread_create(ipmsg_tcp_server_thread,
			     (gpointer)hostinfo_get_ipmsg_system_addr_family(),
			     FALSE,
			     NULL);
  g_thread_join (ui_thread);

  release_lock_file();

  return 0;
}
Пример #2
0
/*
** This ties it all together.  It is called from main().
** Since the last thing it does is call get_connexion(),
** the parent never returns but the child does numberous times.
*/
void run_standalone(int server_port)
	{
	int fd;

	/*
	** Duplicate this process and close the origional one
	** so that the shell will stop waiting.  Also, disassociate
	** from the controlling terminal and close all file descriptors.
	*/
	gu_daemon(PPR_UMASK);

	/* By convention, PPR processes run in the PPR home
	   directory. */
	chdir(HOMEDIR);

	DODEBUG_MAIN(("entering standalone mode"));
	create_lock_file();

	DODEBUG_MAIN(("starting server on port %d", server_port));
	fd = bind_server(server_port);

	signal_restarting(SIGCHLD, reapchild);

	standalone_main_loop(fd);
	} /* run_standalone() */
Пример #3
0
int main()
{
    daemon();
    create_lock_file();
    while(1)
        usleep(100);
    return 0;
}
Пример #4
0
static gboolean
choose_xdisplay (MetaXWaylandManager *manager)
{
  int display = 0;
  char *lock_file = NULL;

  /* Hack to keep the unused Xwayland instance on
   * the login screen from taking the prime :0 display
   * number.
   */
  if (g_getenv ("RUNNING_UNDER_GDM") != NULL)
    display = 1024;

  do
    {
      lock_file = create_lock_file (display, &display);
      if (!lock_file)
        {
          g_warning ("Failed to create an X lock file");
          return FALSE;
        }

      manager->abstract_fd = bind_to_abstract_socket (display);
      if (manager->abstract_fd < 0)
        {
          unlink (lock_file);

          if (errno == EADDRINUSE)
            {
              display++;
              continue;
            }
          else
            return FALSE;
        }

      manager->unix_fd = bind_to_unix_socket (display);
      if (manager->abstract_fd < 0)
        {
          unlink (lock_file);
          close (manager->abstract_fd);
          return FALSE;
        }

      break;
    }
  while (1);

  manager->display_index = display;
  manager->display_name = g_strdup_printf (":%d", manager->display_index);
  manager->lock_file = lock_file;

  return TRUE;
}
Пример #5
0
/*========================================================================
** The Main Procedure
** Initialization and FIFO command dispatch routine.
========================================================================*/
static int real_main(int argc, char *argv[])
	{
	const char function[] = "real_main";
	int option_foreground = FALSE;
	int FIFO;					/* First-in-first-out which feeds us requests */
	sigset_t lock_set;
	struct timeval next_tick;

	/* Initialize internation messages library. */
	#ifdef INTERNATIONAL
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE_PPRD, LOCALEDIR);
	textdomain(PACKAGE_PPRD);
	#endif

	/*
	** Set some environment variables, (PATH, IFS, and
	** SHELL) for safety and for the convenience of the
	** programs we launch (HOME, and PPR_VERSION).
	** Remove unnecessary and potentially misleading
	** variables.
	*/
	set_ppr_env();
	prune_env();

	parse_command_line(argc, argv, &option_foreground);

	/* Switch all UIDs to USER_PPR, all GIDS to GROUP_PPR. */
	adjust_ids();

	/* If the --forground switch wasn't used, then dropt into background. */
	if(! option_foreground)
		gu_daemon(PPR_PPRD_UMASK);
	else
		umask(PPR_PPRD_UMASK);

	/* Change the home directory to the PPR home directory: */
	chdir(HOMEDIR);

	/* Create /var/spool/ppr/pprd.pid. */
	create_lock_file();

	/* Signal handlers for silly stuff. */
	signal_restarting(SIGPIPE, signal_ignore);
	signal_restarting(SIGHUP, signal_ignore);

	/* Signal handler for shutdown request. */
	signal_interupting(SIGTERM, sigterm_handler);

	/* Arrange for child termination to be noted. */
	signal_restarting(SIGCHLD, sigchld_handler);

	/* Move /var/spool/ppr/logs/pprd to pprd.old before we call debug()
	   for the first time (below). */
	rename_old_log_file();

	/*
	** This code must come after adjust_ids() and gu_daemon().
	** It makes the first log entry and tells queue-display
	** programs that we are starting up.
	*/
	debug("PPRD startup, pid=%ld", (long)getpid());
	state_update("STARTUP");

	/* Make sure the local node gets the node id of 0. */
	if(! nodeid_is_local_node(nodeid_assign(ppr_get_nodename())))
		fatal(1, "%s(): line %d: assertion failed", function, __LINE__);

	/* Initialize other subsystems. */
	question_init();

	/* Load the printers database. */
	DODEBUG_STARTUP(("loading printers database"));
	load_printers();

	/* Load the groups database. */
	DODEBUG_STARTUP(("loading groups database"));
	load_groups();

	/* Set up the FIFO. */
	DODEBUG_STARTUP(("opening FIFO"));
	FIFO = open_fifo();

	/* Initialize the queue.  This is likely to start printers. */
	DODEBUG_STARTUP(("initializing the queue"));
	initialize_queue();

	/* Schedule the first timer tick. */
	gettimeofday(&next_tick, NULL);
	next_tick.tv_sec += TICK_INTERVAL;

	/*
	** Create a signal block set which will be used to block SIGCHLD except
	** when we are calling select().
	*/
	sigemptyset(&lock_set);
	sigaddset(&lock_set, SIGCHLD);
	sigprocmask(SIG_BLOCK, &lock_set, (sigset_t*)NULL);

	/*
	** This is the Main Loop.  It runs until the sigterm_handler
	** sets sigterm_received.
	*/
	while(!sigterm_received)
		{
		int readyfds;					/* return value from select() */
		fd_set rfds;					/* list of file descriptors for select() to watch */
		struct timeval time_now;		/* the current time */

		DODEBUG_MAINLOOP(("top of main loop"));

		gettimeofday(&time_now, NULL);

		/* If it is time for or past time for the next tick, */
		if(gu_timeval_cmp(&time_now, &next_tick) >= 0)
			{
			readyfds = 0;
			}

		/* If it is not time for the next tick yet, */
		else
			{
			/* Set the select() timeout so that it will return in time for the
			   next tick(). */
			gu_timeval_cpy(&select_tv, &next_tick);
			gu_timeval_sub(&select_tv, &time_now);

			/* Create a file descriptor list which contains only the descriptor
			   of the FIFO. */
			FD_ZERO(&rfds);
			FD_SET(FIFO, &rfds);

			/* Call select() with SIGCHLD unblocked. */
			sigprocmask(SIG_UNBLOCK, &lock_set, (sigset_t*)NULL);
			readyfds = select(FIFO + 1, &rfds, NULL, NULL, &select_tv);
			sigprocmask(SIG_BLOCK, &lock_set, (sigset_t*)NULL);
			}

		/* If there is something to read, */
		if(readyfds > 0)
			{
			if(!FD_ISSET(FIFO, &rfds))
				fatal(0, "%s(): assertion failed: select() returned but FIFO not ready", function);
			do_command(FIFO);
			continue;
			}

		/* If the SIGCHLD handler set the flag, handle child termination.  Once
		   we have done that, we must go back to the top of the loop because
		   we don't really know if it is time for a tick() call yet. */
		if(sigchld_caught)
			{
			sigchld_caught = FALSE;
			reapchild();
			continue;
			}

		/* If there was no error and no file descriptors are ready, then the 
		   timeout must have expired.  Call tick(). */
		if(readyfds == 0)
			{
			tick();
			next_tick.tv_sec += TICK_INTERVAL;
			continue;
			}

		/* If interupted by a system call, restart it. */
		if(errno == EINTR)
			continue;

		/* If we get this far, there was an error. */
		fatal(0, "%s(): select() failed, errno=%d (%s)", function, errno, gu_strerror(errno));
		} /* end of endless while() loop */

	state_update("SHUTDOWN");
	fatal(0, "Received SIGTERM, exiting");
	} /* end of real_main() */
Пример #6
0
/*---------------------------------------------------------------------+
|                               main                                   |
| ==================================================================== |
|                                                                      |
| Function:  Main program  (see prolog for more details)               |
|                                                                      |
| Returns:   (0)  Successful completion                                |
|            (-1) Error occurred                                       |
|                                                                      |
+---------------------------------------------------------------------*/
int main (int argc, char **argv)
{
	char	*shmptr,	/* Shared memory segment address */
		value = 0;	/* Value written into shared memory segment */
	int	i;
	char 	*ptr;
	int	status;
	int	shmem_size;
	unsigned long cksum;

	lockfd = create_lock_file (LOCK_FILE);
	setup_signal_handlers ();

	/*
	 * Parse command line arguments and print out program header
	 */
	parse_args (argc, argv);
	printf ("%s: IPC Shared Memory TestSuite program\n", *argv);

	parent_pid = getpid ();
	for (i=0; i<num_children; i++)
		pid [i] = (pid_t)0;

	/*
	 * Get chunk of shared memory for storing num_children checksum
	 */
	shmem_size = sizeof (unsigned long);
	if ((long)(checksum = (unsigned long *) 
		mmap (0, shmem_size, PROT_READ | PROT_WRITE, 
		      MAP_ANON | MAP_SHARED, -1, 0)) < 0)
		sys_error ("mmap failed", __LINE__);

	for (i=0; i < num_children; i++)
		*(checksum + (sizeof (unsigned long) * i)) = 0;

	/*
	 * Get chunk of memory for writing scratch data
	 */
	printf ("\n\tGet shared memory segment (%d bytes)\n", buffer_size);
	if ((shmptr = mmap (0, buffer_size, PROT_READ | PROT_WRITE, 
		MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
		sys_error ("mmap failed", __LINE__);

	/*
	 * Parent:
	 * 
	 * Fill buffer with data..
	 */
	cksum = value = 0;

	printf ("\n\tParent: calculate shared memory segment checksum\n");
	write_lock (lockfd);
	for (ptr=shmptr; ptr < (shmptr+buffer_size); ptr++) {
		*ptr = value++;
		cksum += *ptr;
	}
	unlock_file (lockfd);
	printf ("\t        shared memory checksum %08lx\n", cksum);

	printf ("\n\tSpawning %d child processes ... \n", num_children);
	for (i=0; i<num_children; i++) {

		if ((pid [i] = fork()) == (pid_t)0) {
			child (i, shmptr);
			exit (0);
		} else if (pid [i] < (pid_t)0)
			sys_error ("fork failed", __LINE__);
	}

	/*
	 * Wait for child processes to compute checksum and complete...
	 */
	for (i=0; i<num_children; i++) {
		waitpid (pid [i], &status, 0);

		if (!WIFEXITED (status)) 
			sys_error ("child process terminated abnormally", 
				__LINE__);
		if (cksum != *(checksum + (sizeof (unsigned long) * i))) {
			printf ("checksum [%d] = %08lx\n", i, checksum [i]);
			error ("checksums do not match", __LINE__); 
		}
	}
	printf ("\n\tParent: children calculated segment successfully\n");
	/* 
	 * Program completed successfully -- exit
	 */
	printf ("\nsuccessful!\n");

	return (0);
}
Пример #7
0
int
main(int argc, char **argv)
{
	char	*data;
	char	*key;
	char	*bufp;
	char	save_c;
	long	type;
	int	linenum;
	char	*cache = NULL;
	char	*cross_cache = NULL;
	FILE	*logfp = NULL;
	FILE	*infp = stdin;
	char	*file = NULL;
	int	first_xref = TRUE;
	char	*db_prefix = NULL;
	char	*sn_host = NULL;
	char	*sn_pid = NULL;
	char	*omode = NULL;
	char	lock_file[MAXPATHLEN];
	int	set_sgns = FALSE;
	LongString	buf;
	char *macro_file[MAX_MACRO_FILES];
	int macro_file_num = 0;

	LongStringInit(&buf,0);
	Tcl_FindExecutable(argv[0]);

#if !WIN32 && TTY_TRACE
	tty = fopen("/dev/tty","w");
#endif /* !WIN32 && TTY_TRACE */

	while((type = getopt(argc,argv,"c:C:lf:H:O:P:M:sFm:")) != EOF)
	{
		switch (type)
		{
		case 'c':
			cache = optarg;
			break;

		case 'C':
			cross_cache = optarg;
			break;

		case 'l':
			report_local_vars = TRUE;
			break;

		case 'f':
			file = optarg;
			break;

		case 'H':
			sn_host = optarg;
			break;

		case 'P':
			sn_pid = optarg;
			break;

		case 'M':
			omode = optarg;
			break;

		case 's':
			set_sgns = FALSE;
			break;

		case 'F':
			break;

		case 'm':
			if (macro_file_num < MAX_MACRO_FILES - 1)
			{
				macro_file[macro_file_num++] = optarg;
			}
			MacroReadFile(optarg);
			break;
		}
	}

	if (optind < argc)
	{
		db_prefix = argv[optind];
	}

	if (!db_prefix)
	{
		printf("Usage: %s ?-c cache_size? ?-C cross_cache_size? ?-l? ?-f file? ?-m macrofile? db_prefix\n",
			argv[0]);

		exit(2);
	}

	if (set_sgns)
		set_signals();

	killed = FALSE;

	if (sn_host && sn_pid)
	{
		sprintf(lock_file,"%s.lck",db_prefix);

		switch (create_lock_file(lock_file,sn_host,sn_pid))
		{
		case -1:
			fprintf(stderr,"Could not create lock,%s\n",strerror(errno));
			exit(2);
			break;

		case FALSE:
			fprintf(stderr,"The database is in an inconsistent state.\n");
			exit(2);
			break;
		}
	}
	else
		lock_file[0] = '\0';

	if (file)
	{
		FILE	*in;

		in = fopen(file,"r");
		if (in)
			infp = in;
	}

	/*SN_DBIMP is set interactive in the tcl init procedure!*/
	if (getenv("SN_DBIMP"))
	{
		char    tmp[MAXPATHLEN];
		char    *e;

		e = getenv("TMPDIR");

		sprintf(tmp,"%s/dbimp_%lu.tmp",
			e ? e : "/tmp",
			(unsigned long)getpid());

		logfp = fopen(tmp,"w+");
		if (logfp)
		{
			chmod(tmp,0666);
			fprintf(logfp,"#dbimp (pid: %lu) started database\n#prefix: <%s> cache: %s ",
				(unsigned long)getpid(),
				db_prefix,
				cache ? cache : "#");
			fprintf(logfp,"cross_cache: %s local_vars: %d file: <%s>\n",
				cross_cache ? cross_cache : "#",
				report_local_vars,
				file ? file : "stdin");
			if (macro_file_num)
			{
				int cou;

				for (cou = 0; cou < macro_file_num; cou++)
				{
					fprintf(logfp,"macro file %d: <%s>\n",
						cou + 1,
						macro_file[cou]);
				}
			}
			fflush(logfp);
		}
	}

	Paf_dbimp_running = TRUE;

	Paf_db_init_tables(db_prefix,cache,cross_cache);

	linenum = 0;
	type = -999;

	switch (setjmp(BAD_IMPL_jmp_buf))
	{
	case PAF_PANIC_SOFT:
		killed = TRUE;

		fprintf(stderr,"dbimp (soft) panic");

		if (filename_g)
		{
			fprintf(stderr,", in file: %s, at line: %d column: %d\n",
				filename_g,yylineno,yycharno);
		}
		fprintf(stderr,"\n");
		fflush(stderr);
		break;

	case PAF_PANIC_SIMPLE:
		killed = TRUE;
		break;

	case PAF_PANIC_EMERGENCY:
		killed = -1;

		fprintf(stderr,"Run time error: %s\n",strerror(errno));
		fflush(stderr);
		break;
	}

	while (!killed && (bufp = buf.fgets(&buf,infp)))
	{
		if (logfp)
		{
			fputs(bufp,logfp);
			fputs("\n",logfp);
			fflush(logfp);
		}

		if (bufp[0] == '#' || bufp[0] == '\0')
			continue;

		linenum++;

		key = strchr(bufp,';');
		if (key)
			data = strchr(key + 1,';');
		else
			data = NULL;

		if (!key || !data)
		{
			fprintf(stderr,"dbimp Error: %s\n",bufp);
			fflush(stderr);
			continue;
		}

		save_c = *key;
		*key = '\0';
		type = strtol(bufp,NULL,0);
		*key = save_c;

		if (type < -3)
		{
			fprintf(stderr,"dbimp invalid type: %ld, %s\n",type,bufp);
			fflush(stderr);
			continue;
		}
		else if (type == -1)
		{
			db_remove_file_def(0,data + 1);
		}
		else if (type == -3)
		{
			db_remove_file_xfer_using_keys(0,data + 1);
		}
		else if (type == PAF_CROSS_REF_CPP)
		{
			if (first_xref)
			{
				first_xref = FALSE;

				open_tables_for_cross_ref();
			}
			Paf_insert_cross_ref_qry(bufp);
		}
		else
		{
			*data = '\0';
			db_insert_entry(type,key + 1,data + 1);
		}
	}

#if TTY_TRACE
	if (tty)
	{
		fprintf(tty,"END (pid: %d) killed: %d eof: %d\n",
			(int)getpid(),killed,feof(infp));
	}
#endif /* TTY_TRACE */

	buf.free(&buf);

	if (!first_xref)
	{
		Paf_Cpp_Cross_Ref_Clean();
	}

	if (Paf_db_close_tables() == -1)
	{
		fprintf(stderr,"Database closing error: %s\n",strerror(errno));
		fflush(stderr);

		killed = -1;
	}

	if (logfp)
	{
		fprintf(logfp,"#dbimp (pid: %lu) exited\n",(unsigned long)getpid());
		fclose(logfp);
	}

	if (yyfd >= 0)
	{
		close(yyfd);
		yyfd = -1;
	}

	if (lock_file[0])
	{
		if (killed == -1)
		{
			/*
			 * Mark the project as unusable. We must not open the
			 * file with O_TRUNC, because if the disk is already full,
			 * we will get a zero size file that we cannot write.
			 */
			int	fd = open(lock_file,O_WRONLY,0666);
			if (fd != -1)
			{
				char	status_buf[500];

				/* Now, we just overwrite the file. The 0000000 pid indicates
				 * that the project is unusable.
				 */
				sprintf(status_buf,"%s %s %7d\n",
					sn_pid,
					sn_host,
					0);

				write(fd,status_buf,strlen(status_buf));

				close(fd);
			}
		}
		else
		{
			/* Delete the lock file to indicate, that we did not crash! */
			if (unlink(lock_file) == -1 && access(lock_file,F_OK) != -1)
			{
				/* Under Windows, somebody might have open the file. */
				sleep(1);

#if TTY_TRACE
				if (tty)
				{
					fprintf(tty,"Removing lock file %d\n",(int)getpid());
				}
#endif /* TTY_TRACE */
				unlink(lock_file);
			}
		}
	}

	fclose(infp);	/* It is important to synchronize. */

	exit(killed == -1 ? 2 : 0);

	return 0;
}