Пример #1
0
void handle_usb_unplugged(char* device){
	must_exit = 1;
	pid_t pid = fork();

	if (pid ==0){ /*child process*/			
		execl("/bin/umount", "umount", "-l", device, (char *)0);
	}else{
		waitpid(pid,0,0);
		
		MacRec *head = (MacRec *) ht_lookup(macaddrs, device);
		char* signal =  (char*) malloc(32);
		
		if (head != NULL){
			
			sprintf(signal, head->addr, 0, 17);
			strcat(signal, " deny");
			sendsignal(signal);
			MacRec *current = head;
			
			while ( (current = current->next) != NULL){
			    printf("deny --> %s\n", current->addr);
				sprintf(signal, current->addr, 0, 17);
				strcat(signal, " deny");
				sendsignal(signal);
			}
			ht_remove(macaddrs, device);
			free_macrec(head);
			free(signal);
		}
	}
}
Пример #2
0
void handle_usb_plugged(char* device){
	pid_t pid = fork();
	pthread_t thr;
	//char device[128];
	char mountpoint[128];
	must_exit = 0;
	
	//sprintf(device, "/dev/%s", data);
	sprintf(mountpoint, "/mnt");

	if (pid ==0){ /*child process*/
		printf("mounting...%s\n", device);
		mkdir(mountpoint, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		execl("/bin/mount", "mount", "-o", "sync", device, mountpoint, (char *)0);
		
	}else{ /*pid !=0 parent process*/
		waitpid(pid,0,0);
		DIR *dp;
		struct dirent *ep;
		printf("mounted!!, opening %s..\n", mountpoint);
		dp = opendir(mountpoint);
		printf("opened dir\n");
		if (dp != NULL){
			MacRec* previous = NULL;
			MacRec* head = NULL;
			char* signal =  (char*) malloc(32);
			
			while (ep = readdir(dp)){
				if (match(ep->d_name, "^([[:xdigit:]]{1,2}\\-){5}[[:xdigit:]]{1,2}")){
					replace_char(ep->d_name, '-', ':');
					MacRec* m = (MacRec *)malloc(sizeof(MacRec));
					m->next = NULL;
					sprintf(m->addr, "%s", ep->d_name);
					if (previous != NULL){
						previous->next = m;
					}else{
						head = m;
					}
					previous = m;
					
					//signal = "";
					sprintf(signal, ep->d_name, 0, 17);
					strcat(signal, " permit");
					printf("signal is %s\n", signal);
					sendsignal(signal);
					printf("would permit %s\n", ep->d_name);
				}
			}
			if (head){ //if any matches found
                printf("adding %s to macrec!\n", head->addr);
                ht_insert(macaddrs, device, head);
            }
			
			free(signal);
			(void) closedir(dp);
		}else{
			perror("couldn't open the directory!");
		}	   
	}
}
Пример #3
0
int main(int argc, char** argv)
{
   if (2 > argc) {
      printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]\n");
      return 1;
   }
   char* param = "no param";
   int repeatmode = 0;
   if (3 <= argc && NULL != argv[2]) param = argv[2];
    if (4 <= argc && NULL != argv[3]) {
        if ( strncmp(argv[3],"1",2) == 0 ) {
            repeatmode = 1;
        } else if ( strncmp(argv[3],"2",2) == 0 ) {
            repeatmode = 2;
        }
    }
    if (5 <= argc && NULL != argv[4]) {
        long t = atol(argv[4]);
        if ( t > 0 && t < TMNMAX ) {
            tmnmax = (unsigned int)t;
            printf(" repeat max new value %u\n", tmnmax);
        } else {
            printf(" repeat max out of range %ld\n", t);
        }
    }
   if (0 == strcmp(argv[1], "send"))
      sendsignal(param);
   else if (0 == strcmp(argv[1], "receive"))
      receive();
   else if (0 == strcmp(argv[1], "listen"))
      listen();
   else if (0 == strcmp(argv[1], "query"))
    {
        printf(" repeatmode %d \n", repeatmode);
        if ( repeatmode == 0 ) {
      query(param, 0, 0);
        } else if ( repeatmode == 1 ) {
            int i=0;
            query(param, 0, 1);
            for (i=0; i<tmnmax; i++) {
                query(param, 0, 1);
            }
        } else if ( repeatmode == 2 ) {
            query(param, 0, 2);
        }
    }
   else if (0 == strcmp(argv[1], "selector"))
      dbus_selector(param, 0);
   else if (0 == strcmp(argv[1], "seltest"))
      query(param, 1, 0);
   else if (0 == strcmp(argv[1], "selpost"))
      dbus_selector(param, 1);
   else {
      printf ("Syntax: dbus-select [[send|receive|listen|query]|"
                        "[selector|seltest|selpost]] [<param>]\n");
      return 1;
   }
   return 0;
}
Пример #4
0
int main(int argc, char** argv)
{
   if (2 > argc) {
	   fprintf(stdout, "Syntax: %s [send|receive] [<param>]\n", argv[0] );
      return 1;
   }
   char* param = "no param";
   if (3 >= argc && NULL != argv[2]) param = argv[2];
   if (0 == strcmp(argv[1], "send"))
      sendsignal(param);
   else if (0 == strcmp(argv[1], "receive"))
      receive();
   else {
	   fprintf (stdout, "Syntax: dbus-example [send|receive|listen|query] [<param>]\n");
      return 1;
   }
   return 0;
}
Пример #5
0
//产生信号的按钮
pb2::pb2(QWidget *parent) :
QPushButton(parent)
{
	connect(this, SIGNAL(clicked()), this, SLOT(sendsignal()));
}
Пример #6
0
int main(int argc, char *argv[])
{
    const char	*prog = xbasename(argv[0]);
    int	i, cmd = 0, checking_flag = 0;

    printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);

    /* if no configuration file is specified on the command line, use default */
    configfile = xmalloc(SMALLBUF);
    snprintf(configfile, SMALLBUF, "%s/upsmon.conf", confpath());
    configfile = xrealloc(configfile, strlen(configfile) + 1);

    run_as_user = xstrdup(RUN_AS_USER);

    while ((i = getopt(argc, argv, "+Dhic:f:pu:VK46")) != -1) {
        switch (i) {
        case 'c':
            if (!strncmp(optarg, "fsd", strlen(optarg)))
                cmd = SIGCMD_FSD;
            if (!strncmp(optarg, "stop", strlen(optarg)))
                cmd = SIGCMD_STOP;
            if (!strncmp(optarg, "reload", strlen(optarg)))
                cmd = SIGCMD_RELOAD;

            /* bad command name given */
            if (cmd == 0)
                help(argv[0]);
            break;
        case 'D':
            nut_debug_level++;
            break;
        case 'f':
            free(configfile);
            configfile = xstrdup(optarg);
            break;
        case 'h':
            help(argv[0]);
            break;
        case 'K':
            checking_flag = 1;
            break;
        case 'p':
            use_pipe = 0;
            break;
        case 'u':
            free(run_as_user);
            run_as_user = xstrdup(optarg);
            break;
        case 'V':
            /* just show the banner */
            exit(EXIT_SUCCESS);
        case '4':
            opt_af = AF_INET;
            break;
        case '6':
            opt_af = AF_INET6;
            break;
        default:
            help(argv[0]);
            break;
        }
    }

    if (cmd) {
        sendsignal(prog, cmd);
        exit(EXIT_SUCCESS);
    }

    /* otherwise, we are being asked to start.
     * so check if a previous instance is running by sending signal '0'
     * (Ie 'kill <pid> 0') */
    if (sendsignal(prog, 0) == 0) {
        printf("Fatal error: A previous upsmon instance is already running!\n");
        printf("Either stop the previous instance first, or use the 'reload' command.\n");
        exit(EXIT_FAILURE);
    }

    argc -= optind;
    argv += optind;

    open_syslog(prog);

    loadconfig();

    if (checking_flag)
        exit(check_pdflag());

    if (shutdowncmd == NULL)
        printf("Warning: no shutdown command defined!\n");

    /* we may need to get rid of a flag from a previous shutdown */
    if (powerdownflag != NULL)
        clear_pdflag();
    /* FIXME (else): POWERDOWNFLAG is not defined!!
     * => fallback to a default value */

    if (totalpv < minsupplies) {
        printf("\nFatal error: insufficient power configured!\n\n");

        printf("Sum of power values........: %d\n", totalpv);
        printf("Minimum value (MINSUPPLIES): %d\n", minsupplies);

        printf("\nEdit your upsmon.conf and change the values.\n");
        exit(EXIT_FAILURE);
    }

    if (nut_debug_level < 1) {
        background();
    } else {
        upsdebugx(1, "debug level is '%d'", nut_debug_level);
    }

    /* only do the pipe stuff if the user hasn't disabled it */
    if (use_pipe) {
        struct passwd	*new_uid = get_user_pwent(run_as_user);

        /* === root parent and unprivileged child split here === */
        start_pipe();

        /* write the pid file now, as we will soon lose root */
        writepid(prog);

        become_user(new_uid);
    } else {
        upslogx(LOG_INFO, "Warning: running as one big root process by request (upsmon -p)");

        writepid(prog);
    }

    /* prep our signal handlers */
    setup_signals();

    /* reopen the log for the child process */
    closelog();
    open_syslog(prog);

    while (exit_flag == 0) {
        utype_t	*ups;

        /* check flags from signal handlers */
        if (userfsd)
            forceshutdown();

        if (reload_flag)
            reload_conf();

        for (ups = firstups; ups != NULL; ups = ups->next)
            pollups(ups);

        recalc();

        /* make sure the parent hasn't died */
        if (use_pipe)
            check_parent();

        /* reap children that have exited */
        waitpid(-1, NULL, WNOHANG);

        sleep(sleepval);
    }

    upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
    upsmon_cleanup();

    exit(EXIT_SUCCESS);
}