Различия между версиями 5 и 6
Версия 5 от 2013-05-06 14:05:24
Размер: 16812
Редактор: FrBrGeorge
Комментарий:
Версия 6 от 2013-05-06 14:06:58
Размер: 16804
Редактор: FrBrGeorge
Комментарий:
Удаления помечены так. Добавления помечены так.
Строка 1: Строка 1:
== Linux-specific == = Linux-specific =
Строка 216: Строка 216:
=== procfs === == procfs ==
Строка 222: Строка 222:
=== sysfs === == sysfs ==
Строка 353: Строка 353:
=== inotify === == inotify ==

Linux-specific

или за что вас будут ненавидеть пользователи *BSD и Solaris, когда будут портировать ваш код

  • API:
    • Дополнительные возможности
      • Управление памятью

        mbind \- set memory policy for a memory range
        mremap \- remap a virtual memory address
        remap_file_pages \- create a nonlinear file mapping
      • Управление процессами

        clone, __clone2 \- create a child process
        prlimit \- get/set resource limits
        ioprio_get, ioprio_set \- get/set I/O scheduling class and priority
        personality \- set the process execution domain
        prctl \- operations on a process
        sched_setaffinity, sched_getaffinity \- \
        sched_setscheduler, sched_getscheduler \-
      • Неймспейсы

        setns \- reassociate thread with a namespace
      • Треды

        exit_group \- exit all threads in a process
        get_thread_area \- get a thread-local storage (TLS) area
        gettid \- get thread identification
        set_thread_area \- set a thread local storage (TLS) area
        set_tid_address \- set pointer to thread ID
        tkill, tgkill \- send a signal to a thread
      • epoll

        epoll_create, epoll_create1 \- open an epoll file descriptor
        epoll_ctl \- control interface for an epoll descriptor
        epoll_wait, epoll_pwait \- wait for an I/O event on an epoll file descriptor
      • extended attributes

        getxattr, lgetxattr, fgetxattr \- retrieve an extended attribute value
        listxattr, llistxattr, flistxattr \- list extended attribute names
        removexattr, lremovexattr, fremovexattr \- remove an extended attribute
        setxattr, lsetxattr, fsetxattr \- set an extended attribute value
      • numa

        get_mempolicy \- retrieve NUMA memory policy for a process
        getcpu \- determine CPU and NUMA node on which the calling thread is running
        migrate_pages \- move all pages in a process to another set of nodes
        move_pages \- move individual pages of a process to another node
        set_mempolicy \- set default NUMA memory policy for a process and its children
      • dnotify/inotify

        fcntl \- manipulate file descriptor (F_GETOWN_EX, F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE, F_SETLEASE)
        inotify_add_watch \- add a watch to an initialized inotify instance
        inotify_init, inotify_init1 \- initialize an inotify instance
        inotify_rm_watch \- remove an existing watch from an inotify instance
      • capabilities

        capget, capset \- set/get capabilities of thread(s)
      • futex

        futex \- fast user-space locking
    • Костыли

      adjtimex \- tune kernel clock
      bdflush \- start, flush, or tune buffer-dirty-flush daemon
      idle - make process 0 idle (legacy)
      ioperm \- set port input/output permissions (obscure)
      iopl \- change I/O privilege level (obscure)
      ipc \- System V IPC system calls
      kexec_load \- load a new kernel for later execution
      lookup_dcookie \- return a directory entry's path (obscure)
      mount \- mount file system
      nfsservctl \- syscall interface to kernel nfs daemon
      pciconfig_read, pciconfig_write, pciconfig_iobase \- pci device information handling
      readahead \- perform file readahead into page cache
      readdir \- read directory entry (legacy)
      reboot \- reboot or enable/disable Ctrl-Alt-Del
      sendfile \- transfer data between file descriptors
      setup \- setup devices and file systems, mount root file system (obscure)
      swapon, swapoff \- start/stop swapping to file/device
      sync, syncfs \- commit buffer cache to disk
      sync_file_range \- sync a file segment with disk
      sysctl \- read/write system parameters
      sysinfo \- returns information on overall system statistics
      umount, umount2 \- unmount file system
      • Управление модулями

        create_module \- create a loadable module entry
        delete_module \- unload a kernel module
        get_kernel_syms \- retrieve exported kernel and module symbols
        init_module \- load a kernel module
        query_module \- query the kernel for various bits pertaining to modules
      • Security

        pivot_root \- change the root file system
        setfsgid \- set group identity used for file system checks
        setfsuid \- set user identity used for file system checks
        unshare \- disassociate parts of the process execution context
      • Расширение POSIX

        dup3 \- duplicate a file descriptor
        eventfd \- create a file descriptor for event notification
        fallocate \- manipulate file space (есть posix_fallocate)
        fcntl \- manipulate file descriptor (F_GETOWN_EX, F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE, F_SETLEASE)
        getrusage \- get resource usage (потребление потока)
        _llseek \- reposition read/write file offset (legac5)
        madvise \- give advice about use of memory (posix_madvise)
        mmap2 \- map files or devices into memory
        msgctl \- message control operations (IPC_INFO, MSG_INFO, MSG_STAT)
        pipe2 \- create pipe
        ppoll \- wait for some event on a file descriptor
        rt_sigqueueinfo, rt_tgsigqueueinfo \- queue a signal and data
        pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
        semctl \- semaphore control operations (IPC_INFO, SEM_INFO, SEM_STAT)
        recvmmsg
        sendmmsg
        signalfd \- create a file descriptor for accepting signals
        shmctl \- shared memory control (IPC_INFO, SHN_INFO, SHM_STAT, SHM_LOCK/SHM_UNLOCK)
        splice \- splice data to/from a pipe
        statfs, fstatfs \- get file system statistics
        tee \- duplicating pipe content
        timer_create \- create a POSIX per-process timer
        timerfd_create, timerfd_settime, timerfd_gettime \-
        uselib \- load shared library
        vhangup \- virtually hangup the current tty
        vmsplice \- splice user pages into a pipe
        • aio

          io_cancel \- cancel an outstanding asynchronous I/O operation
          io_destroy \- destroy an asynchronous I/O context
          io_getevents \- read asynchronous I/O events from the completion queue
          io_setup \- create an asynchronous I/O context
          io_submit \- submit asynchronous I/O blocks for processing
      • personality

        personality \- set the process execution domain
    • Platform-specific особенности

      alloc_hugepages (obscure, x86-64 only)
      arch_prctl - set architecture-specific thread state (obscure, x86-64 only)
      cacheflush \- flush contents of instruction and/or data cache (obscure, MIPS only)
      modify_ldt \- get or set ldt (obscure, x86 only)
      spu_create \- create a new spu context (obscure, ppc only)
      spu_run \- execute an SPU context (obscure, ppc only)
      subpage_prot \- define a subpage protection for an address range (ppc only)
      vm86 (obscure, x86 only)


procfs

  • Специфичен для ряда UNIX-like систем, следует идее "всё есть файл"
  • Для linux специфично активное использование procfs не только для получения информации о процессах
    • Это не в последнюю очередь связано с простотой добавления подобной поддержки в ядерные модули и универсальное удобство (как следствие, востребованность) файлового интерфейса

sysfs

Рассказать про kobject, ktype и kset.

    struct uio_map {
        struct kobject kobj;
        struct uio_mem *mem;
    };

    #define to_map(map) container_of(map, struct uio_map, kobj)

    struct uio_map *map = to_map(kobj);

    void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
    int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
    int kobject_rename(struct kobject *kobj, const char *new_name);
    const char *kobject_name(const struct kobject * kobj);
    int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
                             struct kobject *parent, const char *fmt, ...);
    int kobject_uevent(struct kobject *kobj, enum kobject_action action);
    kobject_del()

    struct kobject *kobject_get(struct kobject *kobj);
    void kobject_put(struct kobject *kobj);

    struct kobj_type {
            void (*release)(struct kobject *);
            const struct sysfs_ops *sysfs_ops;
            struct attribute    **default_attrs;
    };

    struct kset *kset_create_and_add(const char *name,
                                   struct kset_uevent_ops *u,
                                   struct kobject *parent);
    struct kset_uevent_ops {
        int (*filter)(struct kset *kset, struct kobject *kobj);
        const char *(*name)(struct kset *kset, struct kobject *kobj);
        int (*uevent)(struct kset *kset, struct kobject *kobj,
                      struct kobj_uevent_env *env);
    };

sysfs

    int sysfs_create_file(struct kobject *kobj, struct attribute *attr);
    int sysfs_create_group(struct kobject *kobj, struct attribute_group *grp);


struct attribute {
        char                    * name;
        struct module           *owner;
        umode_t                 mode;
};

struct device_attribute {
        struct attribute        attr;
        ssize_t (*show)(struct device *dev, struct device_attribute *attr,
                        char *buf);
        ssize_t (*store)(struct device *dev, struct device_attribute *attr,
                         const char *buf, size_t count);
};

configfs

        struct config_item {
                char                    *ci_name;
                char                    ci_namebuf[UOBJ_NAME_LEN];
                struct kref             ci_kref;
                struct list_head        ci_entry;
                struct config_item      *ci_parent;
                struct config_group     *ci_group;
                struct config_item_type *ci_type;
                struct dentry           *ci_dentry;
        };

        void config_item_init(struct config_item *);
        void config_item_init_type_name(struct config_item *,
                                        const char *name,
                                        struct config_item_type *type);
        struct config_item *config_item_get(struct config_item *);
        void config_item_put(struct config_item *);

[struct config_item_type]

        struct configfs_item_operations {
                void (*release)(struct config_item *);
                ssize_t (*show_attribute)(struct config_item *,
                                          struct configfs_attribute *,
                                          char *);
                ssize_t (*store_attribute)(struct config_item *,
                                           struct configfs_attribute *,
                                           const char *, size_t);
                int (*allow_link)(struct config_item *src,
                                  struct config_item *target);
                int (*drop_link)(struct config_item *src,
                                 struct config_item *target);
        };

        struct config_item_type {
                struct module                           *ct_owner;
                struct configfs_item_operations         *ct_item_ops;
                struct configfs_group_operations        *ct_group_ops;
                struct configfs_attribute               **ct_attrs;
        };

[struct configfs_attribute]

        struct configfs_attribute {
                char                    *ca_name;
                struct module           *ca_owner;
                umode_t                  ca_mode;
        };


        struct configfs_subsystem {
                struct config_group     su_group;
                struct mutex            su_mutex;
        };

        int configfs_register_subsystem(struct configfs_subsystem *subsys);
        void configfs_unregister_subsystem(struct configfs_subsystem *subsys);

inotify

        int inotify_init (void);
        int inotify_add_watch (int fd, const char *path, __u32 mask);
        int inotify_rm_watch (int fd, __u32 mask);

int fd = inotify_init ();
int wd = inotify_add_watch (fd, path, mask);

           struct inotify_event {
               int      wd;       /* Watch descriptor */
               uint32_t mask;     /* Mask of events */
               uint32_t cookie;   /* Unique cookie associating related
                                     events (for rename(2)) */
               uint32_t len;      /* Size of name field */
               char     name[];   /* Optional null-terminated name */
           };

           IN_ACCESS         File was accessed (read) (*).
           IN_ATTRIB         Metadata  changed,  e.g.,  permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID,
                             GID, etc. (*).
           IN_CLOSE_WRITE    File opened for writing was closed (*).
           IN_CLOSE_NOWRITE  File not opened for writing was closed (*).
           IN_CREATE         File/directory created in watched directory (*).
           IN_DELETE         File/directory deleted from watched directory (*).
           IN_DELETE_SELF    Watched file/directory was itself deleted.
           IN_MODIFY         File was modified (*).
           IN_MOVE_SELF      Watched file/directory was itself moved.
           IN_MOVED_FROM     File moved out of watched directory (*).
           IN_MOVED_TO       File moved into watched directory (*).
           IN_OPEN           File was opened (*).


    struct inotify_handle *ih = inotify_init(my_event_handler);
    void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
                      u32 cookie, const char *name, struct inode *inode)

        watch - the pointer to the inotify_watch that triggered this call
        wd - the watch descriptor
        mask - describes the event that occurred
        cookie - an identifier for synchronizing events
        name - the dentry name for affected files in a directory-based event
        inode - the affected inode in a directory-based event

    void destroy_watch(struct inotify_watch *watch)

LecturesCMC/LinuxApplicationDevelopment2012/10 (последним исправлял пользователь FrBrGeorge 2013-05-06 14:06:58)