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*0b84df5cSSascha Wildner.\" $DragonFly: src/lib/libc/sys/sys_checkpoint.2,v 1.5 2006/02/17 19:35:06 swildner Exp $ 348877df94SMatthew Dillon.\" 358877df94SMatthew Dillon.Dd November 22, 2004 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 55c1de475dSHiten Pandya.Bl -tag -width 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 748877df94SMatthew Dillonon resume. The value returned on resume is controlled by the 758877df94SMatthew Dillon.Fa retval 768877df94SMatthew Dillonargument passed to 778877df94SMatthew Dillon.Fn sys_checkpoint 788877df94SMatthew Dillonwhen resuming a checkpoint file. A user program which installs its 798877df94SMatthew Dillonown 808877df94SMatthew Dillon.Dv SIGCKPT 818877df94SMatthew Dillonsignal handler and calls 828877df94SMatthew Dillon.Fn sys_checkpoint 838877df94SMatthew Dillonmanually thus has control over both termination/continuance and 848877df94SMatthew Dillonresumption. 858877df94SMatthew Dillon.It Dv CKPT_THAW 868877df94SMatthew DillonRestore a checkpointed program. 87c1de475dSHiten PandyaThe 88c1de475dSHiten Pandya.Fa pid 898877df94SMatthew Dillonmust be specified as -1, and 908877df94SMatthew Dillon.Fa fd 918877df94SMatthew Dillonrepresents the checkpoint file. 92c1de475dSHiten PandyaThe 93c1de475dSHiten Pandya.Fa retval 94c1de475dSHiten Pandyaspecifies the value returned to the resumed program if 958877df94SMatthew Dillon.Fn sys_checkpoint 96c1de475dSHiten Pandyawas called directly. 978877df94SMatthew Dillon.Pp 988877df94SMatthew DillonThe checkpointed program will replace the current program, similar to 998877df94SMatthew Dillonan exec call. 1008877df94SMatthew Dillon.El 1018877df94SMatthew Dillon.Sh RETURN VALUES 1028877df94SMatthew DillonUpon successful completion, the value 0 is typically returned. A checkpoint 1038877df94SMatthew Dillonbeing resumed typically returns a positive value; otherwise the value -1 104c1de475dSHiten Pandyais returned and the global variable 105c1de475dSHiten Pandya.Li errno 106c1de475dSHiten Pandyais set to indicate the error. 1078877df94SMatthew Dillon.Sh EXAMPLE 108c1de475dSHiten Pandya.Bd -literal -offset indent -compact 1098877df94SMatthew Dillon 1108877df94SMatthew Dillon/* 111c1de475dSHiten Pandya * Demonstrate checkpointing. Use control-E to checkpoint 112c1de475dSHiten Pandya * the program and 'checkpt -r x.ckpt' to resume it. 1138877df94SMatthew Dillon */ 1148877df94SMatthew Dillon#include <sys/types.h> 1158877df94SMatthew Dillon#include <sys/signal.h> 1168877df94SMatthew Dillon#include <sys/checkpoint.h> 1178877df94SMatthew Dillon#include <stdio.h> 1188877df94SMatthew Dillon#include <unistd.h> 1198877df94SMatthew Dillon#include <fcntl.h> 1208877df94SMatthew Dillon#include <errno.h> 1218877df94SMatthew Dillon 1228877df94SMatthew Dillonvoid docheckpoint(void); 1238877df94SMatthew Dillon 1248877df94SMatthew Dillonint wantckpt; 1258877df94SMatthew Dillon 1268877df94SMatthew Dillonvoid 1278877df94SMatthew Dillondockpt(int sig) 1288877df94SMatthew Dillon{ 1298877df94SMatthew Dillon wantckpt = 1; 1308877df94SMatthew Dillon} 1318877df94SMatthew Dillon 1328877df94SMatthew Dillonint 1338877df94SMatthew Dillonmain(int argc, char** argv) 1348877df94SMatthew Dillon{ 1358877df94SMatthew Dillon int i = 0; 1368877df94SMatthew Dillon 1378877df94SMatthew Dillon signal(SIGCKPT, dockpt); 1388877df94SMatthew Dillon 1398877df94SMatthew Dillon for (;;) { 1408877df94SMatthew Dillon printf("iteration: %d\en", i); 1418877df94SMatthew Dillon ++i; 1428877df94SMatthew Dillon sleep(1); 1438877df94SMatthew Dillon if (wantckpt) { 1448877df94SMatthew Dillon wantckpt = 0; 1458877df94SMatthew Dillon printf("Checkpoint requested\en"); 1468877df94SMatthew Dillon docheckpoint(); 1478877df94SMatthew Dillon } 1488877df94SMatthew Dillon } 1498877df94SMatthew Dillon return(0); 1508877df94SMatthew Dillon} 1518877df94SMatthew Dillon 1528877df94SMatthew Dillonvoid 1538877df94SMatthew Dillondocheckpoint(void) 1548877df94SMatthew Dillon{ 1558877df94SMatthew Dillon int ret; 1568877df94SMatthew Dillon int fd; 1578877df94SMatthew Dillon 1588877df94SMatthew Dillon fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666); 1598877df94SMatthew Dillon if (fd < 0) { 160c1de475dSHiten Pandya printf("unable to create checkpoint file: %s\en", 161c1de475dSHiten Pandya strerror(errno)); 1628877df94SMatthew Dillon return; 1638877df94SMatthew Dillon } 1648877df94SMatthew Dillon 1658877df94SMatthew Dillon ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1); 1668877df94SMatthew Dillon if (ret < 0) { 167c1de475dSHiten Pandya printf("unable to checkpoint: %s\en", 168c1de475dSHiten Pandya strerror(errno)); 1698877df94SMatthew Dillon } else if (ret == 0) { 1708877df94SMatthew Dillon printf("checkpoint successful, continuing\en"); 1718877df94SMatthew Dillon } else if (ret == 1) { 1728877df94SMatthew Dillon printf("resuming from checkpoint.\en"); 1738877df94SMatthew Dillon } else { 1748877df94SMatthew Dillon printf("unknown return value %d from sys_checkpoint\en", ret); 1758877df94SMatthew Dillon exit(1); 1768877df94SMatthew Dillon } 1778877df94SMatthew Dillon /* note that the file descriptor is still valid on a resume */ 1788877df94SMatthew Dillon close(fd); 1798877df94SMatthew Dillon} 1808877df94SMatthew Dillon.Ed 181*0b84df5cSSascha Wildner.Sh ERRORS 182*0b84df5cSSascha Wildner.Bl -tag -width Er 183*0b84df5cSSascha Wildner.It Bq Er EBADF 184*0b84df5cSSascha WildnerThe given 185*0b84df5cSSascha Wildner.Fa fd 186*0b84df5cSSascha Wildneris not a valid regular file, socket descriptor, or pipe. Note that not 187*0b84df5cSSascha Wildnerall systems necessarily support checkpointing to sockets and pipes. 188*0b84df5cSSascha Wildner.It Bq Er EPERM 189*0b84df5cSSascha WildnerThe caller does not have permission to issue the checkpoint command. 190*0b84df5cSSascha WildnerCheckpointing may be restricted or disabled using sysctls. 191*0b84df5cSSascha Wildner.It Bq Er EIO 192*0b84df5cSSascha WildnerAn I/O error occurred while reading from the file system. 193*0b84df5cSSascha Wildner.It Bq Er EINVAL 194*0b84df5cSSascha WildnerAn invalid parameter was specified. 195*0b84df5cSSascha Wildner.El 196*0b84df5cSSascha Wildner.Sh CHECKPOINT FEATURES 197*0b84df5cSSascha WildnerThe system checkpointing code will save the process register state (including 198*0b84df5cSSascha Wildnerfloating point registers), signal state, file descriptors representing 199*0b84df5cSSascha Wildnerregular files or directories (anything that can be converted into a file 200*0b84df5cSSascha Wildnerhandle for storage), and both shared and private memory mappings. 201*0b84df5cSSascha WildnerPrivate, writable mappings are copied to the checkpoint file while shared 202*0b84df5cSSascha Wildnermappings and stored by referencing the file handle and offset. 203*0b84df5cSSascha WildnerNote that the system checkpointing code does not retain references to 204*0b84df5cSSascha Wildnerdeleted files, so mappings and open descriptors of deleted files 205*0b84df5cSSascha Wildnercannot be restored. 206*0b84df5cSSascha WildnerUnpredictable operation will occur if a checkpoint-unaware program 207*0b84df5cSSascha Wildneris restored and some of the underlying files mapped by the program 208*0b84df5cSSascha Wildnerhave changed. 209*0b84df5cSSascha Wildner.Pp 210*0b84df5cSSascha WildnerThe system checkpointing code is not able to retain the process pid, process 211*0b84df5cSSascha Wildnergroup, user/group creds, or descriptors 0, 1, and 2. These will be inherited 212*0b84df5cSSascha Wildnerfrom whomever restores the checkpoint. 213*0b84df5cSSascha Wildner.Pp 214*0b84df5cSSascha WildnerWhen a checkpointed program is restored modified private mappings will 215*0b84df5cSSascha Wildnerbe mapped from the checkpoint file itself, but major portions of the 216*0b84df5cSSascha Wildneroriginal program binary will be mapped from the original program binary. 217*0b84df5cSSascha WildnerIf the resumed program is checkpointed again the system will automatically 218*0b84df5cSSascha Wildnercopy any mappings from the original checkpoint file to the new one, since 219*0b84df5cSSascha Wildnerthe original is likely being replaced. 220*0b84df5cSSascha WildnerThe caller must not truncate the existing checkpoint file when creating 221*0b84df5cSSascha Wildnera new one or specify the existing file's file descriptor as the new 222*0b84df5cSSascha Wildnerone as this will destroy the data that the checkpoint operation needs 223*0b84df5cSSascha Wildnerto copy to the new file. 224*0b84df5cSSascha Wildner.Sh SECURITY 225*0b84df5cSSascha WildnerThe sysctl 226*0b84df5cSSascha Wildner.Em kern.ckptgroup 227*0b84df5cSSascha Wildnercontrols which group can use system checkpointing. 228*0b84df5cSSascha WildnerBy default, only users in the 229*0b84df5cSSascha Wildner.Ql wheel 230*0b84df5cSSascha Wildnergroup are allowed to checkpoint and restore processes. 231*0b84df5cSSascha WildnerTo allow users in any group to have this capability (risky), set sysctl 232*0b84df5cSSascha Wildner.Em kern.ckptgroup 233*0b84df5cSSascha Wildnerto -1. 234*0b84df5cSSascha Wildner.Sh SIGNALS 235*0b84df5cSSascha WildnerTwo signals are associated with checkpointing. 236*0b84df5cSSascha Wildner.Dv SIGCKPT 237*0b84df5cSSascha Wildneris delivered via the tty ckpt character, usually control-E. Its default 238*0b84df5cSSascha Wildneraction is to checkpoint a program and continue running it. The 239*0b84df5cSSascha Wildner.Dv SIGCKPTEXIT 240*0b84df5cSSascha Wildnersignal can only be delivered by 241*0b84df5cSSascha Wildner.Xr kill 2 . 242*0b84df5cSSascha WildnerIts default action is to checkpoint a program and then exit. 243*0b84df5cSSascha Wildner.Dv SIGCKPTEXIT 244*0b84df5cSSascha Wildnermight not be implemented by the system. Both signals are defined to 245*0b84df5cSSascha Wildnerbe greater or equal to signal 32 and cannot be manipulated using legacy 246*0b84df5cSSascha Wildnermasking functions. 247*0b84df5cSSascha Wildner.Pp 248*0b84df5cSSascha WildnerIf a program overrides the default action for a checkpoint signal the 249*0b84df5cSSascha Wildnersystem will not undertake any action of its own. The program may issue 250*0b84df5cSSascha Wildnerthe checkpoint command from the signal handler itself or simply set a 251*0b84df5cSSascha Wildnerreminder for later action. It is usually safest to set a reminder and 252*0b84df5cSSascha Wildnerdo the actual checkpointing from your main loop. 2538877df94SMatthew Dillon.Sh SEE ALSO 254c1de475dSHiten Pandya.Xr checkpt 1 , 255c1de475dSHiten Pandya.Xr signal 3 2568877df94SMatthew Dillon.Sh HISTORY 2578877df94SMatthew DillonThe 2588877df94SMatthew Dillon.Fn sys_checkpoint 2598877df94SMatthew Dillonfunction call 2608877df94SMatthew Dillonappeared in 261a3220ac5SSascha Wildner.Dx 1.1 . 262