Syscall per file system: equivalenza comandi

From Sistemi Operativi
Revision as of 17:59, 11 November 2025 by Renzo (talk | contribs) (Created page with "<pre> tutte queste syscall restituiscono il valore 0 se l'operazione ha s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
tutte queste syscall restituiscono il valore 0 se l'operazione ha                                                                                                      
successo o -1 in caso di errore (il tipo di errore lo si legge nella                                                                                                   
                "variabile" errno).                                                                                                                                    
Le system call indicate con (R!) possono essere eseguite solo da                                                                                                       
utenti privilegiati (root)                                                                                                                                             

// change working directory                                                                                                                                            
chdir("/home/user")                      cd /home/user                                                                                                                 

// create directory                                                                                                                                         
mkdir("/tmp/mydir", 0755)                mkdir -m 755 /tmp/mydir                                                                                                       

// remove (empty) directory                                                                                                                                            
rmdir("/tmp/olddir")                     rmdir /tmp/olddir                                                                                                             

// create (hard) link:                                                                                                                                                 
// add one more name to a file                                                                                                                                         
link("vecchio", "/tmp/nuovo")            ln vecchio /tmp/nuovo                                                                                                         

// create symbolic link                                                                                                                                                
link("vecchio", "/tmp/nuovolink")        ln -s vecchio /tmp/nuovolink                                                                                                  

// delete one name of a file,                                                                                                                                          
// removed it when it has no more names                                                                                                                                
unlink("/tmp/dacancellare")              rm /tmp/dacancellare                                                                                                          

// rename a file (atomically!)                                                                                                                                         
rename("vecchionome", "nuovonome")       rename vecchionome nuovonome                                                                                                  

// get file info (show i-node data)                                                                                                                                    
struct stat buf;                                                                                                                                
stat("/tmp/symlink", &buf)               stat -f /tmp/symlink                                                                                                          
/* le info vengono scritte in buf */                                                                                                                                   

// get file info                                                                                                                                
// (do not follow symbolic links)                                                                                                                                      
struct stat buf;                                                                                                                                
lstat("/tmp/symlink", &buf)               stat /tmp/symlink                                                                                                            
/* le info vengono scritte in buf */                                                                                                                                   

// change file permissions                                                                                                                                             
chmod("/tmp/miofile", 0750)               chmod 0750 /tmp/miofile                                                                                                      

// change file ownership (R!)
chown("/tmp/miofile", 1000, 1000)         chown 1000:1000 /tmp/miofile
/* il primo 1000 è l'user-id (uid),
         il secondo è il group-id (gid) */

// change file ownership (R!)
// (do not follow symbolic links)
lchown("/tmp/miofile", 1000, 1000)         chown -P 1000:1000 /tmp/miofile
/* il primo 1000 è l'user-id (uid),
         il secondo è il group-id (gid) */

// truncate the contents of a file to a specific length
truncate("/tmp/file", 420)                 truncate -s 420 /tmp/file

// get info of a mounted file system
struct statfs buf;
statfs("/", &buf)                          statfs /
/* le info vengono scritte in buf */

// change root directory (R!)
chroot("/tmp/newroot")                     chroot /tmp/newroot

// mount a filesystem (R!)
mount("/dev/hda0", "/mnt", "ext4", MS_RDONLY, "")
                                           mount -o ro -t ext4 /dev/hda0 /mnt

// umount a filesystem (R!)
umount("/mnt")                             umount /mnt

// create a special file
mknod("tmp/myfifo", S_IFIFO | 0770, 0)     mkfifo -m 770 /tmp/myfifo

// create a special file (device) (R!)
// major number = 1, minor number = 7
mknod("/tmp/mydev", S_IFCHR | 0660,        mknod -m 660 /tmp/mydev c 1 7
                makedev(1, 7));

// change access/modification time
struct utimbuf ut = {time(NULL), time(NULL)};
utime("/tmp/myfile", &ut);
                                           touch /tm/myfile

------------
// get the urrent working directory
// the glibc wrapper function returns
// a string
#include <linux/limits.h>                  readlink /tmp/symlink
...
char path[PATH_MAX];                       pwd
if (getcwd(path, PATH_MAX) == NULL)
        /* error management */ 


// read the value of a symbolic link 
#include <linux/limits.h>                  readlink /tmp/symlink
...
char path[PATH_MAX];                      
ssize_t pathlen = 
  readlink("/tmp/symlink", path, PATH_MAX);
if (pathlen == -1) /* error management */ ;
else
        path[pathlen] = 0;

// test if a file exists, or is readable/writable/executable
// the return value of access is 0=true (success), -1=false (failure)
// the exit value of the test command is 0=true, 1=false

access("/tmp/file", F_OK)   ↔  test -e /tmp/file
access("/tmp/file", R_OK)   ↔  test -r /tmp/file
access("/tmp/file", W_OK)   ↔  test -w /tmp/file
access("/tmp/file", X_OK)   ↔  test -x /tmp/file