Debian Codegraph @

docs/gsg_txn/C/enabletxn.html

raw
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Chapter 2. Enabling Transactions</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10 <link rel="up" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
11 <link rel="prev" href="perftune-intro.html" title="Performance Tuning" />
12 <link rel="next" href="envopen.html" title="Opening a Transactional Environment and Database" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Chapter 2. Enabling Transactions</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
25 <th width="60%" align="center"> </th>
26 <td width="20%" align="right"> <a accesskey="n" href="envopen.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="chapter" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title"><a id="enabletxn"></a>Chapter 2. Enabling Transactions</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <p>
41 <b>Table of Contents</b>
42 </p>
43 <dl>
44 <dt>
45 <span class="sect1">
46 <a href="enabletxn.html#environments">Environments</a>
47 </span>
48 </dt>
49 <dd>
50 <dl>
51 <dt>
52 <span class="sect2">
53 <a href="enabletxn.html#filenaming">File Naming</a>
54 </span>
55 </dt>
56 <dt>
57 <span class="sect2">
58 <a href="enabletxn.html#errorsupport">Error Support</a>
59 </span>
60 </dt>
61 <dt>
62 <span class="sect2">
63 <a href="enabletxn.html#sharedmemory">Shared Memory Regions</a>
64 </span>
65 </dt>
66 <dt>
67 <span class="sect2">
68 <a href="enabletxn.html#security">Security Considerations</a>
69 </span>
70 </dt>
71 </dl>
72 </dd>
73 <dt>
74 <span class="sect1">
75 <a href="envopen.html">Opening a Transactional Environment and
76 <span>Database</span>
77
78
79 </a>
80 </span>
81 </dt>
82 </dl>
83 </div>
84 <p>
85 In order to use transactions with your application, you must turn them
86 on. To do this you must:
87 </p>
88 <div class="itemizedlist">
89 <ul type="disc">
90 <li>
91 <p>
92 Use an
93 environment (see <a class="xref" href="enabletxn.html#environments" title="Environments">Environments</a> for details).
94 </p>
95 </li>
96 <li>
97 <p>
98 Turn on transactions for your environment.
99
100
101
102 <span>
103 You do this by providing the <code class="literal">DB_INIT_TXN</code>
104 flag to the
105
106 <code class="methodname">DB_ENV-&gt;open()</code>
107 method.
108 </span>
109
110 <span>
111 Note that initializing the transactional subsystem implies that
112 the logging subsystem is also initialized. Also, note that
113 if you do not initialize transactions when you first create
114 your environment, then you cannot use transactions for that
115 environment after that. This is because DB
116 allocates certain structures needed for transactional
117 locking that are not available if the environment is
118 created without transactional support.
119 </span>
120 </p>
121 </li>
122 <li>
123 <p>
124 Initialize the in-memory cache by
125 <span>
126 passing the <code class="literal">DB_INIT_MPOOL</code>
127 flag to the
128
129 <code class="methodname">DB_ENV-&gt;open()</code>
130 method.
131 </span>
132
133
134 </p>
135 </li>
136 <li>
137 <p>
138 Initialize the locking subsystem. This is what provides locking for concurrent applications. It also is used
139 to perform deadlock detection. See <a class="xref" href="txnconcurrency.html" title="Chapter 4. Concurrency">Concurrency</a>
140 for more information.
141 </p>
142 <p>
143 You initialize the locking subsystem by
144 <span>
145 passing the <code class="literal">DB_INIT_LOCK</code>
146 flag to the
147
148 <code class="methodname">DB_ENV-&gt;open()</code>
149 method.
150 </span>
151
152
153 </p>
154 </li>
155 <li>
156 <p>
157 Initialize the logging subsystem. While this is enabled by
158 default for transactional applications, we suggest that
159 you explicitly initialize it anyway for the purposes of code readability. The logging
160 subsystem is what provides your transactional application its durability guarantee, and it is required for
161 recoverability purposes. See <a class="xref" href="filemanagement.html" title="Chapter 5. Managing DB Files">Managing DB Files</a>
162 for more information.
163 </p>
164 <p>
165 You initialize the logging subsystem by
166 <span>
167 passing the <code class="literal">DB_INIT_LOG</code>
168 flag to the
169
170 <code class="methodname">DB_ENV-&gt;open()</code>
171 method.
172 </span>
173
174
175 </p>
176 </li>
177 <li>
178 <p>
179 <span>
180 Transaction-enable your databases.
181 </span>
182 <span>
183 If you are using the base API, transaction-enable your databases.
184 </span>
185 You do this by
186
187
188 <span>
189 encapsulating the database open in a transaction.
190 </span>
191
192
193 <span>
194 Note that the common practice is for auto commit to be used to
195 transaction-protect the database open. To use auto-commit, you
196 must still enable transactions as described here, but you do
197 not have to explicitly use a transaction when you open your
198 database. An example of this is given in the next section.
199 </span>
200 </p>
201 </li>
202 </ul>
203 </div>
204 <div class="sect1" lang="en" xml:lang="en">
205 <div class="titlepage">
206 <div>
207 <div>
208 <h2 class="title" style="clear: both"><a id="environments"></a>Environments</h2>
209 </div>
210 </div>
211 </div>
212 <div class="toc">
213 <dl>
214 <dt>
215 <span class="sect2">
216 <a href="enabletxn.html#filenaming">File Naming</a>
217 </span>
218 </dt>
219 <dt>
220 <span class="sect2">
221 <a href="enabletxn.html#errorsupport">Error Support</a>
222 </span>
223 </dt>
224 <dt>
225 <span class="sect2">
226 <a href="enabletxn.html#sharedmemory">Shared Memory Regions</a>
227 </span>
228 </dt>
229 <dt>
230 <span class="sect2">
231 <a href="enabletxn.html#security">Security Considerations</a>
232 </span>
233 </dt>
234 </dl>
235 </div>
236 <p>
237 For simple DB applications, environments are optional. However, in
238 order to transaction protect your database operations, you must use an
239 environment.
240 </p>
241 <p>
242 An <span class="emphasis"><em>environment</em></span>, represents an
243 encapsulation of one or more databases and any associated log and
244 region files. They are used to support multi-threaded
245 and multi-process applications by allowing different threads of
246 control to share the in-memory cache, the locking tables, the
247 logging subsystem, and the file namespace. By sharing these things,
248 your concurrent application is more efficient than if each thread
249 of control had to manage these resources on its own.
250 </p>
251 <p>
252 By default all DB databases are backed by files on disk. In
253 addition to these files, transactional DB applications create
254 logs that are also by default stored on disk (they can optionally
255 be backed using shared memory). Finally, transactional
256 DB applications also create and use shared-memory regions that
257 are also typically backed by the filesystem. But like databases and
258 logs, the regions can be maintained strictly in-memory if your
259 application requires it. For an example of an application that
260 manages all environment files in-memory, see
261 <span><a class="xref" href="inmem_txnexample_c.html" title="In-Memory Transaction Example">In-Memory Transaction Example</a>.</span>
262
263
264
265 </p>
266 <div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
267 <h3 class="title">Warning</h3>
268 <p>
269 Using environments with some journaling filesystems might
270 result in log file corruption. This can occur if the operating
271 system experiences an unclean shutdown when a log file is being
272 created. Please see
273 <a href="../../programmer_reference/transapp_journal.html" class="olink">Using
274 Recovery on Journaling Filesystems</a>
275
276 in the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information.
277 </p>
278 </div>
279 <div class="sect2" lang="en" xml:lang="en">
280 <div class="titlepage">
281 <div>
282 <div>
283 <h3 class="title"><a id="filenaming"></a>File Naming</h3>
284 </div>
285 </div>
286 </div>
287 <p>
288 In order to operate, your DB application must be able to
289 locate its database files, log files, and region files. If these
290 are stored in the filesystem, then you must tell DB where
291 they are located (a number of mechanisms exist that allow you to
292 identify the location of these files – see below). Otherwise,
293 by default they are located in the current working directory.
294 </p>
295 <div class="sect3" lang="en" xml:lang="en">
296 <div class="titlepage">
297 <div>
298 <div>
299 <h4 class="title"><a id="envhome"></a>Specifying the Environment Home Directory</h4>
300 </div>
301 </div>
302 </div>
303 <p>
304 The environment home directory is used to determine where
305 DB files are located. Its location
306 is identified using one of the following mechanisms, in the
307 following order of priority:
308 </p>
309 <div class="itemizedlist">
310 <ul type="disc">
311 <li>
312 <p>
313 If no information is given as to where to put the
314 environment home, then the current working
315 directory is used.
316 </p>
317 </li>
318 <li>
319 <p>
320 If a home directory is specified on the
321 <code class="methodname">DB_ENV-&gt;open()</code>
322
323
324 <span>method,</span>
325
326 then that location is always used for the environment
327 home.
328 </p>
329 </li>
330 <li>
331 <p>
332 If a home directory is not supplied to
333 <span><code class="methodname">DB_ENV-&gt;open()</code>, </span>
334
335
336 then the directory identified by the <code class="literal">DB_HOME</code> environment variable
337 is used <span class="emphasis"><em>if</em></span> you specify
338 <span>
339 either the <code class="literal">DB_USE_ENVIRON</code> or
340 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags to the
341 <code class="methodname">DB_ENV-&gt;open()</code>
342
343 method. Both flags allow you to identify the
344 path to the environment's home directory
345 using the <code class="literal">DB_HOME</code> environment variable. However,
346 <code class="literal">DB_USE_ENVIRON_ROOT</code> is honored only if the
347 process is run with root or administrative privileges.
348 </span>
349
350
351 </p>
352 </li>
353 </ul>
354 </div>
355 </div>
356 <div class="sect3" lang="en" xml:lang="en">
357 <div class="titlepage">
358 <div>
359 <div>
360 <h4 class="title"><a id="filelocation"></a>Specifying File Locations</h4>
361 </div>
362 </div>
363 </div>
364 <p>
365 By default, all DB files are created relative to the environment
366 home directory. For example, suppose your environment home is in
367 <code class="literal">/export/myAppHome</code>. Also suppose you name your database
368 <span><code class="literal">data/myDatabase.db</code>.</span>
369
370 Then in this case, the database is placed in:
371 <span><code class="literal">/export/myAppHome/data/myDatabase.db</code>.</span>
372
373 </p>
374 <p>
375 That said, DB always defers to absolute pathnames.
376 This means that if you provide an absolute filename when you
377 name your database, then that file is <span class="emphasis"><em>not</em></span>
378 placed relative to the environment home directory. Instead, it
379 is placed in the exact location that you specified for the
380 filename.
381 </p>
382 <p>
383 On UNIX systems, an absolute pathname is a name that begins with a
384 forward slash ('/'). On Windows systems, an absolute pathname is a
385 name that begins with one of the following:
386 </p>
387 <div class="itemizedlist">
388 <ul type="disc">
389 <li>
390 <p>
391 A backslash ('\').
392 </p>
393 </li>
394 <li>
395 <p>
396 Any alphabetic letter, followed by a colon (':'), followed
397 by a backslash ('\').
398 </p>
399 </li>
400 </ul>
401 </div>
402 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
403 <h3 class="title">Note</h3>
404 <p>
405 Try not to use absolute path names for your environment's
406 files. Under certain recovery scenarios, absolute path
407 names can render your environment unrecoverable. This
408 occurs if you are attempting to recover your environment on
409 a system that does not support the absolute path name that
410 you used.
411 </p>
412 </div>
413 </div>
414 <div class="sect3" lang="en" xml:lang="en">
415 <div class="titlepage">
416 <div>
417 <div>
418 <h4 class="title"><a id="splittingdata"></a>Identifying Specific File Locations</h4>
419 </div>
420 </div>
421 </div>
422 <p>
423 As described in the previous sections, DB will place all its
424 files in or relative to the environment home directory.
425 You can also cause a
426 specific database file to be placed in a particular location by
427 using an absolute path name for its name. In this
428 situation, the environment's home directory is not
429 considered when naming the file.
430 </p>
431 <p>
432 It is frequently desirable to place database, log, and region files on separate
433 disk drives. By spreading I/O across multiple drives, you
434 can increase parallelism and improve throughput.
435 Additionally, by placing log files and database files on
436 separate drives, you improve your application's
437 reliability by providing your application with a greater
438 chance of surviving a disk failure.
439 </p>
440 <p>
441 You can cause DB's files to be placed in specific
442 locations using the following mechanisms:
443 </p>
444 <div class="informaltable">
445 <table border="1" width="80%">
446 <colgroup>
447 <col />
448 <col />
449 </colgroup>
450 <thead>
451 <tr>
452 <th>File Type</th>
453 <th>To Override</th>
454 </tr>
455 </thead>
456 <tbody>
457 <tr>
458 <td>database files</td>
459 <td>
460 <p>
461 You can cause database files to be created
462 in a directory other than the
463 environment home by using the
464 <code class="methodname">DB_ENV-&gt;add_data_dir()</code>
465
466
467 method. The directory identified here must
468 exist. If a relative path is provided, then
469 the directory location is resolved relative
470 to the environment's home directory.
471 </p>
472
473 <p>
474 This method modifies the directory
475 used for database files created and managed by
476 a single environment handle; it does not
477 configure the entire environment.
478 <span>This
479 method may not be called after the
480 environment has been opened.
481 </span>
482 </p>
483
484 <p>
485 You can also set a default data location that is used by
486 the entire environment by using the
487 <code class="literal">add_data_dir</code> parameter
488 in the environment's <code class="literal">DB_CONFIG</code> file.
489 Note that the <code class="literal">add_data_dir</code>
490 parameter overrides any value set by the
491 <code class="methodname">DB_ENV-&gt;set_data_dir()</code>
492
493
494 method.
495 </p>
496 </td>
497 </tr>
498 <tr>
499 <td>Log files</td>
500 <td>
501 <p>
502 You can cause log files to be created
503 in a directory other than the environment home
504 directory by using the
505 <code class="methodname">DB_ENV-&gt;set_lg_dir()</code>
506
507
508 method. The directory identified
509 here must exist. If a relative path is
510 provided, then the directory location is
511 resolved relative to the environment's home
512 directory.
513 </p>
514
515 <p>
516 This method modifies the directory
517 used for database files created and managed by
518 a single environment handle; it does not
519 configure the entire environment.
520 <span>This
521 method may not be called after the
522 environment has been opened.
523 </span>
524 </p>
525
526 <p>
527 You can also set a default log file location that is used by
528 the entire environment by using the
529 <code class="literal">set_lg_dir</code> parameter
530 in the environment's <code class="literal">DB_CONFIG</code> file.
531 Note that the <code class="literal">set_lg_dir</code>
532 parameter overrides any value set by the
533 <code class="methodname">DB_ENV-&gt;set_lg_dir()</code>
534
535
536 method.
537 </p>
538 </td>
539 </tr>
540 <tr>
541 <td>Temporary files</td>
542 <td>
543 <p>
544 You can cause temporary files required by the
545 environment to be created
546 in a directory other than the environment home
547 directory by using the
548 <code class="methodname">DB_ENV-&gt;set_tmp_dir()</code>
549
550
551 method. The directory identified
552 here must exist. If a relative path is
553 provided, then the directory location is
554 resolved relative to the environment's home
555 directory.
556 </p>
557
558 <p>
559 You can also set a temporary file location
560 by using the
561 <code class="literal">set_tmp_dir</code> parameter
562 in the environment's <code class="literal">DB_CONFIG</code> file.
563 Note that the <code class="literal">set_tmp_dir</code>
564 parameter overrides any value set by the
565 <code class="methodname">DB_ENV-&gt;set_tmp_dir()</code>
566
567
568 method.
569 </p>
570 </td>
571 </tr>
572 <tr>
573 <td>Metadata files</td>
574 <td>
575 <p>
576 You can cause persistent metadata files required by the
577 replicated applications to be created
578 in a directory other than the environment home
579 directory by using the
580 <code class="methodname">DB_ENV-&gt;set_metadata_dir()</code>
581
582
583 method. The directory identified
584 here must exist. If a relative path is
585 provided, then the directory location is
586 resolved relative to the environment's home
587 directory.
588 </p>
589
590 <p>
591 You can also set a metadata directory location
592 by using the
593 <code class="literal">set_metadata_dir</code> parameter
594 in the environment's <code class="literal">DB_CONFIG</code> file.
595 Note that the <code class="literal">set_metadata_dir</code>
596 parameter overrides any value set by the
597 <code class="methodname">DB_ENV-&gt;set_metadata_dir()</code>
598
599
600 method.
601 </p>
602 </td>
603 </tr>
604 <tr>
605 <td>Region files</td>
606 <td>
607 If backed by the filesystem, region
608 files are always placed in the environment home
609 directory.
610 </td>
611 </tr>
612 </tbody>
613 </table>
614 </div>
615 <p>
616 Note that the <code class="literal">DB_CONFIG</code> must reside in the
617 environment home directory. Parameters are specified in it one
618 parameter to a line. Each parameter is followed by a space,
619 which is followed by the parameter value. For example:
620 </p>
621 <pre class="programlisting"> add_data_dir /export1/db/env_data_files </pre>
622 </div>
623 </div>
624 <div class="sect2" lang="en" xml:lang="en">
625 <div class="titlepage">
626 <div>
627 <div>
628 <h3 class="title"><a id="errorsupport"></a>Error Support</h3>
629 </div>
630 </div>
631 </div>
632 <p>
633 To simplify error handling and to aid in application debugging, environments offer several useful
634 methods.
635
636 <span>Note that many of these
637 methods are identical to the error handling methods available for the
638 <span>DB</span>
639
640
641 <span>structure.</span>
642
643
644 </span>
645
646 They are:
647 </p>
648 <div class="itemizedlist">
649 <ul type="disc">
650 <li>
651 <p>
652 <code class="methodname">set_errcall()</code>
653
654 </p>
655 <p>
656 Defines the function that is called when an error message is
657 issued by DB. The error prefix and message are passed to
658 this callback. It is up to the application to display this
659 information correctly.
660 </p>
661 <p>
662 This is the recommended way to get error messages from
663 DB.
664 </p>
665 </li>
666 <li>
667 <p>
668 <code class="methodname">set_errfile()</code>
669 </p>
670 <p>
671 Sets the C library <code class="literal">FILE *</code> to be used for
672 displaying error messages issued by the DB library.
673 </p>
674 </li>
675 <li>
676 <p>
677 <code class="methodname">set_errpfx()</code>
678
679 </p>
680 <p>
681 Sets the prefix used to for any error messages issued by the
682 DB library.
683 </p>
684 </li>
685 <li>
686 <p>
687 <code class="methodname">err()</code>
688 </p>
689 <p>
690 Issues an error message based upon a DB error code a message text that you supply.
691 The error message is sent to the
692 callback function as defined by <code class="methodname">set_errcall()</code>.
693 If that method has not been used, then the error message is sent to the
694 file defined by
695 <span><code class="methodname">set_errfile()</code>.</span>
696
697 If none of these methods have been used, then the error message is sent to
698 standard error.
699 </p>
700 <p>
701 The error message consists of the prefix string
702 (as defined by <code class="methodname">set_errprefix()</code>),
703 an optional <code class="literal">printf</code>-style formatted message,
704 the DB error message associated with the supplied error code,
705 and a trailing newline.
706 </p>
707 </li>
708 <li>
709 <p>
710 <code class="methodname">errx()</code>
711 </p>
712 <p>
713 Behaves identically to <code class="methodname">err()</code> except
714 that you do not provide the DB error code and so
715 the DB message text is not displayed.
716 </p>
717 </li>
718 </ul>
719 </div>
720 <p>
721 In addition, you can use the <code class="function">db_strerror()</code>
722 function to directly return the error string that corresponds to a
723 particular error number. For more information on the
724 <code class="function">db_strerror()</code> function, see the <code class="literal">Error Returns</code>
725 section of the <em class="citetitle">Getting Started with Berkeley DB</em> guide.
726 </p>
727 </div>
728 <div class="sect2" lang="en" xml:lang="en">
729 <div class="titlepage">
730 <div>
731 <div>
732 <h3 class="title"><a id="sharedmemory"></a>Shared Memory Regions</h3>
733 </div>
734 </div>
735 </div>
736 <p>
737 The subsystems that you enable for an environment (in our case,
738 transaction, logging, locking, and the memory pool)
739 are described by one or more regions. The regions contain all of the
740 state information that needs to be shared among threads and/or
741 processes using the environment.
742 </p>
743 <p>
744 Regions may be backed by the file system, by heap memory, or by
745 system shared memory.
746 </p>
747 <div class="sect3" lang="en" xml:lang="en">
748 <div class="titlepage">
749 <div>
750 <div>
751 <h4 class="title"><a id="filebackedregions"></a>Regions Backed by Files</h4>
752 </div>
753 </div>
754 </div>
755 <p>
756 By default, shared memory regions are created as files in the environment's
757 home directory (<span class="emphasis"><em>not</em></span> the environment's data
758 directory). If it is available, the POSIX <code class="literal">mmap</code>
759 interface is used to map these files into your application's
760 address space. If <code class="literal">mmap</code>
761 is not available, then the UNIX <code class="literal">shmget</code> interfaces
762 are used instead (again, if they are available).
763 </p>
764 <p>
765 In this default case, the region files are named
766 <code class="literal">__db.###</code>
767 (for example, <code class="literal">__db.001</code>, <code class="literal">__db.002</code>,
768 and so on).
769 </p>
770 </div>
771 <div class="sect3" lang="en" xml:lang="en">
772 <div class="titlepage">
773 <div>
774 <div>
775 <h4 class="title"><a id="heapbackedregions"></a>Regions Backed by Heap Memory</h4>
776 </div>
777 </div>
778 </div>
779 <p>
780 If heap memory is used to back your shared memory regions, then
781 you can only open a single handle for the environment. This
782 means that the environment cannot be accessed by multiple
783 processes. In this case, the regions are managed only in
784 memory, and they are not written to the filesystem. You
785 indicate that heap memory is to be used for the region files by
786 specifying
787 <span>
788 <code class="literal">DB_PRIVATE</code> to the
789 <code class="methodname">DB_ENV-&gt;open()</code>
790
791 method.
792 </span>
793
794
795 </p>
796 <p>
797 Note that you can also set this flag by using the
798 <code class="literal">set_open_flags</code> parameter in the
799 <code class="literal">DB_CONFIG</code> file. See the
800 <em class="citetitle">Berkeley DB C API Reference Guide</em> for more information.
801 </p>
802 <p>
803 (For an example of an entirely in-memory transactional
804 application, see
805 <span>
806 <a class="xref" href="inmem_txnexample_c.html" title="In-Memory Transaction Example">In-Memory Transaction Example</a>.)
807 </span>
808
809
810
811 </p>
812 </div>
813 <div class="sect3" lang="en" xml:lang="en">
814 <div class="titlepage">
815 <div>
816 <div>
817 <h4 class="title"><a id="systembackedregions"></a>Regions Backed by System Memory</h4>
818 </div>
819 </div>
820 </div>
821 <p>
822 Finally, you can cause system memory to be used for your
823 regions instead of memory-mapped files. You do this by providing
824 <span>
825 <code class="literal">DB_SYSTEM_MEM</code> to the
826 <code class="methodname">DB_ENV-&gt;open()</code>
827
828 method.
829 </span>
830
831
832 </p>
833 <p>
834 When region files are backed by system memory, DB creates a
835 single file in the environment's home directory. This file
836 contains information necessary to identify the system shared
837 memory in use by the environment. By creating this file, DB
838 enables multiple processes to share the environment.
839 </p>
840 <p>
841 The system memory that is used is architecture-dependent. For
842 example, on systems supporting X/Open-style shared memory
843 interfaces, such as UNIX systems, the <code class="literal">shmget(2)</code>
844 and related System V IPC interfaces are used.
845
846 <span>
847
848 Additionally, VxWorks systems use system memory. In these cases,
849 an initial segment ID must be specified by the application to
850 ensure that applications do not overwrite each other's
851 environments, so that the number of segments created does not
852 grow without bounds. See the
853
854 <code class="methodname">DB_ENV-&gt;set_shm_key()</code>
855
856
857 method for more information.
858 </span>
859 </p>
860 <p>
861 On Windows platforms, the use of system memory for the region files
862 is problematic because the operating system uses reference counting
863 to clean up shared objects in the paging file automatically. In
864 addition, the default access permissions for shared objects are
865 different from files, which may cause problems when an environment
866 is accessed by multiple processes running as different users. See
867 <a class="ulink" href="" target="_top">Windows notes</a>
868 or more information.
869 </p>
870 </div>
871 </div>
872 <div class="sect2" lang="en" xml:lang="en">
873 <div class="titlepage">
874 <div>
875 <div>
876 <h3 class="title"><a id="security"></a>Security Considerations</h3>
877 </div>
878 </div>
879 </div>
880 <p>
881 When using environments, there are some security considerations to
882 keep in mind:
883 </p>
884 <div class="itemizedlist">
885 <ul type="disc">
886 <li>
887 <p>
888 Database environment permissions
889 </p>
890 <p>
891 The directory used for the environment
892 should have its permissions set to ensure that files in the
893 environment are not accessible to users without appropriate
894 permissions. Applications that add to the user's permissions
895 (for example, UNIX <code class="literal">setuid</code> or
896 <code class="literal">setgid</code> applications), must be
897 carefully checked to not permit illegal use of those
898 permissions such as general file access in the environment
899 directory.
900 </p>
901 </li>
902 <li>
903 <p>
904 Environment variables
905 </p>
906 <p>
907 Setting
908
909 <span>
910 the <code class="literal">DB_USE_ENVIRON</code> or
911 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
912 </span>
913
914
915 so that environment variables can be used during file naming
916 can be dangerous. Setting those flags in DB
917 applications with additional permissions (for example, UNIX
918 <code class="literal">setuid</code> or <code class="literal">setgid</code>
919 applications) could potentially allow users
920 to read and write databases to which they would not normally
921 have access.
922 </p>
923 <p>
924 For example, suppose you write a DB application
925 that runs <code class="literal">setuid</code>. This means that
926 when the application runs, it does so under a
927 userid different than that of the application's caller.
928 This is especially problematic if the application is
929 granting stronger privileges to a user than the user
930 might ordinarily have.
931 </p>
932 <p>
933 Now, if
934 <span>
935 the <code class="literal">DB_USE_ENVIRON</code> or
936 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
937 are set for the environment,
938 </span>
939
940
941
942 then the environment that the application is
943 using is modifiable using the
944 <code class="literal">DB_HOME</code> environment variable. In
945 this scenario, if the uid used by the application has
946 sufficiently broad privileges, then the application's caller
947 can read and/or write databases owned by another user
948 simply by setting his
949 <code class="literal">DB_HOME</code> environment variable to the
950 environment used by that other user.
951 </p>
952 <p>
953 Note that this scenario need not be malicious; the
954 wrong environment could be used by the application
955 simply by inadvertently specifying the wrong path to
956 <code class="literal">DB_HOME</code>.
957 </p>
958 <p>
959 As always, you should use <code class="literal">setuid</code>
960 sparingly, if at all. But if you do use
961 <code class="literal">setuid</code>, then you should refrain from
962 specifying
963 <span>
964 the <code class="literal">DB_USE_ENVIRON</code> or
965 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
966 </span>
967
968 for the environment open. And, of course, if you must
969 use <code class="literal">setuid</code>, then make sure you use
970 the weakest uid possible – preferably one that is
971 used only by the application itself.
972 </p>
973 </li>
974 <li>
975 <p>
976 File permissions
977 </p>
978 <p>
979 By default, DB always creates database and log files readable and
980 writable by the owner and the group (that is,
981 <code class="literal">S_IRUSR</code>,
982 <code class="literal">S_IWUSR</code>, <code class="literal">S_IRGRP</code> and
983 <code class="literal">S_IWGRP</code>; or octal mode 0660 on historic
984 UNIX systems). The group ownership of created files is based
985 on the system and directory defaults, and is not further
986 specified by DB.
987 </p>
988 </li>
989 <li>
990 <p>
991 Temporary backing files
992 </p>
993 <p>
994 If an unnamed database is created and the cache is too small
995 to hold the database in memory, Berkeley DB will create a
996 temporary physical file to enable it to page the database to
997 disk as needed. In this case, environment variables such as
998 <code class="literal">TMPDIR</code> may be used to specify the
999 location of that temporary file. Although temporary backing
1000 files are created readable and writable by the owner only
1001 (<code class="literal">S_IRUSR</code> and <code class="literal">S_IWUSR</code>,
1002 or octal mode 0600 on historic UNIX systems), some
1003 filesystems may not sufficiently protect temporary files
1004 created in random directories from improper access. To be
1005 absolutely safe, applications storing sensitive data in
1006 unnamed databases should use the
1007 <code class="methodname">DB_ENV-&gt;set_tmp_dir()</code>
1008
1009
1010 method to specify a temporary directory with known permissions.
1011 </p>
1012 </li>
1013 </ul>
1014 </div>
1015 </div>
1016 </div>
1017 </div>
1018 <div class="navfooter">
1019 <hr />
1020 <table width="100%" summary="Navigation footer">
1021 <tr>
1022 <td width="40%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
1023 <td width="20%" align="center"> </td>
1024 <td width="40%" align="right"> <a accesskey="n" href="envopen.html">Next</a></td>
1025 </tr>
1026 <tr>
1027 <td width="40%" align="left" valign="top">Performance Tuning </td>
1028 <td width="20%" align="center">
1029 <a accesskey="h" href="index.html">Home</a>
1030 </td>
1031 <td width="40%" align="right" valign="top"> Opening a Transactional Environment and
1032 <span>Database</span>
1033
1034
1035 </td>
1036 </tr>
1037 </table>
1038 </div>
1039 </body>
1040 </html>