From 7bd1670460aea5107ad0aeeb5f0639587ceb4399 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Thu, 3 Jan 2002 23:44:14 +0000 Subject: [PATCH] Vari programmi di test. Proseguito con le varie macro e funzione per la lettura delle caratteristiche. --- pref.tex | 14 ++-- sources/ProcInfo.c | 110 ++++++++++++++++++++++++ sources/TestRen.c | 101 ++++++++++++++++++++++ sources/getparam.c | 194 +++++++++++++++++++++++++++++++++++++++++++ sources/test_fopen.c | 110 ++++++++++++++++++++++++ system.tex | 176 ++++++++++++++++++++++++++++++++------- 6 files changed, 666 insertions(+), 39 deletions(-) create mode 100644 sources/ProcInfo.c create mode 100644 sources/TestRen.c create mode 100644 sources/getparam.c create mode 100644 sources/test_fopen.c diff --git a/pref.tex b/pref.tex index 0c80fe8..5e5776e 100644 --- a/pref.tex +++ b/pref.tex @@ -35,15 +35,15 @@ programmazione in ambito Unix generico, pur restando l'intenzione di approfondire in maniera specifica le caratteristiche peculiari di GNU/Linux. L'idea è quella di riuscire a ottenere alla fine un testo utilizzabile per -apprendere la programmazione sotto GNU/Linux della stessa qualità dei testi +apprendere la programmazione, sotto GNU/Linux della stessa qualità dei testi del compianto R. W. Stevens (è un progetto molto ambizioso ...). Infatti benché le man pages e il manuale delle librerie del C GNU siano una fonte inesauribile di informazioni (da cui si è costantemente attinto nella stesura di tutto il testo) la loro struttura li rende totalmente inadatti ad una trattazione che vada oltre la descrizione delle caratteristiche -particolari dell'argomento in esame (in particolare il manuale delle glibc non -brilla certo per chiarezza espositiva). +particolari dell'argomento in esame (in particolare lo \textit{GNU C Library + Reference Manual} non brilla certo per chiarezza espositiva). Per questo motivo si è cercato di fare tesoro di quanto appreso dai testi di R. Stevens (in particolare \textit{Advanced Programming in the Unix @@ -57,7 +57,9 @@ sia interessato. Dato che sia il kernel che tutte le librerie fondamentali di GNU/Linux sono scritte in C, questo sarà il linguaggio di riferimento del testo. In particolare il compilatore usato per provare tutti i programmi e gli esempi -descritti nel testo è lo GNU GCC. +descritti nel testo è lo GNU GCC. Il testo presuppone una conoscenza media del +linguaggio, e di quanto necessario per scrivere, compilare ed eseguire un +programma. Infine, dato che lo scopo del progetto è la produzione di un libro, si è scelto di usare LaTex come "ambiente di sviluppo" del medesimo, sia per @@ -68,10 +70,6 @@ Il testo sar - - - - %%% Local Variables: %%% mode: latex %%% TeX-master: "gapil" diff --git a/sources/ProcInfo.c b/sources/ProcInfo.c new file mode 100644 index 0000000..eb16fc0 --- /dev/null +++ b/sources/ProcInfo.c @@ -0,0 +1,110 @@ +/* ProcInfo.c + * + * Copyright (C) 2001 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_fopen.c: + * Program to test function fopen + * + * Author: Simone Piccardi + * Dec. 2001 + * + * Usage: procinfo -h give all info's + * + * $Id: ProcInfo.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $ + * + ****************************************************************/ +/* + * 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 /* string functions */ + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + FILE * file; + char * ptr; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + /* There must be 2 remaing parameters */ + if ( (argc-optind) != 2 ) { + printf("From %d arguments, removed %d options\n", argc, optind); + usage(); + } + if ( !(file = fopen(argv[1], argv[2]))) { + perror("cannot open file"); + exit(1); + } + i = 100; + ptr = malloc(i); + getline(&ptr, &i, file); +// fclean(file); /* do not exist on Linux */ + flockfile(file); + fclose(file); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program testfopen : test fopen for a file \n"); + printf("Usage:\n"); + printf(" testfopen [-h] file mode \n"); + printf(" -h print this help\n"); + + exit(1); +} + diff --git a/sources/TestRen.c b/sources/TestRen.c new file mode 100644 index 0000000..9e736de --- /dev/null +++ b/sources/TestRen.c @@ -0,0 +1,101 @@ +/* TestRen.c + * + * Copyright (C) 2001 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 TestRen.c: + * Program to test function rename + * + * Author: Simone Piccardi + * Oct. 2001 + * + * Usage: testrem -h give all info's + * + * $Id: TestRen.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $ + * + ****************************************************************/ +/* + * Include needed headers + */ +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* string functions */ + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + /* There must be 2 remaing parameters */ + if ( (argc-optind) != 2 ) { + printf("From %d arguments, removed %d options\n", argc, optind); + usage(); + } + if ( rename(argv[optind], argv[optind+1]) ) { + perror("cannot rename"); + exit(1); + } + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program testren : test renaming of file/directories \n"); + printf("Usage:\n"); + printf(" testrem [-h] fileorig filedest \n"); + printf(" -h print this help\n"); + + exit(1); +} + diff --git a/sources/getparam.c b/sources/getparam.c new file mode 100644 index 0000000..54212e3 --- /dev/null +++ b/sources/getparam.c @@ -0,0 +1,194 @@ +/* getparam.c + * + * Copyright (C) 2002 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 getparam.c: + * Program to test function fopen + * + * Author: Simone Piccardi + * Jan. 2002 + * + * Usage: getparam -h give all info's + * + * $Id: getparam.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $ + * + ****************************************************************/ +/* + * Include needed headers + */ +#define _GNU_SOURCE +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* string functions */ +#include +#include + + +#include /* for CLK_TCK e CLOCKS_PER_SEC */ + +/* Table of constants for sysconf() */ +char *sc_names[]={"_SC_ARG_MAX", + "_SC_CHILD_MAX", + "_SC_OPEN_MAX", + "_SC_STREAM_MAX", + "_SC_TZNAME_MAX", + "_SC_NGROUPS_MAX", + "_SC_SSIZE_MAX", + "_SC_CLK_TCK", + "_SC_JOB_CONTROL", + "_SC_SAVED_IDS", + "_SC_VERSION"}; + +int sc_argument[]={_SC_ARG_MAX, + _SC_CHILD_MAX, + _SC_OPEN_MAX, + _SC_STREAM_MAX, + _SC_TZNAME_MAX, + _SC_NGROUPS_MAX, + _SC_SSIZE_MAX, + _SC_CLK_TCK, + _SC_JOB_CONTROL, + _SC_SAVED_IDS, + _SC_VERSION}; + +/* + Set the defined[] array to true if the macro is defined else set it + to false and define the macro to -1 as a marker +*/ +int defined[]={ +#ifdef ARG_MAX + 1, +#else +#define ARG_MAX -1 + 0, +#endif +#ifdef CHILD_MAX + 1, +#else +#define CHILD_MAX -1 + 0, +#endif +#ifdef OPEN_MAX + 1, +#else +#define OPEN_MAX -1 + 0, +#endif +#ifdef STREAM_MAX + 1, +#else +#define STREAM_MAX -1 + 0, +#endif +#ifdef NGROUPS_MAX + 1, +#else +#define NGROUPS_MAX -1 + 0, +#endif +#ifdef TZNAME_MAX + 1, +#else +#define TZNAME_MAX -1 + 0, +#endif +#ifdef SSIZE_MAX + 1 +#else +#define SSIZE_MAX -1 + 0 +}; + + + +/* values of stadard macros */ +long values[]={ARG_MAX, + CHILD_MAX, + OPEN_MAX, + STREAM_MAX, + TZNAME_MAX, + NGROUPS_MAX, + SSIZE_MAX, + CLOCKS_PER_SEC, + _POSIX_JOB_CONTROL, + _POSIX_SAVED_IDS, + _POSIX_VERSION}; + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + /* There must be 2 remaing parameters */ + if ( (argc-optind) != 1 ) { + printf("From %d arguments, removed %d options\n", argc, optind); + usage(); + } + for (i=0; i<=4; i++) { + printf("Response for %s is %ld, values is %ld\n", names[i], + sysconf(argument[i]), values[i]); + } + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program testfopen : test fopen for a file \n"); + printf("Usage:\n"); + printf(" testfopen [-h] file mode \n"); + printf(" -h print this help\n"); + + exit(1); +} + diff --git a/sources/test_fopen.c b/sources/test_fopen.c new file mode 100644 index 0000000..f783afe --- /dev/null +++ b/sources/test_fopen.c @@ -0,0 +1,110 @@ +/* test_fopen.c + * + * Copyright (C) 2001 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_fopen.c: + * Program to test function fopen + * + * Author: Simone Piccardi + * Oct. 2001 + * + * Usage: testfopen -h give all info's + * + * $Id: test_fopen.c,v 1.1 2002/01/03 23:44:14 piccardi Exp $ + * + ****************************************************************/ +/* + * 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 /* string functions */ + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + FILE * file; + char * ptr; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + /* There must be 2 remaing parameters */ + if ( (argc-optind) != 2 ) { + printf("From %d arguments, removed %d options\n", argc, optind); + usage(); + } + if ( !(file = fopen(argv[1], argv[2]))) { + perror("cannot open file"); + exit(1); + } + i = 100; + ptr = malloc(i); + getline(&ptr, &i, file); +// fclean(file); /* do not exist on Linux */ + flockfile(file); + fclose(file); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program testfopen : test fopen for a file \n"); + printf("Usage:\n"); + printf(" testfopen [-h] file mode \n"); + printf(" -h print this help\n"); + + exit(1); +} + diff --git a/system.tex b/system.tex index 211b112..27f484c 100644 --- a/system.tex +++ b/system.tex @@ -171,9 +171,9 @@ caratteristiche dei file, riportate in \tabref{tab:sys_file_macro}). Lo standard prevede che queste macro devono essere definite in \file{limits.h} quando i valori a cui fanno riferimento sono fissi, e altrimenti devono essere lasciate indefinite, ed i loro valori dei limiti devono essere accessibili -solo attraverso \func{sysconf}. In realtà in Linux queste sono comunque -definite e spesso indicano un limite generico, che può però essere superato -dal valore restituito da \func{sysconf}. +solo attraverso \func{sysconf}. Si tenga presente poi che alcuni di questi +limiti possono assumere valori molto elevati (come \macro{CHILD\_MAX}), e non +è pertanto il caso di utilizzarli per allocare staticamente della memoria. A complicare la faccenda si aggiunge il fatto che POSIX.1 prevede una serie di altre macro (che iniziano sempre con \code{\_POSIX\_}) che definiscono i @@ -217,44 +217,64 @@ riportati in \tabref{tab:sys_posix1_base}. \label{tab:sys_posix1_general} \end{table} -In genere questi valori sono di scarsa utilità, la loro unica utilità è quella +In genere questi valori non servono a molto, la loro unica utilità è quella di indicare un limite superiore che assicura la portabilità senza necessità di ulteriori controlli. Tuttavia molti di essi sono troppo ristretti, ed -ampiamente superati in tutti i sistemi POSIX in uso oggigiorno. - -Oltre ai precedenti valori, previsti obbligatoriamente, lo standard POSIX +ampiamente superati in tutti i sistemi POSIX in uso oggigiorno. Per questo è +sempre meglio usare i valori provvisti da \func{sysconf}. \begin{table}[htb] \centering \footnotesize - \begin{tabular}[c]{|l|r|p{8cm}|} + \begin{tabular}[c]{|l|p{8cm}|} \hline - \textbf{Macro}&\textbf{Valore}&\textbf{Significato}\\ + \textbf{Macro}&\textbf{Significato}\\ \hline \hline - \macro{\_POSIX\_MQ\_OPEN\_MAX}& 8& \\ - \macro{\_POSIX\_MQ\_PRIO\_MAX}& 32& \\ - \macro{\_POSIX\_FD\_SETSIZE}& 16 & \\ - \macro{\_POSIX\_DELAYTIMER\_MAX}& 32 & \\ + \macro{\_POSIX\_JOB\_CONTROL}& il sistema supporta il + \textit{job control} (vedi + \secref{sec:sess_xxx}).\\ + \macro{\_POSIX\_SAVED\_IDS} & il sistema supporta i \textit{saved id} + (vedi \secref{sec:proc_access_id}). + per il controllo di accesso dei processi\\ + \macro{\_POSIX\_VERSION} & fornisce la versione dello standard POSIX.1 + supportata nel formato YYYYMML (ad esempio + 199009L).\\ \hline \end{tabular} - \caption{Macro definite in \file{limits.h} in conformità allo standard + \caption{Alcune macro definite in \file{limits.h} in conformità allo standard POSIX.1.} \label{tab:sys_posix1_other} \end{table} -Oltre a questi - +Oltre ai precedenti valori (e a quelli elencati in +\tabref{tab:sys_posix1_file}), che devono essere obbligatoriamente definiti, +lo standard POSIX.1 ne prevede parecchi altri; in Linux la lista completa si +può ricavare dall'header file \file{bits/posix1\_lim.h} (da non usare mai +direttamente, è incluso automaticamente all'interno di \file{limits.h}); di +questi vale la pena menzionare quelli di uso più comune, riportati in +\tabref{tab:sys_posix1_other}, che permettono di ricavare alcune +caratteristiche del sistema (come il supporto del \textit{job control} o dei +\textit{saved id}). + +Oltre allo standard POSIX.1, anche lo standard POSIX.2 definisce una serie di +altre macro. Siccome queste sono principalmente attinenti a limiti relativi +alle applicazioni di sistema presenti (come quelli su alcuni parametri delle +espressioni regolari o del comando \cmd{bc}), non li tratteremo +esplicitamente, se ne trova una menzione completa nell'header file +\file{bits/posix2\_lim.h}, e alcuni di loro sono descritti nella man page di +\func{sysconf} e nel manuale delle \acr{glibc}. \subsection{La funzione \func{sysconf}} \label{sec:sys_sysconf} Come accennato in \secref{sec:sys_limits} quando uno dei limiti o delle -caratteristiche del sistema può variare, per evitare di dover ricompilare un -programma tutte le volte che si cambiano le opzioni con cui è compilato il -kernel, o alcuni dei parametri modificabili a run time, è necessario ottenerne -il valore attraverso la funzione \func{sysconf}, il cui prototipo è: +caratteristiche del sistema può variare, è necessario ottenerne il valore +attraverso la funzione \func{sysconf}, per non dover essere costretti a +ricompilare un programma tutte le volte che si cambiano le opzioni con cui è +compilato il kernel, o alcuni dei parametri modificabili a run time. Il suo +prototipo è: \begin{prototype}{unistd.h}{long sysconf(int name)} Restituisce il valore del parametro di sistema \param{name}. @@ -264,10 +284,10 @@ il valore attraverso la funzione \func{sysconf}, il cui prototipo \end{prototype} La funzione prende come argomento un intero che specifica quale dei limiti si -vuole conoscere; uno specchietto contenente i principali valori disponibili -in Linux (l'elenco completo è contenuto in \file{bits/confname}, una lista più -esaustiva si può trovare nel manuale delle \acr{glibc}), e la corrispondente -macro di sistema, è riportato in \tabref{tab:sys_sysconf_par}. +vuole conoscere; uno specchietto contenente i principali valori disponibili in +Linux è riportato in \tabref{tab:sys_sysconf_par}; l'elenco completo è +contenuto in \file{bits/confname}, ed una lista più esaustiva, con le relative +spiegazioni, si può trovare nel manuale delle \acr{glibc}. \begin{table}[htb] \centering @@ -316,12 +336,32 @@ macro di sistema, \label{tab:sys_sysconf_par} \end{table} -In generale ogni valore per cui è definita una macro, sia dagli standard ANSI -C e ISO C90, che da POSIX.1 e POSIX.2, può essere ottenuto attraverso una -chiamata a \func{sysconf}, ed il valore di \param{name} si ottiene appendendo -un \code{\_SC\_} per o primi due o sostituendolo a \code{\_POSIX\_} per gli -altri due. Lo stesso dicasi per le macro relative alle caratteristiche dei -file. +In generale ogni limite o caratteristica del sistema per cui è definita una +macro, sia dagli standard ANSI C e ISO C90, che da POSIX.1 e POSIX.2, può +essere ottenuto attraverso una chiamata a \func{sysconf}. Il valore si otterrà +speficando come valore del parametro \param{name} il nome ottenuto aggiungendo +\code{\_SC\_} ai nomi delle macro definite dai primi due, o sostituendolo a +\code{\_POSIX\_} per le macro definite dagli gli altri due. + +In generale si dovrebbe fare uso di \func{sysconf} solo quando la relativa +macro non è definita, quindi con un codice analogo al seguente: +\begin{lstlisting}[labelstep=0,frame=,indent=1cm]{} +get_child_max(void) +{ +#ifdef CHILD_MAX + return CHILD_MAX; +#else + int val = sysconf(_SC_CHILD_MAX); + if (val < 0) { + perror("fatal error"); + exit(-1); + } + return val; +} +\end{lstlisting} +ma in realtà in Linux queste macro sono comunque definite e indicando un +limite generico, per cui è sempre meglio usare i valori restituiti da +quest'ultima. \subsection{I limiti dei file} @@ -358,7 +398,7 @@ riportate in \tabref{tab:sys_file_macro}. Come per i limiti di sistema POSIX.1 detta una serie di valori minimi per queste caratteristiche, che ogni sistema che vuole essere conforme deve rispettare; le relative macro sono riportate in \tabref{tab:sys_posix1_file}, -e vale lo stesso discorso fatto per le analoghe di +e per esse vale lo stesso discorso fatto per le analoghe di \tabref{tab:sys_posix1_general}. \begin{table}[htb] @@ -390,18 +430,92 @@ e vale lo stesso discorso fatto per le analoghe di \label{tab:sys_posix1_file} \end{table} +Tutti questi limiti sono definiti in \file{limits.h}; come nel caso precedente +il loro uso è di scarsa utilità in quanto ampiamente superati in tutte le +implementazioni moderne. + \subsection{La funzione \func{pathconf}} \label{sec:sys_pathconf} +In generale i limiti per i file sono molto più soggetti ad essere variabili +rispetto ai precedenti limiti generali del sistema; ad esempio parametri come +la lunghezza del nome del file o il numero di link possono variare da +filesystem a filesystem; per questo motivo questi limiti devono essere sempre +controllati con la funzione \func{pathconf}, il cui prototipo è: +\begin{prototype}{unistd.h}{long pathconf(char *path, int name)} + Restituisce il valore del parametro \param{name} per il file \param{path}. + + \bodydesc{La funzione restituisce indietro il valore del parametro + richiesto, o -1 in caso di errore (ed \var{errno} viene settata ad uno + degli errori possibili relativi all'accesso a \param{path}).} +\end{prototype} + +E si noti come la funzione in questo caso richieda un parametro che specifichi +a quale file si fa riferimento, dato che il valore del limite cercato può +variare a seconda del filesystem. Una seconda versione della funzione, +\func{fpathconf}, opera su un file descriptor invece che su un pathname, il +suo prototipo è: +\begin{prototype}{unistd.h}{long fpathconf(int fd, int name)} + Restituisce il valore del parametro \param{name} per il file \param{fd}. + + \bodydesc{È identica a \func{pathconf} solo che utilizza un file descriptor + invece di un pathname; pertanto gli errori restituiti cambiano di + conseguenza.} +\end{prototype} +\noindent ed il suo comportamento è identico a quello di \func{fpathconf}. + + +\subsection{La funzione \func{uname}} +\label{sec:sys_uname} + +Una altra funzione che si può utilizzare per raccogliere informazioni riguardo +al sistema ed al computer su cui esso sta girando è \func{uname}, il suo +prototipo è: +\begin{prototype}{sys/utsname.h}{int uname(struct utsname *info)} + Restituisce informazioni sul sistema nella struttura \param{info}. + + \bodydesc{La funzione ritorna 0 in caso di successo e -1 in caso di + fallimento, nel qual caso \var{errno} viene settata a \macro{EFAULT}.} +\end{prototype} +\noindent la struttura è anch'essa definita in \file{sys/utsname.h} come: +\begin{lstlisting}[labelstep=0,frame=,indent=1cm]{} + struct utsname { + char sysname[_UTSNAME_LENGTH]; + char nodename[_UTSNAME_LENGTH]; + char release[_UTSNAME_LENGTH]; + char version[_UTSNAME_LENGTH]; + char machine[_UTSNAME_LENGTH]; +#ifdef _GNU_SOURCE + char domainname[_UTSNAME_DOMAIN_LENGTH]; +#endif + }; +\end{lstlisting} +ed i suoi menbri indicano rispettivamente: +\begin{itemize*} +\item il nome del systema operativo; +\item il nome della release del kernel; +\item il nome della versione del kernel; +\item il tipo di macchina in uso; +\item il nome della stazione; +\item il nome del domino (è una estensione recente). +\end{itemize*} + \section{Opzioni e configurazione del sistema} \label{sec:sys_config} +In questa sezione prenderemo in esame le funzioni per leggere e settare i vari +parametri di configurazione del sistema. + + \subsection{La funzione \func{sysctl}} \label{sec:sys_sysctl} +\subsection{Il filesystem \file{/proc}} +\label{sec:sys_proc_files} + \subsection{La configurazione dei filesystem} \label{sec:sys_file_config} -- 2.30.2