18877df94SMatthew Dillon.\" Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 28877df94SMatthew Dillon.\" 38877df94SMatthew Dillon.\" This code is derived from software contributed to The DragonFly Project 48877df94SMatthew Dillon.\" by Matthew Dillon <dillon@backplane.com> 58877df94SMatthew Dillon.\" 68877df94SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without 78877df94SMatthew Dillon.\" modification, are permitted provided that the following conditions 88877df94SMatthew Dillon.\" are met: 98877df94SMatthew Dillon.\" 108877df94SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright 118877df94SMatthew Dillon.\" notice, this list of conditions and the following disclaimer. 128877df94SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright 138877df94SMatthew Dillon.\" notice, this list of conditions and the following disclaimer in 148877df94SMatthew Dillon.\" the documentation and/or other materials provided with the 158877df94SMatthew Dillon.\" distribution. 168877df94SMatthew Dillon.\" 3. Neither the name of The DragonFly Project nor the names of its 178877df94SMatthew Dillon.\" contributors may be used to endorse or promote products derived 188877df94SMatthew Dillon.\" from this software without specific, prior written permission. 198877df94SMatthew Dillon.\" 208877df94SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 218877df94SMatthew Dillon.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 228877df94SMatthew Dillon.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 238877df94SMatthew Dillon.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 248877df94SMatthew Dillon.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 258877df94SMatthew Dillon.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 268877df94SMatthew Dillon.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 278877df94SMatthew Dillon.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 288877df94SMatthew Dillon.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 298877df94SMatthew Dillon.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 308877df94SMatthew Dillon.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318877df94SMatthew Dillon.\" SUCH DAMAGE. 328877df94SMatthew Dillon.\" 33*fb49adadSSascha Wildner.\" $DragonFly: src/lib/libc/sys/sys_checkpoint.2,v 1.8 2007/06/30 19:20:48 swildner Exp $ 348877df94SMatthew Dillon.\" 35*fb49adadSSascha Wildner.Dd June 29, 2007 368877df94SMatthew Dillon.Dt SYS_CHECKPOINT 2 378877df94SMatthew Dillon.Os 388877df94SMatthew Dillon.Sh NAME 398877df94SMatthew Dillon.Nm sys_checkpoint 40c1de475dSHiten Pandya.Nd checkpoint or restore a process 418877df94SMatthew Dillon.Sh LIBRARY 428877df94SMatthew Dillon.Lb libc 438877df94SMatthew Dillon.Sh SYNOPSIS 448877df94SMatthew Dillon.In sys/types.h 458877df94SMatthew Dillon.In sys/checkpoint.h 468877df94SMatthew Dillon.Ft int 478877df94SMatthew Dillon.Fn sys_checkpoint "int type" "int fd" "pid_t pid" "int retval" 488877df94SMatthew Dillon.Sh DESCRIPTION 49c1de475dSHiten PandyaThe 50c1de475dSHiten Pandya.Fn sys_checkpoint 51c1de475dSHiten Pandyasystem call executes a checkpoint function as specified by 528877df94SMatthew Dillon.Fa type . 538877df94SMatthew DillonSupported types are as follows: 548877df94SMatthew Dillon.Pp 55*fb49adadSSascha Wildner.Bl -tag -width ".Dv CKPT_FREEZE" -offset indent 568877df94SMatthew Dillon.It Dv CKPT_FREEZE 578877df94SMatthew DillonGenerate a checkpoint file. 588877df94SMatthew DillonCurrently 598877df94SMatthew Dillon.Fa pid 608877df94SMatthew Dillonmust be -1 or the pid of the current process. 618877df94SMatthew DillonThe checkpoint file will be written out to 628877df94SMatthew Dillon.Fa fd , 638877df94SMatthew Dillonand 648877df94SMatthew Dillon.Fa retval 658877df94SMatthew Dillonis unused but must be specified as -1. 668877df94SMatthew DillonAs a special case, if 678877df94SMatthew Dillon.Fa pid 688877df94SMatthew Dillonand 698877df94SMatthew Dillon.Fa fd 708877df94SMatthew Dillonare both specified as -1, the system will generate a checkpoint file 718877df94SMatthew Dillonusing the system checkpoint template. 728877df94SMatthew Dillon.Pp 738877df94SMatthew DillonThis function returns 0 on success, -1 on error, and typically 1 74*fb49adadSSascha Wildneron resume. 75*fb49adadSSascha WildnerThe value returned on resume is controlled by the 768877df94SMatthew Dillon.Fa retval 778877df94SMatthew Dillonargument passed to 788877df94SMatthew Dillon.Fn sys_checkpoint 79*fb49adadSSascha Wildnerwhen resuming a checkpoint file. 80*fb49adadSSascha WildnerA user program which installs its own 818877df94SMatthew Dillon.Dv SIGCKPT 828877df94SMatthew Dillonsignal handler and calls 838877df94SMatthew Dillon.Fn sys_checkpoint 848877df94SMatthew Dillonmanually thus has control over both termination/continuance and 858877df94SMatthew Dillonresumption. 868877df94SMatthew Dillon.It Dv CKPT_THAW 878877df94SMatthew DillonRestore a checkpointed program. 88c1de475dSHiten PandyaThe 89c1de475dSHiten Pandya.Fa pid 908877df94SMatthew Dillonmust be specified as -1, and 918877df94SMatthew Dillon.Fa fd 928877df94SMatthew Dillonrepresents the checkpoint file. 93c1de475dSHiten PandyaThe 94c1de475dSHiten Pandya.Fa retval 95c1de475dSHiten Pandyaspecifies the value returned to the resumed program if 968877df94SMatthew Dillon.Fn sys_checkpoint 97c1de475dSHiten Pandyawas called directly. 988877df94SMatthew Dillon.Pp 998877df94SMatthew DillonThe checkpointed program will replace the current program, similar to 100*fb49adadSSascha Wildneran 101*fb49adadSSascha Wildner.Xr exec 3 102*fb49adadSSascha Wildnercall. 1038877df94SMatthew Dillon.El 1048877df94SMatthew Dillon.Sh RETURN VALUES 105*fb49adadSSascha WildnerUpon successful completion, the value 0 is typically returned. 106*fb49adadSSascha WildnerA checkpoint being resumed typically returns a positive value; 107*fb49adadSSascha Wildnerotherwise the value -1 is returned and the global variable 1080f09e575SSascha Wildner.Va errno 109c1de475dSHiten Pandyais set to indicate the error. 1108877df94SMatthew Dillon.Sh EXAMPLE 111c1de475dSHiten Pandya.Bd -literal -offset indent -compact 1128877df94SMatthew Dillon/* 113c1de475dSHiten Pandya * Demonstrate checkpointing. Use control-E to checkpoint 114c1de475dSHiten Pandya * the program and 'checkpt -r x.ckpt' to resume it. 1158877df94SMatthew Dillon */ 1168877df94SMatthew Dillon#include <sys/types.h> 1178877df94SMatthew Dillon#include <sys/signal.h> 1188877df94SMatthew Dillon#include <sys/checkpoint.h> 1198877df94SMatthew Dillon#include <stdio.h> 1208877df94SMatthew Dillon#include <unistd.h> 1218877df94SMatthew Dillon#include <fcntl.h> 1228877df94SMatthew Dillon#include <errno.h> 1238877df94SMatthew Dillon 1248877df94SMatthew Dillonvoid docheckpoint(void); 1258877df94SMatthew Dillon 1268877df94SMatthew Dillonint wantckpt; 1278877df94SMatthew Dillon 1288877df94SMatthew Dillonvoid 1298877df94SMatthew Dillondockpt(int sig) 1308877df94SMatthew Dillon{ 1318877df94SMatthew Dillon wantckpt = 1; 1328877df94SMatthew Dillon} 1338877df94SMatthew Dillon 1348877df94SMatthew Dillonint 1358877df94SMatthew Dillonmain(int argc, char** argv) 1368877df94SMatthew Dillon{ 1378877df94SMatthew Dillon int i = 0; 1388877df94SMatthew Dillon 1398877df94SMatthew Dillon signal(SIGCKPT, dockpt); 1408877df94SMatthew Dillon 1418877df94SMatthew Dillon for (;;) { 1428877df94SMatthew Dillon printf("iteration: %d\en", i); 1438877df94SMatthew Dillon ++i; 1448877df94SMatthew Dillon sleep(1); 1458877df94SMatthew Dillon if (wantckpt) { 1468877df94SMatthew Dillon wantckpt = 0; 1478877df94SMatthew Dillon printf("Checkpoint requested\en"); 1488877df94SMatthew Dillon docheckpoint(); 1498877df94SMatthew Dillon } 1508877df94SMatthew Dillon } 1518877df94SMatthew Dillon return(0); 1528877df94SMatthew Dillon} 1538877df94SMatthew Dillon 1548877df94SMatthew Dillonvoid 1558877df94SMatthew Dillondocheckpoint(void) 1568877df94SMatthew Dillon{ 1578877df94SMatthew Dillon int ret; 1588877df94SMatthew Dillon int fd; 1598877df94SMatthew Dillon 1608877df94SMatthew Dillon fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666); 1618877df94SMatthew Dillon if (fd < 0) { 162c1de475dSHiten Pandya printf("unable to create checkpoint file: %s\en", 163c1de475dSHiten Pandya strerror(errno)); 1648877df94SMatthew Dillon return; 1658877df94SMatthew Dillon } 1668877df94SMatthew Dillon 1678877df94SMatthew Dillon ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1); 1688877df94SMatthew Dillon if (ret < 0) { 169c1de475dSHiten Pandya printf("unable to checkpoint: %s\en", 170c1de475dSHiten Pandya strerror(errno)); 1718877df94SMatthew Dillon } else if (ret == 0) { 1728877df94SMatthew Dillon printf("checkpoint successful, continuing\en"); 1738877df94SMatthew Dillon } else if (ret == 1) { 1748877df94SMatthew Dillon printf("resuming from checkpoint.\en"); 1758877df94SMatthew Dillon } else { 1768877df94SMatthew Dillon printf("unknown return value %d from sys_checkpoint\en", ret); 1778877df94SMatthew Dillon exit(1); 1788877df94SMatthew Dillon } 1798877df94SMatthew Dillon /* note that the file descriptor is still valid on a resume */ 1808877df94SMatthew Dillon close(fd); 1818877df94SMatthew Dillon} 1828877df94SMatthew Dillon.Ed 1830b84df5cSSascha Wildner.Sh ERRORS 1840b84df5cSSascha Wildner.Bl -tag -width Er 1850b84df5cSSascha Wildner.It Bq Er EBADF 1860b84df5cSSascha WildnerThe given 1870b84df5cSSascha Wildner.Fa fd 188*fb49adadSSascha Wildneris not a valid regular file, socket descriptor, or pipe. 189*fb49adadSSascha WildnerNote that not 1900b84df5cSSascha Wildnerall systems necessarily support checkpointing to sockets and pipes. 1910b84df5cSSascha Wildner.It Bq Er EPERM 1920b84df5cSSascha WildnerThe caller does not have permission to issue the checkpoint command. 1930b84df5cSSascha WildnerCheckpointing may be restricted or disabled using sysctls. 1940b84df5cSSascha Wildner.It Bq Er EIO 1950b84df5cSSascha WildnerAn I/O error occurred while reading from the file system. 1960b84df5cSSascha Wildner.It Bq Er EINVAL 1970b84df5cSSascha WildnerAn invalid parameter was specified. 1980b84df5cSSascha Wildner.El 1990b84df5cSSascha Wildner.Sh CHECKPOINT FEATURES 2000b84df5cSSascha WildnerThe system checkpointing code will save the process register state (including 2010b84df5cSSascha Wildnerfloating point registers), signal state, file descriptors representing 2020b84df5cSSascha Wildnerregular files or directories (anything that can be converted into a file 2030b84df5cSSascha Wildnerhandle for storage), and both shared and private memory mappings. 2040b84df5cSSascha WildnerPrivate, writable mappings are copied to the checkpoint file while shared 2050b84df5cSSascha Wildnermappings and stored by referencing the file handle and offset. 2060b84df5cSSascha WildnerNote that the system checkpointing code does not retain references to 2070b84df5cSSascha Wildnerdeleted files, so mappings and open descriptors of deleted files 2080b84df5cSSascha Wildnercannot be restored. 2090b84df5cSSascha WildnerUnpredictable operation will occur if a checkpoint-unaware program 2100b84df5cSSascha Wildneris restored and some of the underlying files mapped by the program 2110b84df5cSSascha Wildnerhave changed. 2120b84df5cSSascha Wildner.Pp 2130b84df5cSSascha WildnerThe system checkpointing code is not able to retain the process pid, process 214*fb49adadSSascha Wildnergroup, user/group creds, or descriptors 0, 1, and 2. 215*fb49adadSSascha WildnerThese will be inherited from whomever restores the checkpoint. 2160b84df5cSSascha Wildner.Pp 2170b84df5cSSascha WildnerWhen a checkpointed program is restored modified private mappings will 2180b84df5cSSascha Wildnerbe mapped from the checkpoint file itself, but major portions of the 2190b84df5cSSascha Wildneroriginal program binary will be mapped from the original program binary. 2200b84df5cSSascha WildnerIf the resumed program is checkpointed again the system will automatically 2210b84df5cSSascha Wildnercopy any mappings from the original checkpoint file to the new one, since 2220b84df5cSSascha Wildnerthe original is likely being replaced. 2230b84df5cSSascha WildnerThe caller must not truncate the existing checkpoint file when creating 2240b84df5cSSascha Wildnera new one or specify the existing file's file descriptor as the new 2250b84df5cSSascha Wildnerone as this will destroy the data that the checkpoint operation needs 226*fb49adadSSascha Wildnerto copy to the new file. 227*fb49adadSSascha WildnerIt is best to checkpoint to a new file and then rename-over the old, or to 228*fb49adadSSascha Wildner.Xr remove 3 229*fb49adadSSascha Wildnerthe old file before creating the new 2309caa806aSMatthew Dillonone so it remains valid as long as the program continues to run. 2319caa806aSMatthew Dillon.Pp 232*fb49adadSSascha WildnerThreaded programs cannot currently be checkpointed. 233*fb49adadSSascha WildnerThe program must be 2349caa806aSMatthew Dillonreduced to a single thread before it can be safely checkpointed. 2359caa806aSMatthew Dillon.Pp 236*fb49adadSSascha Wildner.Dv MAP_VPAGETABLE 237*fb49adadSSascha Wildnermappings cannot currently be checkpointed. 238*fb49adadSSascha WildnerA program must restore such mappings manually on resumption. 239*fb49adadSSascha WildnerOnly regular file and 240*fb49adadSSascha Wildneranonymous memory mappings are checkpointed and restored. 241*fb49adadSSascha WildnerDevice and other special mappings are not. 242*fb49adadSSascha WildnerOnly regular file descriptors are checkpointed and restored. 243*fb49adadSSascha WildnerDevices, pipes, sockets, and other special descriptors are not. 244*fb49adadSSascha WildnerMemory wiring states are not checkpointed or restored. 245*fb49adadSSascha Wildner.Xr madvise 2 246*fb49adadSSascha Wildnerstates are not checkpointed or restored. 247*fb49adadSSascha WildnerBasic mapping permissions are checkpointed and restored. 2489caa806aSMatthew Dillon.Pp 2490b84df5cSSascha Wildner.Sh SECURITY 2500b84df5cSSascha WildnerThe sysctl 251*fb49adadSSascha Wildner.Va kern.ckptgroup 2520b84df5cSSascha Wildnercontrols which group can use system checkpointing. 2530b84df5cSSascha WildnerBy default, only users in the 2540b84df5cSSascha Wildner.Ql wheel 2550b84df5cSSascha Wildnergroup are allowed to checkpoint and restore processes. 2560b84df5cSSascha WildnerTo allow users in any group to have this capability (risky), set sysctl 257*fb49adadSSascha Wildner.Va kern.ckptgroup 2580b84df5cSSascha Wildnerto -1. 2590b84df5cSSascha Wildner.Sh SIGNALS 2600b84df5cSSascha WildnerTwo signals are associated with checkpointing. 2610b84df5cSSascha Wildner.Dv SIGCKPT 262*fb49adadSSascha Wildneris delivered via the tty ckpt character, usually control-E. 263*fb49adadSSascha WildnerIts default action is to checkpoint a program and continue running it. 264*fb49adadSSascha WildnerThe 2650b84df5cSSascha Wildner.Dv SIGCKPTEXIT 2660b84df5cSSascha Wildnersignal can only be delivered by 2670b84df5cSSascha Wildner.Xr kill 2 . 2680b84df5cSSascha WildnerIts default action is to checkpoint a program and then exit. 2690b84df5cSSascha Wildner.Dv SIGCKPTEXIT 270*fb49adadSSascha Wildnermight not be implemented by the system. 271*fb49adadSSascha WildnerBoth signals are defined to 2720b84df5cSSascha Wildnerbe greater or equal to signal 32 and cannot be manipulated using legacy 2730b84df5cSSascha Wildnermasking functions. 2740b84df5cSSascha Wildner.Pp 2750b84df5cSSascha WildnerIf a program overrides the default action for a checkpoint signal the 276*fb49adadSSascha Wildnersystem will not undertake any action of its own. 277*fb49adadSSascha WildnerThe program may issue 2780b84df5cSSascha Wildnerthe checkpoint command from the signal handler itself or simply set a 279*fb49adadSSascha Wildnerreminder for later action. 280*fb49adadSSascha WildnerIt is usually safest to set a reminder and 2810b84df5cSSascha Wildnerdo the actual checkpointing from your main loop. 2828877df94SMatthew Dillon.Sh SEE ALSO 283c1de475dSHiten Pandya.Xr checkpt 1 , 284c1de475dSHiten Pandya.Xr signal 3 2858877df94SMatthew Dillon.Sh HISTORY 2868877df94SMatthew DillonThe 2878877df94SMatthew Dillon.Fn sys_checkpoint 288*fb49adadSSascha Wildnerfunction call appeared in 289a3220ac5SSascha Wildner.Dx 1.1 . 290