2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <config-util.h>
29 #include <sys/types.h>
39 #include <grub/types.h>
40 #include <grub/misc.h>
41 #include <grub/i18n.h>
42 #include <grub/time.h>
43 #include <grub/emu/misc.h>
45 #ifdef HAVE_DEVICE_MAPPER
46 # include <libdevmapper.h>
49 #ifdef HAVE_SYS_PARAM_H
50 # include <sys/param.h>
53 #ifdef HAVE_SYS_MOUNT_H
54 # include <sys/mount.h>
57 #ifdef HAVE_SYS_MNTTAB_H
58 # include <stdio.h> /* Needed by sys/mnttab.h. */
59 # include <sys/mnttab.h>
62 #ifdef HAVE_SYS_MKDEV_H
63 # include <sys/mkdev.h> /* makedev */
69 grub_util_warn (const char *fmt
, ...)
73 fprintf (stderr
, _("%s: warning:"), program_name
);
74 fprintf (stderr
, " ");
76 vfprintf (stderr
, fmt
, ap
);
78 fprintf (stderr
, ".\n");
83 grub_util_info (const char *fmt
, ...)
89 fprintf (stderr
, _("%s: info:"), program_name
);
90 fprintf (stderr
, " ");
92 vfprintf (stderr
, fmt
, ap
);
94 fprintf (stderr
, ".\n");
100 grub_util_error (const char *fmt
, ...)
104 fprintf (stderr
, _("%s: error:"), program_name
);
105 fprintf (stderr
, " ");
107 vfprintf (stderr
, fmt
, ap
);
109 fprintf (stderr
, ".\n");
114 xmalloc (grub_size_t size
)
120 grub_util_error ("%s", _("out of memory"));
126 xrealloc (void *ptr
, grub_size_t size
)
128 ptr
= realloc (ptr
, size
);
130 grub_util_error ("%s", _("out of memory"));
136 xstrdup (const char *str
)
142 newstr
= (char *) xmalloc (len
+ 1);
143 memcpy (newstr
, str
, len
+ 1);
148 #ifndef HAVE_VASPRINTF
151 vasprintf (char **buf
, const char *fmt
, va_list ap
)
153 /* Should be large enough. */
154 *buf
= xmalloc (512);
156 return vsnprintf (*buf
, 512, fmt
, ap
);
161 #ifndef HAVE_ASPRINTF
164 asprintf (char **buf
, const char *fmt
, ...)
170 status
= vasprintf (buf
, fmt
, ap
);
179 xasprintf (const char *fmt
, ...)
185 if (vasprintf (&result
, fmt
, ap
) < 0)
188 grub_util_error ("%s", _("out of memory"));
202 grub_get_time_ms (void)
206 gettimeofday (&tv
, 0);
208 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
216 gettimeofday (&tv
, 0);
218 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
219 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
220 * GRUB_TICKS_PER_SECOND
/ 1000000));
224 canonicalize_file_name (const char *path
)
228 ret
= xmalloc (PATH_MAX
);
229 if (!_fullpath (ret
, path
, PATH_MAX
))
231 #elif defined (PATH_MAX)
232 ret
= xmalloc (PATH_MAX
);
233 if (!realpath (path
, ret
))
236 ret
= realpath (path
, NULL
);
241 #ifdef HAVE_DEVICE_MAPPER
242 static void device_mapper_null_log (int level
__attribute__ ((unused
)),
243 const char *file
__attribute__ ((unused
)),
244 int line
__attribute__ ((unused
)),
245 int dm_errno
__attribute__ ((unused
)),
246 const char *f
__attribute__ ((unused
)),
252 grub_device_mapper_supported (void)
254 static int supported
= -1;
260 /* Suppress annoying log messages. */
261 dm_log_with_errno_init (&device_mapper_null_log
);
263 dmt
= dm_task_create (DM_DEVICE_VERSION
);
264 supported
= (dmt
!= NULL
);
266 dm_task_destroy (dmt
);
268 /* Restore the original logger. */
269 dm_log_with_errno_init (NULL
);
274 #endif /* HAVE_DEVICE_MAPPER */