introduction to tizen platform -...
TRANSCRIPT
36
1
Embedded Software Lab.
Sungkyunkwan University
Embedded Software Lab.
Dongkun Shin
전공핵심실습1:운영체제론Chapter 6. Inter-process Communication
(IPC)
36
2
Embedded Software Lab.
• Linux Kernel IPC– Pipe
– FIFO
– System V IPC
• Semaphore
• Message Queue
• Shared Memory
• Tizen Platform IPC– D-Bus
– vconf
Contents
36
3
Embedded Software Lab.
• Pipe
• FIFO
• System V IPC– Semaphore
– Message Queue
– Shared Memory
Linux Kernel IPC
36
4
Embedded Software Lab.
• User uses IPC mechanism via system calls provided by the kernel
IPC System Call
Linux Kernel
IPC System Call
PIPE (42)PIPE2 (359)
OPEN (5)READ (3)WRITE (4)
SEMGET (299)SEMOP (298)
MSGGET (303)MSGSND (301)MSGRCV (302)
SHMGET (307)SHMAT (305)SHMDT (306)
Tizen IPC System Calls (# syscall number)
PIPE Semaphore Message Queue Shared MemoryFIFO
36
5
Embedded Software Lab.
• The oldest form of UNIX IPC and provided by all unix systems
• Two limitations– Half-duplex : data flows only in one direction
– Can be used only between processes that have a common ancestor
• Usually used between the parent and child processes
• int pipe (int fd[2]);– Two file descriptors are returned through the fd argument
• fd[0] : open for reading
• fd[1] : open for writing
– The output of fd[1] is the input for fd[0]
PIPE
36
6
Embedded Software Lab.
PIPE (cont.)
36
7
Embedded Software Lab.
PIPE (cont.)
• Kernel Implementation (linux/fs/pipe.c)
SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags){
struct file *files[2];int fd[2];int error;
error = __do_pipe_flags(fd, files, flags);if (!error) {
if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {fput(files[0]);fput(files[1]);put_unused_fd(fd[0]);put_unused_fd(fd[1]);error = -EFAULT;
} else {fd_install(fd[0], files[0]);fd_install(fd[1], files[1]);
}}return error;
}
create 2 pipe files (later in fifo) and open with O_RDONLY and O_WRONLY flags
Copy file descriptors to userspace as an output of the system call
Install file *s in the file descriptor table of the process
36
8
Embedded Software Lab.
• Unrelated processes can exchange data, whereas pipes can be used only between related processes
• FIFO is a type of file. (S_ISFIFO macro against st_mode)
– FIFO is handled by file operations such as open(), close(), read(), and write(), once a FIFO created.
– Multiple readers/writers are allowed
• int mkfifo (const char *path, mode_t mode)– Generates FIFO with specific file name, path.
– Accesses the generated FIFO with open system calls between 2 processes
• open(“fifo1”, O_RDONLY) FIFO must have Reader / Writer both.
• open(“fifo1”, O_WRONLY)
FIFO (a.k.a named pipe)
36
9
Embedded Software Lab.
Create Pipe Files
• Create a directory entry
• Create an inode
• Plug pipefifo_ops to file_operations of the generated pipe or fifo files.
const struct file_operations pipefifo_fops = {.open = fifo_open,.llseek = no_llseek,.read = do_sync_read,.aio_read = pipe_read,.write = do_sync_write,.aio_write = pipe_write,.poll = pipe_poll,.unlocked_ioctl = pipe_ioctl,.release = pipe_release,.fasync = pipe_fasync,
};once pipefifo_fops is registered as file_operations,
the normal file I/O functions all work with FIFO
36
10
Embedded Software Lab.
• In Shell$ cat /proc/meminfo | grep –I active | tail –n4 > memory.txt
FIFO Example
36
11
Embedded Software Lab.
• Semaphore, Message Queue, and Shared Memory
• Semaphore– Synchronize itself with other processes
• Message Queue (Not equal to POSIX Message queue)
– Send or receive messages each other
• Shared Memory– Share a memory area
• System V IPCs shares same kernel data structures and operations (linux/ipc/util.h)– All IPC objects are stored in kernel memory
– They are referred to by IPC object identifier
– Kernel translate a given key to corresponding ID, whereas processes only can access the key
System V IPC
36
12
Embedded Software Lab.
• Key of IPC Resources– semget() – Get Semaphores IPC Resources
– msgget() – Get Messages IPC Resources
– shmget() – Get Share Memory IPC Resources
IPC Identifier
semget()msgget() + IPC Identifier
shmget() (XXX)
Process1 Process2
Some data for share
IPC resource
XXX
36
13
Embedded Software Lab.
IPC Resource (include/linux/ipc_namespace.h)
ipc_ids
represent
IPC Resources Type
kern_ipc_prem
represent
IPC Resource
kern_ipc_prem
represent
IPC Resource
kern_ipc_prem
represent
IPC Resource
struct idr ipcs_idr
The data structure similar with radix tree
to map a id to the corresponding pointer
36
14
Embedded Software Lab.
• Similar to the kernel semaphores (lock) – include/linux/semaphore.h, kernel/locking/semaphore.c
• Counters used to controlled access to shared data structures for multiple processes
Semaphore
Process
Some data for share
IPC resource
semget() : get IPC Identifier
semop() : Decrease or Increase semaphore count
semop( struct sembuf )
struct sembuf
sem_num : Semaphore number
sem_op : Add this to semval
(increase / decrease semaphore count)
sem_flg : Operation flags
36
15
Embedded Software Lab.
• Kernel Implementation
Semaphore (ipc/sem.c)
SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg){
struct ipc_namespace *ns;struct ipc_ops sem_ops;struct ipc_params sem_params;
ns = current->nsproxy->ipc_ns;
if (nsems < 0 || nsems > ns->sc_semmsl)return -EINVAL;
sem_ops.getnew = newary;sem_ops.associate = sem_security;sem_ops.more_checks = sem_more_checks;
sem_params.key = key;sem_params.flg = semflg;sem_params.u.nsems = nsems;
return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);}
newary() function allocates new ipc semaphore to ids as invoked via ipcget()
36
16
Embedded Software Lab.
newary() (ipc/sem.c)
ipc_ids
represent
IPC Resources Type
struct idr ipcs_idr
kern_ipc_prem
represent
IPC Resources
sem_array
represent
Semephore
kern_ipc_prem
represent
IPC Resources
sem_array
represent
Semephore
36
17
Embedded Software Lab.
• IPC Messages– msgget() : Create IPC Messages resources or get IPC identifier
– msgsnd() : Send messages
– msgrcv() : Receive messages
– POSIX Message Queue is implemented independently
Message Queue
Process1 Process2
Some data for share
IPC resource
XXX
msgsnd()
Buffer Data msgrcv()
Buffer Data
36
18
Embedded Software Lab.
• Kernel Implementation (ipc/msg.c)
Messages IPC Resources
SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg){
struct ipc_namespace *ns;struct ipc_ops msg_ops;struct ipc_params msg_params;
ns = current->nsproxy->ipc_ns;
msg_ops.getnew = newque;msg_ops.associate = msg_security;msg_ops.more_checks = NULL;
msg_params.key = key;msg_params.flg = msgflg;
return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);}
newqueue() function allocates new ipc message to ids as invoked via ipcget()
36
19
Embedded Software Lab.
newque() (ipc/msg.c)
ipc_ids
represent
IPC Resources Type
struct idr ipcs_idr
kern_ipc_prem
represent
IPC Resources
msg_queue
State
Messages
kern_ipc_prem
represent
IPC Resources
msg_queue
State
Messages
msg_queue
Messages
msg_queue
Messages
36
20
Embedded Software Lab.
• shmget() : Create IPC Share Memory resources or get IPC identifier
• shmat() : Map IPC Share Memory space for user process memory space
• shmdt() : Unmap IPC Share Memory space for user process memory space
Shared Memory
IPC Resource
0
0
Kernel
Data
0
Process1
shmat()shmdt()
Default : 32MBMax : 4096
0
0
Kernel
Data
0
Process2
36
21
Embedded Software Lab.
• Kernel Implementation (ipc/shm.c)
Shared Memory IPC Resources
SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg){
struct ipc_namespace *ns;struct ipc_ops shm_ops;struct ipc_params shm_params;
ns = current->nsproxy->ipc_ns;
shm_ops.getnew = newseg;shm_ops.associate = shm_security;shm_ops.more_checks = shm_more_checks;
shm_params.key = key;shm_params.flg = shmflg;shm_params.u.size = size;
return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);}
newseg() function allocates new shared memory to ids as invoked via ipcget()
36
22
Embedded Software Lab.
newseg() (ipc/shm.c)
ipc_ids
represent
IPC Resources Type
kern_ipc_prem
represent
IPC Resources
shmid_kernel
State
Share Memory
kern_ipc_prem
represent
IPC Resources
shmid_kernel
State
Share Memory
File Object File Object
struct idr ipcs_idr
36
23
Embedded Software Lab.
• POSIX IPC Mechanism– Same interface as System V IPC
– Implemented in independent pool from System V IPC
– Implemented not through key-identifier design but through file-based design
– Event-driven support (epoll event)
• Unix Domain Socket– General purpose IPC
• Socket supports not only IPC but also diverse network protocols.
– Simple code style, but high overhead compared to other IPC mechanisms.
Other IPCs in Linux
36
24
Embedded Software Lab.
• Various IPC methods are used in Tizen.– D-Bus
– vconf
– socket
– pipe
• Old Tizen (2.0, 2.1) used socket and pipe for IPC.
• Recent Tizen (2.3~, 3.x) is adopting dbus and vconffor IPC.– Socket D-Bus: system server, AUL, alarm manager,
connectivity manager, BlueZ, NFC manager, …
Tizen Platform IPC
36
25
Embedded Software Lab.
• Inter-process communication (IPC) mechanism using socket– Simplify the IPC requirements with
single shared channel
– Access control using SMACK
D-Bus
Fig. 1 IPC without D-Bus
Fig. 2 IPC with D-BusFig. 3 D-Bus and SMACK
36
26
Embedded Software Lab.
• Access control using SMACK (Simplified Mandatory Access Control Kernel)– dbus daemon allow setting the policy to apply using configuration files
explicitly
– Set allow or deny interface/destination
D-Bus Access Control
<busconfig><policy smack="bluez"><allow own="org.bluez"/>
</policy><policy smack="bt_agent::public">
<allow send_destination="org.bluez" send_interface="org.bluez.ProfileManager1" send_member="RegisterProfile1"/>
</policy></busconfig>
[Tizen 2.3]/etc/dubs-1/system.d/manifest.bluez.conf
36
27
Embedded Software Lab.
• User-level APIs and wrappers to use D-Bus– Low-level API
• DBusConnection
– Connection to another application
• DBusMessage
– Message to be sent or received over a DBusConnection
– D-Bus Wrapper
• EDBus
– D-Bus wrapper for EFL applications
• GDBus
– Glib based D-Bus wrapper
D-Bus in Tizen
36
28
Embedded Software Lab.
D-Bus Low-level API
Process 1 Process 2
dbus_bus_get () dbus_bus_get ()
dbus_bus_request_name ()
dbus_message_new_method_call()
dbus_message_append_args()
reply=dbus_connection_send_with_reply_and_block()
dbus_message_is_method_call()
dbus_message_get_args()
dbus_message_new_signal()
dbus_message_is_signal()
Init
MethodCall
Signal
36
29
Embedded Software Lab.
• Used in system framework and UI framework.
• Example: deviced (in system framework)
1. Initialize
• deviced/src/core/edbus-handler.c
EDBus Usage in Tizen
int init_pm_dbus(void){
ret = register_edbus_method(DEVICED_PATH_DISPLAY,
edbus_methods, ARRAY_SIZE(edbus_methods));
void edbus_init(void *data){...
edbus_request_name = e_dbus_request_name(edbus_conn, DEVICED_BUS_NAME,DBUS_NAME_FLAG_REPLACE_EXISTING, request_name_cb, NULL);
...}
static const struct edbus_method edbus_methods[] = {{ "start", NULL, NULL, edbus_start },{ "stop", NULL, NULL, edbus_stop },{ "lockstate", "sssi", "i", edbus_lockstate },{ "unlockstate", "ss", "i", edbus_unlockstate },
2. Register methods (Display device management in deviced)
• deviced/src/display/display-dbus.c
36
30
Embedded Software Lab.
• Example: deviced (in system framework) (Cont’d)3. Other process send message
• appfw/alarm-manager/alarm-manager.c
EDBus Usage in Tizen
static DBusHandlerResult message_filter(DBusConnection *connection, DBusMessage *message, void *data){
ret = dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,DBUS_TYPE_INVALID);
DD_LIST_FOREACH(edbus_watch_list, n, watch) {if (strcmp(arg, watch->name)) continue;if (watch->func)watch->func(watch->name, watch->id);
}
• Compare method name• Perform function call
int __display_lock_state(char *state, char *flag, unsigned int timeout){
msg = g_dbus_message_new_method_call(DEVICED_BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY, DEVICED_LOCK_STATE);
4. Receive and handle the message
• deviced/src/core/edbus-handler.c
36
31
Embedded Software Lab.
• Example: deviced (in system framework) (Cont’d)5. Perform specific function
EDBus Usage in Tizen
static DBusMessage *edbus_lockstate(E_DBus_Object *obj, DBusMessage *msg){
if (!dbus_message_get_args(msg, &err,DBUS_TYPE_STRING, &state_str,DBUS_TYPE_STRING, &option1_str,DBUS_TYPE_STRING, &option2_str,DBUS_TYPE_INT32, &timeout, DBUS_TYPE_INVALID)) {_E("there is no message");ret = -EINVAL;goto out;
}
if (!strcmp(state_str, PM_LCDON_STR))state = LCD_NORMAL;else if (!strcmp(state_str, PM_LCDDIM_STR))state = LCD_DIM;else if (!strcmp(state_str, PM_LCDOFF_STR))state = LCD_OFF;
① Get arguments from message
② Check LCD state
36
32
Embedded Software Lab.
• Key-value fair + inotify– Store system configuration using SQLite (libsqlfs)
– Able to communicate between inter-process using inotify
• Inotify (inode notify)– Linux kernel subsystem to notify filesystem’s event
• used to notify an event to application
vconf
36
33
Embedded Software Lab.
vconf
• vconftool– A tool to get or set the configuration values managed by vconf
– Example: Initialize Tizen device configuration values in deviced
• set -t <TYPE> <KEY NAME> <VALUE> <OPTIONS>
• Option
– -i : Install memory backend key into flash space for backup
– -f : Overwrite values by force, even when vconf values are already exist
– -r : retrieve all keys included in sub-directories
– -s : SMACK label
36
34
Embedded Software Lab.
• Vconf access is controlled by SMACK– Use one of predefined smack labels
– All modules(including applications) will have read permission to those files and only restricted modules have write permission
vconf Access Control
– Label list
• setting : only setting application can write
• inhouse : preloaded applications and platform modules can write
• privacy : only privacy related modules can write
• system : app framework, security, system, base
• multimedia : graphics & UI, multimedia
• network : web, connectivity
• misc : other keys
36
35
Embedded Software Lab.
• Get the values of Tizen device configuration1. Connect your Tizen Z3 phone
to your PC using micro-USB cable.
2. $ sdb root on
3. $ sdb shell
4. # vconftool get memory/
vconf Usage
…
…
36
36
Embedded Software Lab.
• Set the values of Tizen device configuration– Notify power-off button event
• # vconftool set -t int memory/sysman/power_off 1 -i -s system::vconf_system -f
– Notify earphone jack event
• # vconftool set -t int memory/sysman/earjack 1 -i -s system::vconf_system -f
– Control battery capacity as 1%
• # vconftool set -t int memory/sysman/battery_capacity 5 -i -s system::vconf_system -f
vconf Usage