C++ function call for manual SSD-TRIM commands

Need help with C, C++, perl, python, etc?

C++ function call for manual SSD-TRIM commands

Postby Piglou » 2015-07-25 12:24

Hi everybody,

I'm looking for a way to launch "TRIM" commands to a SSD drive myself, from a c++ code I'm going to write, for flash erasing an SSD drive (and not to wait for others things that "should do it automatically in some circumstances if this or this, this and this have been enabled and [..]" but will never tell me if it worked or not :D

I know there is thousands of complicated ways to test and check if it worked, and also software that needs money to do so, that's why I just want to call myself the TRIM functions and read the return value (like true or false) in order to know if it worked !

Does anybody know where I can find the c++ call that could permit me to do so ?
I heard about the GLibC that gives a way to access every user space function related to Linux Kernel (poll, select and others) as standard c++ functions, I suppose that, if there is a way, it will be on the GLibC but how to find it ?

I'm c++ programmer (HMI and network communication with others devices and machines) but with MSDN for Windows, Qt for Windows and Linux, I miss some of the "pure linux" c++ knowledge (may be just a website that could list me all the available functions !)

Thank you in advance :wink:
Posts: 14
Joined: 2015-07-25 11:55

Re: C++ function call for manual SSD-TRIM commands

Postby Piglou » 2015-07-25 13:11

I began to search about GLibC which is GNU C Library :

But searching for TRIM did not give me what I was looking for !

Another thing here :

By looking at hdparm source code may be I have a chance to find how !
Posts: 14
Joined: 2015-07-25 11:55

Re: C++ function call for manual SSD-TRIM commands

Postby dilberts_left_nut » 2015-07-25 21:14

Maybe the source of 'fstrim' from util-linux package could help.
AdrianTM wrote:There's no hacker in my grandma...
User avatar
Posts: 4912
Joined: 2009-10-05 07:54
Location: enzed

Re: C++ function call for manual SSD-TRIM commands

Postby Piglou » 2015-07-26 10:41

I wasn't aware of the existence of this command, thank you !

0 success
1 failure
32 all failed
64 some filesystem discards have succeeded, some failed

Here it can be used on a mounted file system so it's interesting too. I will also look after the ATA_Secure_Erase in order to see if it's the same commands that is used or not

EDIT : here is an interesting part of the fstrim source code, because it's a simple part - it seems that ioctl and device driver at /dev/sdx are able to do this easily
I will do some test on my SSD in order to be sure that I really ask to these commands what I need ! All my datas are on several computer using "Sync" so I can't lost anything, may be some cookies :mrgreen:

Code: Select all
/* returns: 0 = success, 1 = unsupported, < 0 = error */
static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl, int verbose)
   int fd;
   struct stat sb;
   struct fstrim_range range;

   /* kernel modifies the range */
   memcpy(&range, rangetpl, sizeof(range));

   fd = open(path, O_RDONLY);
   if (fd < 0) {
      warn(_("cannot open %s"), path);
      return -1;
   if (fstat(fd, &sb) == -1) {
      warn(_("stat failed %s"), path);
      return -1;
   if (!S_ISDIR(sb.st_mode)) {
      warnx(_("%s: not a directory"), path);
      return -1;
   errno = 0;
   if (ioctl(fd, FITRIM, &range)) {
      int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;

      if (rc != 1)
         warn(_("%s: FITRIM ioctl failed"), path);
      return rc;

   if (verbose) {
      char *str = size_to_human_string(
            (uint64_t) range.len);
      /* TRANSLATORS: The standard value here is a very large number. */
      printf(_("%s: %s (%" PRIu64 " bytes) trimmed\n"),
            path, str, (uint64_t) range.len);
   return 0;

ioctl(fd, FITRIM, &range)

* This program uses FITRIM ioctl to discard parts or the whole filesystem
* online (mounted). You can specify range (start and length) to be
* discarded, or simply discard whole filesystem.

When I have a working application using all this, I come back to you !
I will try to design something cool with Qt.
Posts: 14
Joined: 2015-07-25 11:55

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests