<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://criu.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kkantor</id>
	<title>CRIU - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://criu.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kkantor"/>
	<link rel="alternate" type="text/html" href="https://criu.org/Special:Contributions/Kkantor"/>
	<updated>2026-05-13T16:14:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.6</generator>
	<entry>
		<id>https://criu.org/index.php?title=C_API&amp;diff=3025</id>
		<title>C API</title>
		<link rel="alternate" type="text/html" href="https://criu.org/index.php?title=C_API&amp;diff=3025"/>
		<updated>2016-08-23T02:07:17Z</updated>

		<summary type="html">&lt;p&gt;Kkantor: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''libcriu''' is a C API for CRIU, which is a simple wrapper around our [[RPC]]. Although you can use [[RPC]] directly, libcriu is a wrapper providing the interface that is much easier to use from C code. Note that [[RPC]] is supported in the first place, and if you want the most recent set of features you should probably use [[RPC]] directly.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
libcriu functions are defined in &amp;lt;code&amp;gt;lib/criu.h&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To create a library &amp;lt;code&amp;gt;lib/libcriu.so&amp;lt;/code&amp;gt;, run &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; in the main directory.&lt;br /&gt;
&lt;br /&gt;
{{Warning|The library is not thread- and fork- safe}}&lt;br /&gt;
&lt;br /&gt;
== Preparation ==&lt;br /&gt;
&lt;br /&gt;
=== init options ===&lt;br /&gt;
&lt;br /&gt;
Call &amp;lt;code&amp;gt;int criu_init_opts(void)&amp;lt;/code&amp;gt; to initialize request options.&lt;br /&gt;
&lt;br /&gt;
Note: it should be called before using any other functions from libcriu, except &amp;lt;code&amp;gt;criu_check()&amp;lt;/code&amp;gt;. Also you should use it to reinitialize options. It returns 0 on success and -1 on fail.&lt;br /&gt;
&lt;br /&gt;
=== set service address ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;void criu_set_service_address(char *address)&amp;lt;/code&amp;gt; to specify path to a CRIU service socket. Call it with NULL to make libcriu use the default address (currently &amp;lt;code&amp;gt;/var/run/criu_service.socket&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
=== set dump/restore options ===&lt;br /&gt;
Use &amp;lt;code&amp;gt;criu_set_*&amp;lt;/code&amp;gt; functions to setup dump/restore options.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void criu_set_pid(int pid);&lt;br /&gt;
void criu_set_images_dir_fd(int fd); /* must be set for dump/restore */&lt;br /&gt;
void criu_set_leave_running(bool leave_running);&lt;br /&gt;
void criu_set_ext_unix_sk(bool ext_unix_sk);&lt;br /&gt;
void criu_set_tcp_established(bool tcp_established);&lt;br /&gt;
void criu_set_evasive_devices(bool evasive_devices);&lt;br /&gt;
void criu_set_shell_job(bool shell_job);&lt;br /&gt;
void criu_set_file_locks(bool file_locks);&lt;br /&gt;
void criu_set_log_level(int log_level);&lt;br /&gt;
void criu_set_log_file(char *log_file);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If no pid is set on dump, CRIU will dump the calling process itself.&lt;br /&gt;
{{Note|If a calling process is not run as root, the whole process tree to be dumped must have the same uid, otherwise CRIU refuses to dump. See [[Usage#Security]].}}&lt;br /&gt;
&lt;br /&gt;
{{Note|&amp;lt;code&amp;gt;images_dir_fd&amp;lt;/code&amp;gt; is '''required''' at dump/restore, all other options might be left unset.&lt;br /&gt;
The client must open directory for/with images by itself and set &amp;lt;code&amp;gt;images_dir_fd&amp;lt;/code&amp;gt; to the opened directory fd.&lt;br /&gt;
CRIU will open &amp;lt;code&amp;gt;/proc/''client_pid''/fd/''images_dir_fd''&amp;lt;/code&amp;gt;, so it will work even if the client is in another namespace.}}&lt;br /&gt;
&lt;br /&gt;
The logic of setting request is the same as when setting options in console. Here is an example:&lt;br /&gt;
&lt;br /&gt;
 # criu restore -D /path/to/imgs_dir -v4 -o restore.log&lt;br /&gt;
&lt;br /&gt;
is equal to:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
criu_init_opts();&lt;br /&gt;
criu_set_service_address(&amp;quot;/path/to/criu/service/socket&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
int fd = open(&amp;quot;/path/to/imgs_dir&amp;quot;, O_DIRECTORY);&lt;br /&gt;
criu_set_images_dir_fd(fd);&lt;br /&gt;
&lt;br /&gt;
criu_set_log_file(&amp;quot;restore.log&amp;quot;);&lt;br /&gt;
criu_set_log_level(4);&lt;br /&gt;
&lt;br /&gt;
criu_restore();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Operations ==&lt;br /&gt;
&lt;br /&gt;
Use the following functions to perform CRIU actions.&lt;br /&gt;
&lt;br /&gt;
=== check ===&lt;br /&gt;
&lt;br /&gt;
 int criu_check(void);&lt;br /&gt;
&lt;br /&gt;
=== dump ===&lt;br /&gt;
&lt;br /&gt;
 int criu_dump(void);&lt;br /&gt;
&lt;br /&gt;
=== restore ===&lt;br /&gt;
&lt;br /&gt;
 int criu_restore(void);&lt;br /&gt;
&lt;br /&gt;
=== restore as a child ===&lt;br /&gt;
&lt;br /&gt;
This one is special. It will fork and exec criu swrk (Service WoRKer) to allow restoring process as a caller child. Calling &amp;quot;exec&amp;quot; implies some restrictions, as, for example, one should make sure, that criu binary is present in PATH and has suid bit set (see [[Usage#Security]]).&lt;br /&gt;
&lt;br /&gt;
 int criu_restore_child(void);&lt;br /&gt;
&lt;br /&gt;
=== Return values ===&lt;br /&gt;
&lt;br /&gt;
Here is a table of return and errno values of the above functions:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Return value&lt;br /&gt;
! errno&lt;br /&gt;
! Description&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 0&lt;br /&gt;
| undefined&lt;br /&gt;
| Success&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;gt;0&lt;br /&gt;
| undefined&lt;br /&gt;
| Success (&amp;lt;code&amp;gt;criu_restore()&amp;lt;/code&amp;gt; only)&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| -EBADE&lt;br /&gt;
| RPC error (if provided(see [https://github.com/xemul/criu/blob/master/include/cr-errno.h#L8 include/cr-errno.h]), 0 otherwise)&lt;br /&gt;
| RPC has returned fail.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| -ECONNREFUSED&lt;br /&gt;
| errno&lt;br /&gt;
| Unable to connect to CRIU.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| -ECOMM&lt;br /&gt;
| errno&lt;br /&gt;
| Unable to send/recv msg to/from CRIU.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| -EINVAL&lt;br /&gt;
| undefined&lt;br /&gt;
| CRIU doesn't support this type of request. You should probably update CRIU.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| -EBADMSG&lt;br /&gt;
| undefined&lt;br /&gt;
| Unexpected response from CRIU. You should probably update CRIU.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
You could find example of using libcriu at [https://github.com/xemul/criu/tree/master/test/others/libcriu test/others/ibcriu].&lt;br /&gt;
&lt;br /&gt;
[[Category: API]]&lt;/div&gt;</summary>
		<author><name>Kkantor</name></author>
	</entry>
</feed>