Linux-specific

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


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 (last edited 2013-05-06 11:06:58 by FrBrGeorge)