From a59f46f457248921d34ba7175697a129469d6d90 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Thu, 21 Apr 2011 23:58:50 +0000 Subject: [PATCH] piccola correzione. --- fileadv.tex | 4 +- sources/test_signalfd.c | 151 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 sources/test_signalfd.c diff --git a/fileadv.tex b/fileadv.tex index 312c36f..c38a396 100644 --- a/fileadv.tex +++ b/fileadv.tex @@ -2199,8 +2199,8 @@ periodicità di ripetizione, per farlo su usa la funzione omologa di \func{timerfd\_settime}; il suo prototipo è: \begin{prototype}{sys/timerfd.h} {int timerfd\_settime(int fd, int flags, - const struct itimerspec *new_value, - struct itimerspec *old_value)} + const struct itimerspec *new\_value, + struct itimerspec *old\_value)} Crea un timer associato ad un file descriptor per la notifica. diff --git a/sources/test_signalfd.c b/sources/test_signalfd.c new file mode 100644 index 0000000..628bd4d --- /dev/null +++ b/sources/test_signalfd.c @@ -0,0 +1,151 @@ +/* test_signalfd.c + * + * Copyright (C) 2011 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program test_signalfd.c: + * Program to test signalfd behaviour on fd close + * + * Author: Simone Piccardi + * Jan. 2011 + * + * Usage: test_signalfd -h give all info's + * + ****************************************************************/ +/* + * Include needed headers + */ +#define _GNU_SOURCE +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* C strings library */ +#include /* signal constants, types and functions */ +#include /* Linux signalfd interface */ +#include + +#include "macros.h" +#include "Gapil.h" + +/* Subroutines declaration */ +void usage(void); +void die(char *); +void HandSIGINT(int signo); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i, nomask, testwrite, sigfd; + time_t t; + struct signalfd_siginfo siginf; + sigset_t sigmask; + char buffer[] = "test write"; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "hnw")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 'n': /* no mask option */ + nomask = 1; + break; + case 'w': /* no mask option */ + testwrite = 1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + Signal(SIGINT, HandSIGINT); + if (nomask != 1) { + sigemptyset(&sigmask); + sigaddset(&sigmask, SIGINT); + if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) // block signals + die("Failing in singal masking"); + } + if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) // take a signalfd + die("Failing in signalfd"); + printf("Signalfd armed\n"); + if (testwrite) { + if (write(sigfd, buffer, sizeof(buffer))) + perror("write on signal fd error"); + else printf("write successfully\n"); + } + /* raise signal */ + if (raise(SIGINT) != 0) + die("Failing in sending SIGINT"); + t = time(NULL); + printf("Lanciato SIGINT, %s\n", ctime(&t)); + sleep(2); + close(sigfd); + if (sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == -1) // block signals + die("Failing in signal unmasking"); + sleep(10); + printf("ERROR:SHOULD BE END IN THE SIGNAL HANDLER\n"); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program testsignalfd : test signalfd on close \n"); + printf("Usage:\n"); + printf(" testfopen [-h] file mode \n"); + printf(" -h print this help\n"); + + exit(1); +} +/* + * Print error message and exit routine + */ +void die(char * mess) { + perror(mess); + exit(-1); +} +/* + * Signal Handler to manage termination + */ +void HandSIGINT(int signo) { + time_t t; + debug("Terminated by %s\n", strsignal(signo)); + t = time(NULL); + printf("Handler executed %s\n", ctime(&t)); + exit(0); +} -- 2.30.2